Merge pull request #2121 from metamx/jdbcExtractionNamespaceLocking

Add nicer locking and shorter timeouts to JDBCExtractionNamespaceTest
This commit is contained in:
Fangjin Yang 2015-12-18 19:02:36 -08:00
commit 1b46ea7b3d
1 changed files with 22 additions and 8 deletions

View File

@ -52,6 +52,8 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** /**
* *
@ -96,7 +98,7 @@ public class JDBCExtractionNamespaceTest
private OnHeapNamespaceExtractionCacheManager extractionCacheManager; private OnHeapNamespaceExtractionCacheManager extractionCacheManager;
private final Lifecycle lifecycle = new Lifecycle(); private final Lifecycle lifecycle = new Lifecycle();
private final AtomicLong updates = new AtomicLong(0L); private final AtomicLong updates = new AtomicLong(0L);
private final Object updateLock = new Object(); private final Lock updateLock = new ReentrantLock(true);
private Handle handle; private Handle handle;
@Before @Before
@ -145,7 +147,8 @@ public class JDBCExtractionNamespaceTest
@Override @Override
public String call() throws Exception public String call() throws Exception
{ {
synchronized (updateLock) { updateLock.lockInterruptibly();
try {
log.debug("Running cache populator"); log.debug("Running cache populator");
try { try {
return cachePopulator.call(); return cachePopulator.call();
@ -154,6 +157,9 @@ public class JDBCExtractionNamespaceTest
updates.incrementAndGet(); updates.incrementAndGet();
} }
} }
finally {
updateLock.unlock();
}
} }
}; };
} }
@ -206,7 +212,7 @@ public class JDBCExtractionNamespaceTest
Thread.sleep(2); Thread.sleep(2);
} }
@Test(timeout = 60_000L) @Test(timeout = 10_000L)
public void testMapping() public void testMapping()
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, ExecutionException, throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, ExecutionException,
InterruptedException, TimeoutException InterruptedException, TimeoutException
@ -231,7 +237,7 @@ public class JDBCExtractionNamespaceTest
Assert.assertEquals("null check", null, extractionFn.apply("baz")); Assert.assertEquals("null check", null, extractionFn.apply("baz"));
} }
@Test(timeout = 60_000L) @Test(timeout = 10_000L)
public void testReverseLookup() throws InterruptedException public void testReverseLookup() throws InterruptedException
{ {
final JDBCExtractionNamespace extractionNamespace = new JDBCExtractionNamespace( final JDBCExtractionNamespace extractionNamespace = new JDBCExtractionNamespace(
@ -254,7 +260,7 @@ public class JDBCExtractionNamespaceTest
reverseExtractionFn.apply("does't exist")); reverseExtractionFn.apply("does't exist"));
} }
@Test(timeout = 60_000L) @Test(timeout = 10_000L)
public void testSkipOld() public void testSkipOld()
throws NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException throws NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException
{ {
@ -269,7 +275,7 @@ public class JDBCExtractionNamespaceTest
assertUpdated(extractionNamespace.getNamespace(), "foo", "bar"); assertUpdated(extractionNamespace.getNamespace(), "foo", "bar");
} }
@Test(timeout = 60_000L) @Test(timeout = 10_000L)
public void testFindNew() public void testFindNew()
throws NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException throws NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException
{ {
@ -310,18 +316,26 @@ public class JDBCExtractionNamespaceTest
{ {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
long pre = 0L; long pre = 0L;
synchronized (updateLock) { updateLock.lockInterruptibly();
try {
pre = updates.get(); pre = updates.get();
} }
finally {
updateLock.unlock();
}
long post = 0L; long post = 0L;
do { do {
// Sleep to spare a few cpu cycles // Sleep to spare a few cpu cycles
Thread.sleep(5); Thread.sleep(5);
log.debug("Waiting for updateLock"); log.debug("Waiting for updateLock");
synchronized (updateLock) { updateLock.lockInterruptibly();
try {
Assert.assertTrue("Failed waiting for update", System.currentTimeMillis() - startTime < timeout); Assert.assertTrue("Failed waiting for update", System.currentTimeMillis() - startTime < timeout);
post = updates.get(); post = updates.get();
} }
finally {
updateLock.unlock();
}
} while (post < pre + numUpdates); } while (post < pre + numUpdates);
} }