kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 598ffc9c7bec2ea84d74d7ef7ce39bca12978255e107221e97516918b5e3bd62 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2014-10-23 10:57 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2014-10-23 10:57 +02:00 |
| Parents | 196d7c54d175, 347acb58eb70 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +12 −24
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 3 | +import java.util.concurrent.CountDownLatch; | |
| 3 | 4 | import java.util.concurrent.TimeUnit; |
| 4 | 5 | import java.util.concurrent.atomic.AtomicBoolean; |
| 5 | 6 | |
| @@ -32,24 +33,18 @@ | ||
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | public HostAssert comesUpWithin(long duration, TimeUnit unit) { |
| 35 | - final AtomicBoolean notification = new AtomicBoolean(); | |
| 36 | + final CountDownLatch upSignal = new CountDownLatch(1); | |
| 36 | 37 | StateListener upListener = new StateListenerBase() { |
| 37 | 38 | public void onUp(Host host) { |
| 38 | - notification.set(true); | |
| 39 | - }; | |
| 39 | + upSignal.countDown(); | |
| 40 | + } | |
| 40 | 41 | }; |
| 41 | 42 | cluster.register(upListener); |
| 42 | 43 | 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)) { | |
| 45 | 46 | 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 | + } | |
| 53 | 48 | } catch (InterruptedException e) { |
| 54 | 49 | fail("Got interrupted while waiting for host to come up"); |
| 55 | 50 | } finally { |
| @@ -60,24 +55,17 @@ | ||
| 60 | 55 | } |
| 61 | 56 | |
| 62 | 57 | public HostAssert goesDownWithin(long duration, TimeUnit unit) { |
| 63 | - final AtomicBoolean notification = new AtomicBoolean(); | |
| 58 | + final CountDownLatch downSignal = new CountDownLatch(1); | |
| 64 | 59 | StateListener upListener = new StateListenerBase() { |
| 65 | 60 | public void onDown(Host host) { |
| 66 | - notification.set(true); | |
| 67 | - }; | |
| 61 | + downSignal.countDown(); | |
| 62 | + } | |
| 68 | 63 | }; |
| 69 | 64 | cluster.register(upListener); |
| 70 | 65 | 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)) | |
| 73 | 68 | 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); | |
| 81 | 69 | } catch (InterruptedException e) { |
| 82 | 70 | fail("Got interrupted while waiting for host to go down"); |
| 83 | 71 | } finally { |