Elffuss BraidHub

kikocis/java-driver public

Wait for Host to be added before validating it.
Changeset11d8732e0b5b09b0af31c003eac74cd3559a092f89c44e737c707ac42f9cf80c
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-26 17:18 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-11-05 18:37 +01:00
Parents3981e916b2aa

Browse the files at this changeset

5 files changed

modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +12 −13
@@ -15,12 +15,13 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import com.google.common.collect.Iterators;
19-import org.assertj.core.api.AbstractAssert;
20-
2118 import java.util.Iterator;
2219 import java.util.Set;
2320 import java.util.TreeSet;
21+import java.util.concurrent.TimeUnit;
22+
23+import com.google.common.collect.Iterators;
24+import org.assertj.core.api.AbstractAssert;
2425
2526 import static org.assertj.core.api.Assertions.assertThat;
2627
@@ -47,19 +48,17 @@
4748 }
4849
4950 public HostAssert host(int hostNumber) {
50- // TODO at some point this won't work anymore if we have assertions that wait for a node to
51- // join the cluster, e.g. assertThat(cluster).node(3).comesUp().
52- return new HostAssert(
53- TestUtils.findHost(actual, hostNumber),
54- actual);
51+ // Wait for the node to be added if it's not already known.
52+ // In 2.2+ C* does not send an added event until the node is ready so we wait a long time.
53+ Host host = TestUtils.findOrWaitForHost(actual, hostNumber,
54+ 60 + Cluster.NEW_NODE_DELAY_SECONDS, TimeUnit.SECONDS);
55+ return new HostAssert(host, actual);
5556 }
5657
5758 public HostAssert host(String hostAddress) {
58- // TODO at some point this won't work anymore if we have assertions that wait for a node to
59- // join the cluster, e.g. assertThat(cluster).node(3).comesUp().
60- return new HostAssert(
61- TestUtils.findHost(actual, hostAddress),
62- actual);
59+ Host host = TestUtils.findOrWaitForHost(actual, hostAddress,
60+ 60 + Cluster.NEW_NODE_DELAY_SECONDS, TimeUnit.SECONDS);
61+ return new HostAssert(host, actual);
6362 }
6463
6564 /**
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +0 −22
@@ -120,26 +120,4 @@
120120 fail(actual + " did not go down within " + duration + " " + unit);
121121 return this;
122122 }
123-
124- public static class StateListenerBase implements StateListener {
125- @Override
126- public void onAdd(Host host) {
127- }
128-
129- @Override
130- public void onUp(Host host) {
131- }
132-
133- @Override
134- public void onSuspected(Host host) {
135- }
136-
137- @Override
138- public void onDown(Host host) {
139- }
140-
141- @Override
142- public void onRemove(Host host) {
143- }
144- }
145123 }
modified driver-core/src/test/java/com/datastax/driver/core/RefreshConnectedHostTest.java +2 −0
@@ -78,6 +78,8 @@
7878 assertThat(cluster).host(2)
7979 .hasState(State.UP)
8080 .isAtDistance(HostDistance.LOCAL);
81+
82+ // Ensure that the host is added to the Cluster.
8183 assertThat(cluster).host(3)
8284 .comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS+1, SECONDS)
8385 .isAtDistance(HostDistance.IGNORED);
added driver-core/src/test/java/com/datastax/driver/core/StateListenerBase.java +40 −0
@@ -0,0 +1,40 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+public class StateListenerBase implements Host.StateListener {
19+ @Override
20+ public void onAdd(Host host) {
21+ }
22+
23+ @Override
24+ public void onUp(Host host) {
25+ }
26+
27+ @Override
28+ public void onSuspected(Host host) {
29+
30+ }
31+
32+ @Override
33+ public void onDown(Host host) {
34+ }
35+
36+ @Override
37+ public void onRemove(Host host) {
38+ }
39+
40+}
No newline at the end of the file
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +33 −0
@@ -23,6 +23,7 @@
2323 import java.net.ServerSocket;
2424 import java.nio.ByteBuffer;
2525 import java.util.*;
26+import java.util.concurrent.CountDownLatch;
2627 import java.util.concurrent.TimeUnit;
2728
2829 import com.google.common.util.concurrent.Futures;
@@ -384,6 +385,38 @@
384385 }
385386 }
386387
388+ public static Host findOrWaitForHost(Cluster cluster, int node, long duration, TimeUnit unit) {
389+ return findOrWaitForHost(cluster, CCMBridge.ipOfNode(node), duration, unit);
390+ }
391+
392+ public static Host findOrWaitForHost(final Cluster cluster, final String address, long duration, TimeUnit unit) {
393+ Host host = findHost(cluster, address);
394+ if(host == null) {
395+ final CountDownLatch addSignal = new CountDownLatch(1);
396+ Host.StateListener addListener = new StateListenerBase() {
397+ @Override
398+ public void onAdd(Host host) {
399+ if(host.getAddress().getHostAddress().equals(address)) {
400+ // for a new node, because of this we also listen for add events.
401+ addSignal.countDown();
402+ }
403+ }
404+ };
405+ cluster.register(addListener);
406+ try {
407+ // Wait until an add event occurs or we timeout.
408+ if (addSignal.await(duration, unit)) {
409+ host = findHost(cluster, address);
410+ }
411+ } catch (InterruptedException e) {
412+ return null;
413+ } finally {
414+ cluster.unregister(addListener);
415+ }
416+ }
417+ return host;
418+ }
419+
387420 public static Host findHost(Cluster cluster, int hostNumber) {
388421 return findHost(cluster, CCMBridge.ipOfNode(hostNumber));
389422 }