Merge pull request #1605 from metamx/friendlierNamespaceTests

Better handle timeouts in namespace tests
This commit is contained in:
Fangjin Yang 2015-08-06 11:54:11 -07:00
commit 78263a95c0
1 changed files with 35 additions and 25 deletions

View File

@ -53,7 +53,6 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.URI;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
@ -139,7 +138,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
} }
@Test(timeout = 500) @Test(timeout = 5_000)
public void testSimpleSubmission() throws ExecutionException, InterruptedException public void testSimpleSubmission() throws ExecutionException, InterruptedException
{ {
URIExtractionNamespace namespace = new URIExtractionNamespace( URIExtractionNamespace namespace = new URIExtractionNamespace(
@ -159,7 +158,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
} }
} }
@Test(timeout = 500) @Test(timeout = 5_000)
public void testRepeatSubmission() throws ExecutionException, InterruptedException public void testRepeatSubmission() throws ExecutionException, InterruptedException
{ {
final int repeatCount = 5; final int repeatCount = 5;
@ -167,6 +166,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
final AtomicLong ranCount = new AtomicLong(0l); final AtomicLong ranCount = new AtomicLong(0l);
final long totalRunCount; final long totalRunCount;
final long start; final long start;
final CountDownLatch latch = new CountDownLatch(repeatCount);
try { try {
final URIExtractionNamespace namespace = new URIExtractionNamespace( final URIExtractionNamespace namespace = new URIExtractionNamespace(
"ns", "ns",
@ -186,16 +186,18 @@ public class NamespaceExtractionCacheManagerExecutorsTest
@Override @Override
public void run() public void run()
{ {
manager.getPostRunnable(namespace, factory, cacheId).run(); try {
ranCount.incrementAndGet(); manager.getPostRunnable(namespace, factory, cacheId).run();
ranCount.incrementAndGet();
}
finally {
latch.countDown();
}
} }
}, },
cacheId cacheId
); );
latch.await();
while (ranCount.get() < repeatCount) {
Thread.sleep(0, 100);
}
long minEnd = start + ((repeatCount - 1) * delay); long minEnd = start + ((repeatCount - 1) * delay);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
Assert.assertTrue( Assert.assertTrue(
@ -248,7 +250,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
executorService.shutdown(); executorService.shutdown();
} }
@Test(timeout = 500) @Test(timeout = 5_000)
public void testDelete() public void testDelete()
throws NoSuchFieldException, IllegalAccessException, InterruptedException, ExecutionException throws NoSuchFieldException, IllegalAccessException, InterruptedException, ExecutionException
{ {
@ -264,6 +266,8 @@ public class NamespaceExtractionCacheManagerExecutorsTest
throws InterruptedException throws InterruptedException
{ {
final CountDownLatch latch = new CountDownLatch(5); final CountDownLatch latch = new CountDownLatch(5);
final CountDownLatch latchMore = new CountDownLatch(10);
final AtomicLong runs = new AtomicLong(0); final AtomicLong runs = new AtomicLong(0);
long prior = 0; long prior = 0;
final URIExtractionNamespace namespace = new URIExtractionNamespace( final URIExtractionNamespace namespace = new URIExtractionNamespace(
@ -283,17 +287,22 @@ public class NamespaceExtractionCacheManagerExecutorsTest
@Override @Override
public void run() public void run()
{ {
if (!Thread.interrupted()) { try {
manager.getPostRunnable(namespace, factory, cacheId).run(); if (!Thread.interrupted()) {
} else { manager.getPostRunnable(namespace, factory, cacheId).run();
Thread.currentThread().interrupt(); } else {
Thread.currentThread().interrupt();
}
if (!Thread.interrupted()) {
runs.incrementAndGet();
} else {
Thread.currentThread().interrupt();
}
} }
if (!Thread.interrupted()) { finally {
runs.incrementAndGet(); latch.countDown();
} else { latchMore.countDown();
Thread.currentThread().interrupt();
} }
latch.countDown();
} }
}, },
cacheId cacheId
@ -303,7 +312,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
Assert.assertFalse(future.isDone()); Assert.assertFalse(future.isDone());
Assert.assertTrue(fnCache.containsKey(ns)); Assert.assertTrue(fnCache.containsKey(ns));
prior = runs.get(); prior = runs.get();
Thread.sleep(20); latchMore.await();
Assert.assertTrue(runs.get() > prior); Assert.assertTrue(runs.get() > prior);
Assert.assertTrue(manager.implData.containsKey(ns)); Assert.assertTrue(manager.implData.containsKey(ns));
@ -318,12 +327,11 @@ public class NamespaceExtractionCacheManagerExecutorsTest
Assert.assertEquals(prior, runs.get()); Assert.assertEquals(prior, runs.get());
} }
@Test(timeout = 500) @Test(timeout = 5_000)
public void testShutdown() public void testShutdown()
throws NoSuchFieldException, IllegalAccessException, InterruptedException, ExecutionException throws NoSuchFieldException, IllegalAccessException, InterruptedException, ExecutionException
{ {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final NamespaceExtractionCacheManager onHeap;
final ListenableFuture future; final ListenableFuture future;
final AtomicLong runs = new AtomicLong(0); final AtomicLong runs = new AtomicLong(0);
long prior = 0; long prior = 0;
@ -359,7 +367,9 @@ public class NamespaceExtractionCacheManagerExecutorsTest
Assert.assertFalse(future.isCancelled()); Assert.assertFalse(future.isCancelled());
Assert.assertFalse(future.isDone()); Assert.assertFalse(future.isDone());
prior = runs.get(); prior = runs.get();
Thread.sleep(50); while (runs.get() <= prior) {
Thread.sleep(50);
}
Assert.assertTrue(runs.get() > prior); Assert.assertTrue(runs.get() > prior);
} }
finally { finally {
@ -377,7 +387,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
Assert.assertTrue(((ListeningScheduledExecutorService) execField.get(manager)).isTerminated()); Assert.assertTrue(((ListeningScheduledExecutorService) execField.get(manager)).isTerminated());
} }
@Test(timeout = 500) @Test(timeout = 5_000)
public void testRunCount() public void testRunCount()
throws InterruptedException, ExecutionException throws InterruptedException, ExecutionException
{ {
@ -426,7 +436,7 @@ public class NamespaceExtractionCacheManagerExecutorsTest
}, },
cacheId cacheId
); );
latch.countDown(); latch.await();
Thread.sleep(20); Thread.sleep(20);
} }
finally { finally {