kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 11d8732e0b5b09b0af31c003eac74cd3559a092f89c44e737c707ac42f9cf80c |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-26 17:18 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-11-05 18:37 +01:00 |
| Parents | 3981e916b2aa |
5 files changed
modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +12 −13
| @@ -15,12 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.collect.Iterators; | |
| 19 | -import org.assertj.core.api.AbstractAssert; | |
| 20 | - | |
| 21 | 18 | import java.util.Iterator; |
| 22 | 19 | import java.util.Set; |
| 23 | 20 | 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; | |
| 24 | 25 | |
| 25 | 26 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 27 | |
| @@ -47,19 +48,17 @@ | ||
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | 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); | |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | 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); | |
| 63 | 62 | } |
| 64 | 63 | |
| 65 | 64 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +0 −22
| @@ -120,26 +120,4 @@ | ||
| 120 | 120 | fail(actual + " did not go down within " + duration + " " + unit); |
| 121 | 121 | return this; |
| 122 | 122 | } |
| 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 | - } | |
| 145 | 123 | } |
modified driver-core/src/test/java/com/datastax/driver/core/RefreshConnectedHostTest.java +2 −0
| @@ -78,6 +78,8 @@ | ||
| 78 | 78 | assertThat(cluster).host(2) |
| 79 | 79 | .hasState(State.UP) |
| 80 | 80 | .isAtDistance(HostDistance.LOCAL); |
| 81 | + | |
| 82 | + // Ensure that the host is added to the Cluster. | |
| 81 | 83 | assertThat(cluster).host(3) |
| 82 | 84 | .comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS+1, SECONDS) |
| 83 | 85 | .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 @@ | ||
| 23 | 23 | import java.net.ServerSocket; |
| 24 | 24 | import java.nio.ByteBuffer; |
| 25 | 25 | import java.util.*; |
| 26 | +import java.util.concurrent.CountDownLatch; | |
| 26 | 27 | import java.util.concurrent.TimeUnit; |
| 27 | 28 | |
| 28 | 29 | import com.google.common.util.concurrent.Futures; |
| @@ -384,6 +385,38 @@ | ||
| 384 | 385 | } |
| 385 | 386 | } |
| 386 | 387 | |
| 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 | + | |
| 387 | 420 | public static Host findHost(Cluster cluster, int hostNumber) { |
| 388 | 421 | return findHost(cluster, CCMBridge.ipOfNode(hostNumber)); |
| 389 | 422 | } |