kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b4b25c17a6f4922c1fb2651c104526325f36a77439de5468a2621de16f84c262 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-08-19 16:02 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-19 16:02 +02:00 |
| Parents | 239fdfe59e41 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +54 −0
| @@ -19,10 +19,12 @@ | ||
| 19 | 19 | import java.util.Collection; |
| 20 | 20 | import java.util.List; |
| 21 | 21 | import java.util.Locale; |
| 22 | +import java.util.concurrent.*; | |
| 22 | 23 | |
| 23 | 24 | import org.testng.annotations.Test; |
| 24 | 25 | import static org.testng.Assert.assertEquals; |
| 25 | 26 | import static org.testng.Assert.assertTrue; |
| 27 | +import static org.testng.Assert.fail; | |
| 26 | 28 | |
| 27 | 29 | /** |
| 28 | 30 | * Simple test of the Sessions methods against a one node cluster. |
| @@ -236,6 +238,58 @@ | ||
| 236 | 238 | |
| 237 | 239 | cluster.close(); |
| 238 | 240 | } |
| 241 | + } | |
| 239 | 242 | |
| 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 | + } | |
| 240 | 294 | } |
| 241 | 295 | } |