Skip to content

Instantly share code, notes, and snippets.

@Randgalt
Last active December 25, 2015 20:09
Show Gist options
  • Save Randgalt/7032632 to your computer and use it in GitHub Desktop.
Save Randgalt/7032632 to your computer and use it in GitHub Desktop.
@Test
public void testTimedAcquire() throws Exception
{
final int MAX_WAIT_MS = 10000;
final int SLEEP_MS = 1000;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
try
{
final CountDownLatch latch = new CountDownLatch(1);
final InterProcessLock mutex = makeLock(client);
Executors.newSingleThreadExecutor().submit
(
new Callable<Object>()
{
@Override
public Object call() throws Exception
{
mutex.acquire();
try
{
latch.countDown();
Thread.sleep(SLEEP_MS);
}
finally
{
mutex.release();
}
return null;
}
}
);
latch.await();
long startMs = System.currentTimeMillis();
boolean gotIt = mutex.acquire(MAX_WAIT_MS, TimeUnit.MILLISECONDS);
long elapsedMs = System.currentTimeMillis() - startMs;
Assert.assertTrue(gotIt);
Assert.assertTrue(elapsedMs <= (2 * SLEEP_MS));
System.out.println(elapsedMs);
}
finally
{
client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment