Elffuss BraidHub

kikocis/java-driver public

Add more integration tests for JAVA-419.
Changeset64a1208dbf6bd2c8977d8fee9e44e6fe77285e35563448a3f57af1102a654baa
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-01-07 19:09 -06:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-27 19:11 +01:00
Parentsd0af041c292a

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +237 −49
@@ -13,6 +13,8 @@
1313 import org.testng.annotations.Test;
1414
1515 import static org.assertj.core.api.Assertions.assertThat;
16+import static org.mockito.Mockito.doReturn;
17+import static org.mockito.Mockito.spy;
1618 import static org.testng.Assert.fail;
1719
1820 public class HostConnectionPoolTest extends CCMBridge.PerClassSingleNodeCluster {
@@ -26,6 +28,14 @@
2628 return Lists.newArrayList(sb.toString());
2729 }
2830
31+ /**
32+ * Ensures that if a fixed-sized pool has filled its core connections that borrowConnection will timeout instead
33+ * of creating a new connection.
34+ *
35+ * @since 2.0.10, 2.1.6
36+ * @jira_ticket JAVA-419
37+ * @test_category connection:connection_pool
38+ */
2939 @Test(groups = "short")
3040 public void fixed_size_pool_should_fill_its_core_connections_and_then_timeout() throws ConnectionException, TimeoutException {
3141 HostConnectionPool pool = createPool(2, 2);
@@ -47,12 +57,53 @@
4757 assertThat(timedOut).isTrue();
4858 }
4959
60+ /**
61+ * Ensures that if a variable-sized pool has filled up to its maximum connections that borrowConnection will
62+ * timeout instead of creating a new connection.
63+ *
64+ * @since 2.0.10, 2.1.6
65+ * @jira_ticket JAVA-419
66+ * @test_category connection:connection_pool
67+ */
68+ @Test(groups = "short")
69+ public void variable_size_pool_should_fill_its_connections_and_then_timeout() throws ConnectionException, TimeoutException {
70+ HostConnectionPool pool = createPool(1, 2);
71+
72+ assertThat(pool.connections.size()).isEqualTo(1);
73+ List<PooledConnection> coreConnections = Lists.newArrayList(pool.connections);
74+
75+ //
76+ for (int i = 0; i < 128; i++) {
77+ PooledConnection connection = pool.borrowConnection(100, MILLISECONDS);
78+ assertThat(coreConnections).contains(connection);
79+ }
80+
81+ // Borrow more and ensure the connection returned is a non-core connection.
82+ for (int i = 0; i < 128; i++) {
83+ PooledConnection connection = pool.borrowConnection(100, MILLISECONDS);
84+ assertThat(coreConnections).doesNotContain(connection);
85+ }
86+
87+ boolean timedOut = false;
88+ try {
89+ pool.borrowConnection(100, MILLISECONDS);
90+ } catch (TimeoutException e) {
91+ timedOut = true;
92+ }
93+ assertThat(timedOut).isTrue();
94+ }
95+
96+ /**
97+ * Ensures that if the core connection pool is full that borrowConnection will create and use a new connection.
98+ *
99+ * @since 2.0.10, 2.1.6
100+ * @jira_ticket JAVA-419
101+ * @test_category connection:connection_pool
102+ */
50103 @Test(groups = "short")
51104 public void should_add_extra_connection_when_core_full() throws ConnectionException, TimeoutException, InterruptedException {
52105 cluster.getConfiguration().getPoolingOptions()
53- .setIdleTimeoutSeconds(20)
54- .setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 1)
55- .setMaxConnectionsPerHost(HostDistance.LOCAL, 128);
106+ .setIdleTimeoutSeconds(20);
56107
57108 HostConnectionPool pool = createPool(1, 2);
58109 PooledConnection core = pool.connections.get(0);
@@ -66,59 +117,187 @@
66117 // Reaching 128 on the core connection should have triggered the creation of an extra one
67118 TimeUnit.MILLISECONDS.sleep(100);
68119 assertThat(pool.connections).hasSize(2);
69- PooledConnection extra1 = pool.connections.get(1);
120+ }
70121
71- assertThat(
72- pool.borrowConnection(100, MILLISECONDS)
73- ).isEqualTo(extra1);
122+ /**
123+ * Ensures that a trashed connection that has not been timed out should be resurrected into the connection pool if
124+ * borrowConnection is called and a new connection is needed.
125+ *
126+ * @since 2.0.10, 2.1.6
127+ * @jira_ticket JAVA-419
128+ * @test_category connection:connection_pool
129+ */
130+ @Test(groups = "long")
131+ public void should_resurrect_trashed_connection_within_idle_timeout() throws ConnectionException, TimeoutException, InterruptedException {
132+ cluster.getConfiguration().getPoolingOptions()
133+ .setIdleTimeoutSeconds(20);
74134
75- // If the extra connection is returned it gets trashed
76- pool.returnConnection(extra1);
77- assertThat(pool.connections).hasSize(1);
135+ HostConnectionPool pool = createPool(1, 2);
136+ PooledConnection connection1 = pool.connections.get(0);
137+
138+ for (int i = 0; i < 101; i++)
139+ assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(connection1);
140+
141+ TimeUnit.MILLISECONDS.sleep(100);
142+ assertThat(pool.connections).hasSize(2);
143+ PooledConnection connection2 = pool.connections.get(1);
144+
145+ assertThat(connection1.inFlight.get()).isEqualTo(101);
146+ assertThat(connection2.inFlight.get()).isEqualTo(0);
147+
148+ // Go back under the capacity of 1 connection
149+ for (int i = 0; i < 51; i++)
150+ pool.returnConnection(connection1);
151+
152+ assertThat(connection1.inFlight.get()).isEqualTo(50);
153+ assertThat(connection2.inFlight.get()).isEqualTo(0);
154+
155+ // Given enough time, one connection gets trashed (and the implementation picks the first one)
156+ TimeUnit.SECONDS.sleep(20);
157+ assertThat(pool.connections).containsExactly(connection2);
158+ assertThat(pool.trash).containsExactly(connection1);
159+
160+ // Now borrow enough to go just under the 1 connection threshold
161+ for (int i = 0; i < 50; i++)
162+ pool.borrowConnection(100, MILLISECONDS);
163+
164+ assertThat(pool.connections).containsExactly(connection2);
165+ assertThat(pool.trash).containsExactly(connection1);
166+ assertThat(connection1.inFlight.get()).isEqualTo(50);
167+ assertThat(connection2.inFlight.get()).isEqualTo(50);
168+
169+ // Borrowing one more time should resurrect the trashed connection
170+ pool.borrowConnection(100, MILLISECONDS);
171+ TimeUnit.MILLISECONDS.sleep(100);
78172
79- // If the core connection gets returned we can borrow it again
80- pool.returnConnection(core);
81- assertThat(
82- pool.borrowConnection(100, MILLISECONDS)
83- ).isEqualTo(core);
173+ assertThat(pool.connections).containsExactly(connection2, connection1);
174+ assertThat(pool.trash).isEmpty();
175+ assertThat(connection1.inFlight.get()).isEqualTo(50);
176+ assertThat(connection2.inFlight.get()).isEqualTo(51);
84177 }
85178
86- @Test(groups = "short")
87- public void should_resurrect_trashed_connection_within_idle_timeout() throws ConnectionException, TimeoutException, InterruptedException {
179+ /**
180+ * Ensures that a trashed connection that has been timed out should not be resurrected into the connection pool if
181+ * borrowConnection is called and a new connection is needed.
182+ *
183+ * @since 2.0.10, 2.1.6
184+ * @jira_ticket JAVA-419
185+ * @test_category connection:connection_pool
186+ */
187+ @Test(groups = "long")
188+ public void should_not_resurrect_trashed_connection_after_idle_timeout() throws ConnectionException, TimeoutException, InterruptedException {
88189 cluster.getConfiguration().getPoolingOptions()
89- .setIdleTimeoutSeconds(20)
90- .setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 1)
91- .setMaxConnectionsPerHost(HostDistance.LOCAL, 128);
190+ .setIdleTimeoutSeconds(20);
92191
93192 HostConnectionPool pool = createPool(1, 2);
94- PooledConnection core = pool.connections.get(0);
193+ PooledConnection connection1 = pool.connections.get(0);
95194
96- for (int i = 0; i < 128; i++)
97- assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(core);
195+ for (int i = 0; i < 101; i++)
196+ assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(connection1);
98197
99198 TimeUnit.MILLISECONDS.sleep(100);
100199 assertThat(pool.connections).hasSize(2);
101- PooledConnection extra1 = pool.connections.get(1);
200+ PooledConnection connection2 = pool.connections.get(1);
102201
103- assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(extra1);
104- pool.returnConnection(extra1);
105- assertThat(pool.connections).hasSize(1);
202+ assertThat(connection1.inFlight.get()).isEqualTo(101);
203+ assertThat(connection2.inFlight.get()).isEqualTo(0);
106204
107- // extra1 is now in the trash, core still full
108- // Borrowing again should resurrect extra1 from the trash
109- assertThat(
110- pool.borrowConnection(100, MILLISECONDS)
111- ).isEqualTo(extra1);
112- assertThat(pool.connections).hasSize(2);
205+ // Go back under the capacity of 1 connection
206+ for (int i = 0; i < 51; i++)
207+ pool.returnConnection(connection1);
208+
209+ assertThat(connection1.inFlight.get()).isEqualTo(50);
210+ assertThat(connection2.inFlight.get()).isEqualTo(0);
211+
212+ // Given enough time, one connection gets trashed (and the implementation picks the first one)
213+ TimeUnit.SECONDS.sleep(20);
214+ assertThat(pool.connections).containsExactly(connection2);
215+ assertThat(pool.trash).containsExactly(connection1);
216+
217+ // Return trashed connection down to 0 inFlight
218+ for (int i = 0; i < 50; i++)
219+ pool.returnConnection(connection1);
220+ assertThat(connection1.inFlight.get()).isEqualTo(0);
221+
222+ // Give enough time for trashed connection to be cleaned up from the trash:
223+ TimeUnit.SECONDS.sleep(30);
224+ assertThat(pool.connections).containsExactly(connection2);
225+ assertThat(pool.trash).isEmpty();
226+ assertThat(connection1.isClosed()).isTrue();
227+
228+ // Fill the live connection to go over the threshold where a second one is needed
229+ for (int i = 0; i < 101; i++)
230+ assertThat(pool.borrowConnection(100, TimeUnit.MILLISECONDS)).isEqualTo(connection2);
231+ assertThat(connection2.inFlight.get()).isEqualTo(101);
232+ TimeUnit.MILLISECONDS.sleep(100);
233+
234+ // Borrow again to get the new connection
235+ PooledConnection connection3 = pool.borrowConnection(100, MILLISECONDS);
236+ assertThat(connection3)
237+ .isNotEqualTo(connection2) // should not be the full connection
238+ .isNotEqualTo(connection1); // should not be the previously trashed one
113239 }
114240
241+ /**
242+ * Ensures that a trashed connection that has been timed out should not be closed until it has 0 in flight requests.
243+ *
244+ * @since 2.0.10, 2.1.6
245+ * @jira_ticket JAVA-419
246+ * @test_category connection:connection_pool
247+ */
115248 @Test(groups = "long")
116- public void should_not_resurrect_trashed_connection_after_idle_timeout() throws ConnectionException, TimeoutException, InterruptedException {
249+ public void should_not_close_trashed_connection_until_no_in_flight()
250+ throws ConnectionException, TimeoutException, InterruptedException {
117251 cluster.getConfiguration().getPoolingOptions()
118- .setIdleTimeoutSeconds(20)
119- .setMinSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 1)
120- .setMaxConnectionsPerHost(HostDistance.LOCAL, 128);
252+ .setIdleTimeoutSeconds(20);
253+
254+ HostConnectionPool pool = createPool(1, 2);
255+ PooledConnection connection1 = pool.connections.get(0);
256+
257+ // Fill core connection enough to trigger creation of another one
258+ for (int i = 0; i < 110; i++)
259+ assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(connection1);
260+
261+ TimeUnit.MILLISECONDS.sleep(100);
262+ assertThat(pool.connections).hasSize(2);
263+
264+ // Return enough times to get back under the threshold where one connection is enough
265+ for (int i = 0; i < 50; i++)
266+ pool.returnConnection(connection1);
267+
268+ // Give enough time for one connection to be trashed. Due to the implementation, this will be the first one.
269+ // It still has in-flight requests so should not get closed.
270+ TimeUnit.SECONDS.sleep(30);
271+ assertThat(pool.trash).containsExactly(connection1);
272+ assertThat(connection1.inFlight.get()).isEqualTo(60);
273+ assertThat(connection1.isClosed()).isFalse();
274+
275+ // Consume all inFlight requests on the trashed connection.
276+ while (connection1.inFlight.get() > 0) {
277+ pool.returnConnection(connection1);
278+ }
279+
280+ // Sleep enough time for the connection to be consider idled and closed.
281+ TimeUnit.SECONDS.sleep(30);
282+
283+ // The connection should be now closed.
284+ // The trashed connection should be closed and not in the pool or trash.
285+ assertThat(connection1.isClosed()).isTrue();
286+ assertThat(pool.connections).doesNotContain(connection1);
287+ assertThat(pool.trash).doesNotContain(connection1);
288+ }
121289
290+ /**
291+ * Ensures that if a connection that has less than the minimum available stream ids is returned to the pool that
292+ * the connection is put in the trash.
293+ *
294+ * @since 2.0.10, 2.1.6
295+ * @jira_ticket JAVA-419
296+ * @test_category connection:connection_pool
297+ */
298+ @Test(groups = "short")
299+ public void should_trash_on_returning_connection_with_insufficient_streams()
300+ throws ConnectionException, TimeoutException, InterruptedException {
122301 HostConnectionPool pool = createPool(1, 2);
123302 PooledConnection core = pool.connections.get(0);
124303
@@ -127,19 +306,27 @@
127306
128307 TimeUnit.MILLISECONDS.sleep(100);
129308 assertThat(pool.connections).hasSize(2);
130- PooledConnection extra1 = pool.connections.get(1);
131309
132- assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(extra1);
310+ // Grab the new non-core connection and replace it with a spy.
311+ PooledConnection extra1 = spy(pool.connections.get(1));
312+ pool.connections.set(1, extra1);
313+
314+ // Borrow 10 times to ensure pool is utilized.
315+ for (int i = 0; i < 10; i++) {
316+ assertThat(pool.borrowConnection(100, MILLISECONDS)).isEqualTo(extra1);
317+ }
318+
319+ // Return connection, it should not be trashed since it still has 9 inflight requests.
133320 pool.returnConnection(extra1);
134- assertThat(pool.connections).hasSize(1);
321+ assertThat(pool.connections).hasSize(2);
135322
136- // Give enough time for extra1 to be cleaned up from the trash:
137- TimeUnit.SECONDS.sleep(30);
323+ // stub the maxAvailableStreams method to return 0, indicating there are no remaining streams.
324+ // this should cause the connection to be replaced and trashed on returnConnection.
325+ doReturn(0).when(extra1).maxAvailableStreams();
138326
139- // Next borrow should create another connection
140- PooledConnection extra2 = pool.borrowConnection(100, MILLISECONDS);
141- assertThat(extra2).isNotEqualTo(extra1);
142- assertThat(extra1.isClosed()).isTrue();
327+ // On returning of the connection, should detect that there are no available streams and trash it.
328+ pool.returnConnection(extra1);
329+ assertThat(pool.connections).hasSize(1);
143330 }
144331
145332 private HostConnectionPool createPool(int coreConnections, int maxConnections) {
@@ -152,14 +339,15 @@
152339 }
153340
154341 /**
155- * Test for #JAVA-349 - Negative openConnection count results in broken connection release.
156- * <p/>
342+ * <p>
157343 * This test uses a table named "Java349" with 1000 column and performs asynchronously 100k insertions. While the
158344 * insertions are being executed, the number of opened connection is monitored.
159345 * <p/>
160346 * If at anytime, the number of opened connections is negative, this test will fail.
161347 *
162- * @throws InterruptedException
348+ * @since 2.0.6, 2.1.1
349+ * @jira_ticket JAVA-349
350+ * @test_category connection:connection_pool
163351 */
164352 @Test(groups = "long", enabled = false /* this test causes timeouts on Jenkins */)
165353 public void open_connections_metric_should_always_be_positive() throws InterruptedException {