kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 0b734fe4c8d1739536949ea05d99397bfd5ec9f867fdf6f1a6dac6c39fa7de1c |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-07-03 23:12 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-07-03 23:12 +02:00 |
| Parents | 4ed19ed9223c |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/StateListenerTest.java +87 −51
| @@ -15,76 +15,112 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.collect.Sets; | |
| 19 | -import org.testng.annotations.Test; | |
| 18 | +import java.util.concurrent.CountDownLatch; | |
| 19 | + | |
| 20 | +import static java.util.concurrent.TimeUnit.MINUTES; | |
| 20 | 21 | |
| 21 | -import java.util.Set; | |
| 22 | +import org.slf4j.Logger; | |
| 23 | +import org.slf4j.LoggerFactory; | |
| 24 | +import org.testng.annotations.Test; | |
| 22 | 25 | |
| 23 | -import static org.testng.Assert.assertEquals; | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 24 | 27 | |
| 25 | -import static com.datastax.driver.core.TestUtils.*; | |
| 28 | +import static com.datastax.driver.core.StateListenerTest.TestListener.Event.*; | |
| 26 | 29 | |
| 27 | -/** | |
| 28 | - * Simple test of the Sessions methods against a one node cluster. | |
| 29 | - */ | |
| 30 | 30 | public class StateListenerTest { |
| 31 | + private static final Logger logger = LoggerFactory.getLogger(StateListenerTest.class); | |
| 31 | 32 | |
| 32 | 33 | @Test(groups = "long") |
| 33 | - public void listenerTest() throws Throwable { | |
| 34 | - | |
| 35 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(1, Cluster.builder()); | |
| 36 | - Cluster cluster = c.cluster; | |
| 37 | - | |
| 34 | + public void should_receive_events_when_node_states_change() throws InterruptedException { | |
| 35 | + CCMBridge ccm = null; | |
| 36 | + Cluster cluster = null; | |
| 38 | 37 | try { |
| 39 | - CountingListener listener = new CountingListener(); | |
| 38 | + ccm = CCMBridge.builder("test").build(); | |
| 39 | + cluster = Cluster.builder() | |
| 40 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 41 | + .build(); | |
| 42 | + cluster.init(); | |
| 43 | + TestListener listener = new TestListener(); | |
| 40 | 44 | cluster.register(listener); |
| 41 | 45 | |
| 42 | - c.cassandraCluster.bootstrapNode(2); | |
| 43 | - waitFor(CCMBridge.IP_PREFIX + '2', cluster); | |
| 44 | - | |
| 45 | - // We sleep slightly before checking the listener because the node is marked UP | |
| 46 | - // just before the listeners are called, and since waitFor is based on isUP, | |
| 47 | - // it can return *just* before the listener are called. | |
| 48 | - Thread.sleep(500); | |
| 49 | - assertEquals(listener.adds.size(), 1); | |
| 50 | - | |
| 51 | - c.cassandraCluster.forceStop(1); | |
| 52 | - waitForDown(CCMBridge.IP_PREFIX + '1', cluster); | |
| 53 | - Thread.sleep(500); | |
| 54 | - assertEquals(listener.downs.size(), 1); | |
| 55 | - | |
| 56 | - c.cassandraCluster.start(1); | |
| 57 | - waitFor(CCMBridge.IP_PREFIX + '1', cluster); | |
| 58 | - Thread.sleep(500); | |
| 59 | - assertEquals(listener.ups.size(), 1); | |
| 60 | - | |
| 61 | - c.cassandraCluster.decommissionNode(2); | |
| 62 | - waitForDecommission(CCMBridge.IP_PREFIX + '2', cluster); | |
| 63 | - Thread.sleep(500); | |
| 64 | - assertEquals(listener.removes.size(), 1); | |
| 65 | - } catch (Throwable e) { | |
| 66 | - c.errorOut(); | |
| 67 | - throw e; | |
| 46 | + listener.setExpectedEvent(ADD); | |
| 47 | + ccm.bootstrapNode(2); | |
| 48 | + listener.waitForEvent(); | |
| 49 | + | |
| 50 | + listener.setExpectedEvent(DOWN); | |
| 51 | + ccm.forceStop(1); | |
| 52 | + listener.waitForEvent(); | |
| 53 | + | |
| 54 | + listener.setExpectedEvent(UP); | |
| 55 | + ccm.start(1); | |
| 56 | + listener.waitForEvent(); | |
| 57 | + | |
| 58 | + listener.setExpectedEvent(REMOVE); | |
| 59 | + ccm.decommissionNode(2); | |
| 60 | + listener.waitForEvent(); | |
| 61 | + | |
| 68 | 62 | } finally { |
| 69 | - c.discard(); | |
| 63 | + if (cluster != null) | |
| 64 | + cluster.close(); | |
| 65 | + if (ccm != null) | |
| 66 | + ccm.remove(); | |
| 70 | 67 | } |
| 71 | 68 | } |
| 72 | 69 | |
| 73 | - private static class CountingListener implements Host.StateListener { | |
| 70 | + static class TestListener implements Host.StateListener { | |
| 71 | + enum Event {ADD, UP, SUSPECTED, DOWN, REMOVE} | |
| 72 | + | |
| 73 | + volatile CountDownLatch latch; | |
| 74 | + volatile Event expectedEvent; | |
| 75 | + volatile Event actualEvent; | |
| 74 | 76 | |
| 75 | - public Set<Host> adds = Sets.newHashSet(); | |
| 76 | - public Set<Host> removes = Sets.newHashSet(); | |
| 77 | - public Set<Host> ups = Sets.newHashSet(); | |
| 78 | - public Set<Host> downs = Sets.newHashSet(); | |
| 77 | + void setExpectedEvent(Event expectedEvent) { | |
| 78 | + logger.debug("Set expected event {}", expectedEvent); | |
| 79 | + this.expectedEvent = expectedEvent; | |
| 80 | + latch = new CountDownLatch(1); | |
| 81 | + } | |
| 82 | + | |
| 83 | + void waitForEvent() throws InterruptedException { | |
| 84 | + assertThat(latch.await(2, MINUTES)) | |
| 85 | + .as("Timed out waiting for event " + expectedEvent) | |
| 86 | + .isTrue(); | |
| 87 | + assertThat(actualEvent).isEqualTo(expectedEvent); | |
| 88 | + } | |
| 89 | + | |
| 90 | + private void reportActualEvent(Event event) { | |
| 91 | + if (latch.getCount() == 0) { | |
| 92 | + // TODO this actually happens because C* sends REMOVE/ADD/REMOVE on a remove | |
| 93 | + logger.error("Was not waiting for an event but got {} (this should eventually be fixed by JAVA-657)", event); | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + logger.debug("Got event {}", event); | |
| 97 | + actualEvent = event; | |
| 98 | + latch.countDown(); | |
| 99 | + } | |
| 79 | 100 | |
| 80 | - public void onAdd(Host host) { adds.add(host); } | |
| 101 | + @Override | |
| 102 | + public void onAdd(Host host) { | |
| 103 | + reportActualEvent(ADD); | |
| 104 | + } | |
| 81 | 105 | |
| 82 | - public void onUp(Host host) { ups.add(host); } | |
| 106 | + @Override | |
| 107 | + public void onUp(Host host) { | |
| 108 | + reportActualEvent(UP); | |
| 109 | + } | |
| 83 | 110 | |
| 84 | - public void onSuspected(Host host) {} | |
| 111 | + @Override | |
| 112 | + public void onSuspected(Host host) { | |
| 113 | + reportActualEvent(SUSPECTED); | |
| 114 | + } | |
| 85 | 115 | |
| 86 | - public void onDown(Host host) { downs.add(host); } | |
| 116 | + @Override | |
| 117 | + public void onDown(Host host) { | |
| 118 | + reportActualEvent(DOWN); | |
| 119 | + } | |
| 87 | 120 | |
| 88 | - public void onRemove(Host host) { removes.add(host); } | |
| 121 | + @Override | |
| 122 | + public void onRemove(Host host) { | |
| 123 | + reportActualEvent(REMOVE); | |
| 124 | + } | |
| 89 | 125 | } |
| 90 | 126 | } |