Elffuss BraidHub

kikocis/java-driver public

Make HostConnectionPoolTest more resilient

Use ConditionChecker to validate pool size as it is not guaranteed that
the connection has been established within 1 second so instead wait for
up to 5 seconds using the ConditionChecker.

Increase waiting for connection open attempt from 1 to 2s.
Changeset564f58a4aeb5cde379106bc428f401de096daf9dbbff7fb57dc596e6a7e53c26
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-05-07 00:21 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2016-07-13 13:12 -07:00
Parentsc0fd956db585

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +39 −19
@@ -19,6 +19,7 @@
1919 import com.datastax.driver.core.exceptions.BusyConnectionException;
2020 import com.datastax.driver.core.exceptions.ConnectionException;
2121 import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
22+import com.google.common.base.Predicate;
2223 import com.google.common.util.concurrent.*;
2324 import org.scassandra.cql.PrimitiveType;
2425 import org.scassandra.http.client.PrimingRequest;
@@ -33,6 +34,7 @@
3334 import java.util.concurrent.atomic.AtomicReference;
3435
3536 import static com.datastax.driver.core.Assertions.assertThat;
37+import static com.datastax.driver.core.ConditionChecker.check;
3638 import static com.google.common.collect.Lists.newArrayList;
3739 import static java.util.Collections.singletonList;
3840 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -49,6 +51,22 @@
4951 cluster.close();
5052 }
5153
54+ /**
55+ * Ensure the given pool has the given size within 5 seconds.
56+ *
57+ * @param pool Pool to check.
58+ * @param expectedSize Expected size of pool.
59+ */
60+ private void assertPoolSize(HostConnectionPool pool, final int expectedSize) {
61+ check().before(5, TimeUnit.SECONDS)
62+ .that(pool, new Predicate<HostConnectionPool>() {
63+ @Override
64+ public boolean apply(HostConnectionPool input) {
65+ return input.connections.size() == expectedSize;
66+ }
67+ }).becomesTrue();
68+ }
69+
5270 /**
5371 * Sends 101 requests on pool, asserting that the connection used is in expectedConnections, except for the last request.
5472 */
@@ -157,7 +175,8 @@
157175 requests.addAll(fillConnectionToThreshold(pool, coreConnections));
158176
159177 // Allow time for new connection to be spawned.
160- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
178+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
179+ assertPoolSize(pool, 2);
161180
162181 // Borrow more and ensure the connection returned is a non-core connection.
163182 for (int i = 0; i < 100; i++) {
@@ -203,8 +222,8 @@
203222 requests.addAll(fillConnectionToThreshold(pool, singletonList(core)));
204223
205224 // Reaching the threshold should have triggered the creation of an extra one
206- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
207- assertThat(pool.connections).hasSize(2);
225+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
226+ assertPoolSize(pool, 2);
208227 } finally {
209228 completeRequests(requests);
210229 cluster.close();
@@ -231,8 +250,8 @@
231250
232251 requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1)));
233252
234- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
235- assertThat(pool.connections).hasSize(2);
253+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
254+ assertPoolSize(pool, 2);
236255 Connection connection2 = pool.connections.get(1);
237256
238257 assertThat(connection1.inFlight.get()).isEqualTo(101);
@@ -259,7 +278,8 @@
259278
260279 // Borrowing one more time should resurrect the trashed connection
261280 requests.addAll(sendRequests(1, pool));
262- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
281+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
282+ assertPoolSize(pool, 2);
263283
264284 assertThat(pool.connections).containsExactly(connection2, connection1);
265285 assertThat(pool.trash).isEmpty();
@@ -291,9 +311,9 @@
291311
292312 requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1)));
293313
294- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
314+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
315+ assertPoolSize(pool, 2);
295316 reset(factory);
296- assertThat(pool.connections).hasSize(2);
297317 Connection connection2 = pool.connections.get(1);
298318
299319 assertThat(connection1.inFlight.get()).isEqualTo(101);
@@ -323,7 +343,8 @@
323343 // Fill the live connection to go over the threshold where a second one is needed
324344 requests.addAll(fillConnectionToThreshold(pool, singletonList(connection2)));
325345 assertThat(connection2.inFlight.get()).isEqualTo(101);
326- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
346+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
347+ assertPoolSize(pool, 2);
327348
328349 // Borrow again to get the new connection
329350 MockRequest request = MockRequest.send(pool);
@@ -358,7 +379,7 @@
358379 // Fill core connection enough to trigger creation of another one
359380 requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1)));
360381
361- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
382+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
362383 assertThat(pool.connections).hasSize(2);
363384
364385 // Return enough times to get back under the threshold where one connection is enough
@@ -408,7 +429,7 @@
408429
409430 requests.addAll(fillConnectionToThreshold(pool, singletonList(core)));
410431
411- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
432+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
412433 assertThat(pool.connections).hasSize(2);
413434
414435 // Grab the new non-core connection and replace it with a spy.
@@ -675,7 +696,7 @@
675696 }
676697
677698 // Pool should grow by 1.
678- verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class));
699+ verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
679700 assertThat(pool.connections).hasSize(2);
680701
681702 // Reset factory mock as we'll be checking for new open() invokes later.
@@ -710,12 +731,13 @@
710731
711732 // After some time the a connection should attempt to be opened (but will fail).
712733 verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class));
734+ assertPoolSize(pool, 1);
713735 assertThat(pool.connections).hasSize(1);
714736
715737 // Wait some reasonable amount of time for connection to reestablish then check pool size.
716738 Uninterruptibles.sleepUninterruptibly(readTimeout * 2, TimeUnit.MILLISECONDS);
717739 // Reconnecting failed since listening was enabled.
718- assertThat(pool.connections).hasSize(1);
740+ assertPoolSize(pool, 1);
719741
720742 // Re enable listening then wait for reconnect.
721743 currentClient.enableListener();
@@ -792,8 +814,7 @@
792814 requests.add(MockRequest.send(pool));
793815 verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class));
794816 reset(factory);
795- Uninterruptibles.sleepUninterruptibly(readTimeout, TimeUnit.MILLISECONDS);
796- assertThat(pool.connections).hasSize(i);
817+ assertPoolSize(pool, i);
797818 }
798819 } finally {
799820 completeRequests(requests);
@@ -896,8 +917,7 @@
896917
897918 // Should create up to core connections.
898919 verify(factory, timeout(readTimeout * 8).times(8)).open(any(HostConnectionPool.class));
899- Uninterruptibles.sleepUninterruptibly(readTimeout, TimeUnit.MILLISECONDS);
900- assertThat(pool.connections).hasSize(8);
920+ assertPoolSize(pool, 8);
901921 } finally {
902922 cluster.close();
903923 }
@@ -959,10 +979,10 @@
959979 }
960980
961981 /**
962- * <p/>
982+ * <p>
963983 * This test uses a table named "Java349" with 1000 column and performs asynchronously 100k insertions. While the
964984 * insertions are being executed, the number of opened connection is monitored.
965- * <p/>
985+ * <p>
966986 * If at anytime, the number of opened connections is negative, this test will fail.
967987 *
968988 * @jira_ticket JAVA-349