Elffuss BraidHub

kikocis/java-driver public

Merge pull request #239 from pingtimeout/2.0

Removed "sleep 10 seconds" calls in tests
Changeset598ffc9c7bec2ea84d74d7ef7ce39bca12978255e107221e97516918b5e3bd62
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2014-10-23 10:57 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2014-10-23 10:57 +02:00
Parents196d7c54d175, 347acb58eb70

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +12 −24
@@ -1,5 +1,6 @@
11 package com.datastax.driver.core;
22
3+import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
45 import java.util.concurrent.atomic.AtomicBoolean;
56
@@ -32,24 +33,18 @@
3233 }
3334
3435 public HostAssert comesUpWithin(long duration, TimeUnit unit) {
35- final AtomicBoolean notification = new AtomicBoolean();
36+ final CountDownLatch upSignal = new CountDownLatch(1);
3637 StateListener upListener = new StateListenerBase() {
3738 public void onUp(Host host) {
38- notification.set(true);
39- };
39+ upSignal.countDown();
40+ }
4041 };
4142 cluster.register(upListener);
4243 try {
43- // Make sure the host did not come up while we were installing the listener
44- if (actual.isUp())
44+ // If the host is already up or if we receive the UP signal within given time
45+ if (actual.isUp() || upSignal.await(duration, unit)) {
4546 return this;
46-
47- long maxTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(duration, unit);
48- do {
49- TimeUnit.SECONDS.sleep(10);
50- if (notification.get())
51- return this;
52- } while (System.nanoTime() < maxTime);
47+ }
5348 } catch (InterruptedException e) {
5449 fail("Got interrupted while waiting for host to come up");
5550 } finally {
@@ -60,24 +55,17 @@
6055 }
6156
6257 public HostAssert goesDownWithin(long duration, TimeUnit unit) {
63- final AtomicBoolean notification = new AtomicBoolean();
58+ final CountDownLatch downSignal = new CountDownLatch(1);
6459 StateListener upListener = new StateListenerBase() {
6560 public void onDown(Host host) {
66- notification.set(true);
67- };
61+ downSignal.countDown();
62+ }
6863 };
6964 cluster.register(upListener);
7065 try {
71- // Make sure the host did not go down while we were installing the listener
72- if (actual.state == State.DOWN)
66+ // If the host is already down or if we receive the DOWN signal within given time
67+ if (actual.state == State.DOWN || downSignal.await(duration, unit))
7368 return this;
74-
75- long maxTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(duration, unit);
76- do {
77- TimeUnit.SECONDS.sleep(10);
78- if (notification.get())
79- return this;
80- } while (System.nanoTime() < maxTime);
8169 } catch (InterruptedException e) {
8270 fail("Got interrupted while waiting for host to go down");
8371 } finally {