use of io.atomix.utils.time.Version in project atomix by atomix.
the class DistributedLockTest method testReleaseOnClose.
/**
* Tests releasing a lock when the client's session is closed.
*/
@Test
public void testReleaseOnClose() throws Throwable {
AsyncDistributedLock lock1 = atomix().lockBuilder("test-lock-on-close", protocol()).build().async();
AsyncDistributedLock lock2 = atomix().lockBuilder("test-lock-on-close", protocol()).build().async();
lock1.lock().join();
CompletableFuture<Version> future = lock2.lock();
lock1.close();
future.join();
}
use of io.atomix.utils.time.Version in project atomix by atomix.
the class AtomicLockProxy method tryLock.
@Override
public CompletableFuture<Optional<Version>> tryLock() {
// If the proxy is currently disconnected from the cluster, we can just fail the lock attempt here.
PrimitiveState state = getProxyClient().getPartition(name()).getState();
if (state != PrimitiveState.CONNECTED) {
return CompletableFuture.completedFuture(Optional.empty());
}
// Create and register a new attempt and invoke the LOCK operation on teh replicated state machine with
// a 0 timeout. The timeout will cause the state machine to immediately reject the request if the lock is
// already owned by another process.
LockAttempt attempt = new LockAttempt();
getProxyClient().acceptBy(name(), service -> service.lock(attempt.id(), 0)).whenComplete((result, error) -> {
if (error != null) {
attempt.completeExceptionally(error);
}
});
return attempt.thenApply(Optional::ofNullable);
}
use of io.atomix.utils.time.Version in project atomix by atomix.
the class DistributedLockProxy method handleLocked.
private void handleLocked(LockEvent event) {
CompletableFuture<Version> future = futures.remove(event.id());
if (future != null) {
this.lock = event.id();
future.complete(new Version(event.version()));
}
}
use of io.atomix.utils.time.Version in project atomix by atomix.
the class AtomicSemaphoreTest method testQueue.
@Test(timeout = 10000)
public void testQueue() throws Exception {
Atomix atomix = atomix();
AtomicSemaphore semaphore = atomix.atomicSemaphoreBuilder("test-semaphore-queue").withProtocol(protocol()).withInitialCapacity(10).build();
CompletableFuture<Version> future20 = semaphore.async().acquire(20);
CompletableFuture<Version> future11 = semaphore.async().acquire(11);
semaphore.increasePermits(1);
assertNotNull(future11);
assertFalse(future20.isDone());
assertEquals(0, semaphore.availablePermits());
CompletableFuture<Version> future21 = semaphore.async().acquire(21);
CompletableFuture<Version> future5 = semaphore.async().acquire(5);
// wakeup 5 and 20
semaphore.release(25);
future5.get(10, TimeUnit.SECONDS);
future20.get(10, TimeUnit.SECONDS);
assertFalse(future21.isDone());
}
use of io.atomix.utils.time.Version in project atomix by atomix.
the class AtomicLockTest method testLockUnlock.
/**
* Tests locking and unlocking a lock.
*/
@Test
public void testLockUnlock() throws Throwable {
AtomicLock lock = atomix().atomicLockBuilder("test-lock-unlock").withProtocol(protocol()).build();
Version version = lock.lock();
assertTrue(lock.isLocked());
assertTrue(lock.isLocked(version));
assertFalse(lock.isLocked(new Version(version.value() + 1)));
lock.unlock();
assertFalse(lock.isLocked());
assertFalse(lock.isLocked(version));
}
Aggregations