kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 564f58a4aeb5cde379106bc428f401de096daf9dbbff7fb57dc596e6a7e53c26 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2016-05-07 00:21 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-13 13:12 -07:00 |
| Parents | c0fd956db585 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +39 −19
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import com.datastax.driver.core.exceptions.BusyConnectionException; |
| 20 | 20 | import com.datastax.driver.core.exceptions.ConnectionException; |
| 21 | 21 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 22 | +import com.google.common.base.Predicate; | |
| 22 | 23 | import com.google.common.util.concurrent.*; |
| 23 | 24 | import org.scassandra.cql.PrimitiveType; |
| 24 | 25 | import org.scassandra.http.client.PrimingRequest; |
| @@ -33,6 +34,7 @@ | ||
| 33 | 34 | import java.util.concurrent.atomic.AtomicReference; |
| 34 | 35 | |
| 35 | 36 | import static com.datastax.driver.core.Assertions.assertThat; |
| 37 | +import static com.datastax.driver.core.ConditionChecker.check; | |
| 36 | 38 | import static com.google.common.collect.Lists.newArrayList; |
| 37 | 39 | import static java.util.Collections.singletonList; |
| 38 | 40 | import static java.util.concurrent.TimeUnit.SECONDS; |
| @@ -49,6 +51,22 @@ | ||
| 49 | 51 | cluster.close(); |
| 50 | 52 | } |
| 51 | 53 | |
| 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 | + | |
| 52 | 70 | /** |
| 53 | 71 | * Sends 101 requests on pool, asserting that the connection used is in expectedConnections, except for the last request. |
| 54 | 72 | */ |
| @@ -157,7 +175,8 @@ | ||
| 157 | 175 | requests.addAll(fillConnectionToThreshold(pool, coreConnections)); |
| 158 | 176 | |
| 159 | 177 | // 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); | |
| 161 | 180 | |
| 162 | 181 | // Borrow more and ensure the connection returned is a non-core connection. |
| 163 | 182 | for (int i = 0; i < 100; i++) { |
| @@ -203,8 +222,8 @@ | ||
| 203 | 222 | requests.addAll(fillConnectionToThreshold(pool, singletonList(core))); |
| 204 | 223 | |
| 205 | 224 | // 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); | |
| 208 | 227 | } finally { |
| 209 | 228 | completeRequests(requests); |
| 210 | 229 | cluster.close(); |
| @@ -231,8 +250,8 @@ | ||
| 231 | 250 | |
| 232 | 251 | requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); |
| 233 | 252 | |
| 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); | |
| 236 | 255 | Connection connection2 = pool.connections.get(1); |
| 237 | 256 | |
| 238 | 257 | assertThat(connection1.inFlight.get()).isEqualTo(101); |
| @@ -259,7 +278,8 @@ | ||
| 259 | 278 | |
| 260 | 279 | // Borrowing one more time should resurrect the trashed connection |
| 261 | 280 | 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); | |
| 263 | 283 | |
| 264 | 284 | assertThat(pool.connections).containsExactly(connection2, connection1); |
| 265 | 285 | assertThat(pool.trash).isEmpty(); |
| @@ -291,9 +311,9 @@ | ||
| 291 | 311 | |
| 292 | 312 | requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); |
| 293 | 313 | |
| 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); | |
| 295 | 316 | reset(factory); |
| 296 | - assertThat(pool.connections).hasSize(2); | |
| 297 | 317 | Connection connection2 = pool.connections.get(1); |
| 298 | 318 | |
| 299 | 319 | assertThat(connection1.inFlight.get()).isEqualTo(101); |
| @@ -323,7 +343,8 @@ | ||
| 323 | 343 | // Fill the live connection to go over the threshold where a second one is needed |
| 324 | 344 | requests.addAll(fillConnectionToThreshold(pool, singletonList(connection2))); |
| 325 | 345 | 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); | |
| 327 | 348 | |
| 328 | 349 | // Borrow again to get the new connection |
| 329 | 350 | MockRequest request = MockRequest.send(pool); |
| @@ -358,7 +379,7 @@ | ||
| 358 | 379 | // Fill core connection enough to trigger creation of another one |
| 359 | 380 | requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); |
| 360 | 381 | |
| 361 | - verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class)); | |
| 382 | + verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); | |
| 362 | 383 | assertThat(pool.connections).hasSize(2); |
| 363 | 384 | |
| 364 | 385 | // Return enough times to get back under the threshold where one connection is enough |
| @@ -408,7 +429,7 @@ | ||
| 408 | 429 | |
| 409 | 430 | requests.addAll(fillConnectionToThreshold(pool, singletonList(core))); |
| 410 | 431 | |
| 411 | - verify(factory, after(1000).times(1)).open(any(HostConnectionPool.class)); | |
| 432 | + verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); | |
| 412 | 433 | assertThat(pool.connections).hasSize(2); |
| 413 | 434 | |
| 414 | 435 | // Grab the new non-core connection and replace it with a spy. |
| @@ -675,7 +696,7 @@ | ||
| 675 | 696 | } |
| 676 | 697 | |
| 677 | 698 | // 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)); | |
| 679 | 700 | assertThat(pool.connections).hasSize(2); |
| 680 | 701 | |
| 681 | 702 | // Reset factory mock as we'll be checking for new open() invokes later. |
| @@ -710,12 +731,13 @@ | ||
| 710 | 731 | |
| 711 | 732 | // After some time the a connection should attempt to be opened (but will fail). |
| 712 | 733 | verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class)); |
| 734 | + assertPoolSize(pool, 1); | |
| 713 | 735 | assertThat(pool.connections).hasSize(1); |
| 714 | 736 | |
| 715 | 737 | // Wait some reasonable amount of time for connection to reestablish then check pool size. |
| 716 | 738 | Uninterruptibles.sleepUninterruptibly(readTimeout * 2, TimeUnit.MILLISECONDS); |
| 717 | 739 | // Reconnecting failed since listening was enabled. |
| 718 | - assertThat(pool.connections).hasSize(1); | |
| 740 | + assertPoolSize(pool, 1); | |
| 719 | 741 | |
| 720 | 742 | // Re enable listening then wait for reconnect. |
| 721 | 743 | currentClient.enableListener(); |
| @@ -792,8 +814,7 @@ | ||
| 792 | 814 | requests.add(MockRequest.send(pool)); |
| 793 | 815 | verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class)); |
| 794 | 816 | reset(factory); |
| 795 | - Uninterruptibles.sleepUninterruptibly(readTimeout, TimeUnit.MILLISECONDS); | |
| 796 | - assertThat(pool.connections).hasSize(i); | |
| 817 | + assertPoolSize(pool, i); | |
| 797 | 818 | } |
| 798 | 819 | } finally { |
| 799 | 820 | completeRequests(requests); |
| @@ -896,8 +917,7 @@ | ||
| 896 | 917 | |
| 897 | 918 | // Should create up to core connections. |
| 898 | 919 | 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); | |
| 901 | 921 | } finally { |
| 902 | 922 | cluster.close(); |
| 903 | 923 | } |
| @@ -959,10 +979,10 @@ | ||
| 959 | 979 | } |
| 960 | 980 | |
| 961 | 981 | /** |
| 962 | - * <p/> | |
| 982 | + * <p> | |
| 963 | 983 | * This test uses a table named "Java349" with 1000 column and performs asynchronously 100k insertions. While the |
| 964 | 984 | * insertions are being executed, the number of opened connection is monitored. |
| 965 | - * <p/> | |
| 985 | + * <p> | |
| 966 | 986 | * If at anytime, the number of opened connections is negative, this test will fail. |
| 967 | 987 | * |
| 968 | 988 | * @jira_ticket JAVA-349 |