Skip to content

Instantly share code, notes, and snippets.

@xdom
Last active September 25, 2016 11:49
Show Gist options
  • Save xdom/e1cf5cc2e457e20bc52a8f39f53bb37f to your computer and use it in GitHub Desktop.
Save xdom/e1cf5cc2e457e20bc52a8f39f53bb37f to your computer and use it in GitHub Desktop.
Lock acquiring test. First obtains a lock and then in new thread tries to obtain lock with the same lock ID. The lock should be obtained only in case that the lock was released by the original thread.
@Test
public void testLockAcquiring() throws Exception {
final AtomicBoolean released = new AtomicBoolean(false);
// acquire lock
Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT);
LockServer remote = ((LockServer) registry.lookup(LockServer.BINDING_NAME));
assertTrue(remote.acquire("lock", null, 0));
// lock in new Thread, and when completed, assert that lock had been released
CompletableFuture.supplyAsync(new LockAcquirer())
.thenAccept(r -> assertTrue(released.get()));
// release the lock
remote.release("lock", null);
released.compareAndSet(false, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment