Elffuss BraidHub

kikocis/java-driver public

Add test for JAVA-418.
Changesetb4b25c17a6f4922c1fb2651c104526325f36a77439de5468a2621de16f84c262
Authorolim7t <omichallat+github@gmail.com> · 2014-08-19 16:02 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-19 16:02 +02:00
Parents239fdfe59e41

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +54 −0
@@ -19,10 +19,12 @@
1919 import java.util.Collection;
2020 import java.util.List;
2121 import java.util.Locale;
22+import java.util.concurrent.*;
2223
2324 import org.testng.annotations.Test;
2425 import static org.testng.Assert.assertEquals;
2526 import static org.testng.Assert.assertTrue;
27+import static org.testng.Assert.fail;
2628
2729 /**
2830 * Simple test of the Sessions methods against a one node cluster.
@@ -236,6 +238,58 @@
236238
237239 cluster.close();
238240 }
241+ }
239242
243+ /**
244+ * Checks for deadlocks when a session shutdown races with the initialization of the cluster (JAVA-418).
245+ */
246+ @Test(groups = "short")
247+ public void closeDuringClusterInitTest() throws InterruptedException {
248+ for (int i = 0; i < 500; i++) {
249+
250+ // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster
251+ // (note the use of newSession below)
252+ final Cluster cluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "1").build();
253+ final Session session = cluster.newSession();
254+
255+ // Spawn two threads to simulate the race
256+ ExecutorService executor = Executors.newFixedThreadPool(2);
257+ final CountDownLatch startLatch = new CountDownLatch(1);
258+
259+ executor.execute(new Runnable() {
260+ @Override
261+ public void run() {
262+ try {
263+ startLatch.await();
264+ cluster.init();
265+ } catch (InterruptedException e) {
266+ fail("unexpected interruption", e);
267+ }
268+ }
269+ });
270+
271+ executor.execute(new Runnable() {
272+ @Override
273+ public void run() {
274+ try {
275+ startLatch.await();
276+ TimeUnit.MILLISECONDS.sleep(10);
277+ session.close();
278+ } catch (InterruptedException e) {
279+ fail("unexpected interruption", e);
280+ }
281+ }
282+ });
283+
284+ // Start the threads
285+ startLatch.countDown();
286+
287+ executor.shutdown();
288+ boolean normalShutdown = executor.awaitTermination(500, TimeUnit.MILLISECONDS);
289+ assertTrue(normalShutdown);
290+
291+ // The deadlock occurred here before JAVA-418
292+ cluster.close();
293+ }
240294 }
241295 }