Search in sources :

Example 1 with AsyncDistributedLock

use of io.atomix.core.lock.AsyncDistributedLock in project atomix by atomix.

the class DistributedLockTest method testBlockingUnlock.

/**
 * Tests unlocking a lock with a blocking call in the event thread.
 */
@Test
public void testBlockingUnlock() throws Throwable {
    AsyncDistributedLock lock1 = atomix().lockBuilder("test-blocking-unlock", protocol()).build().async();
    AsyncDistributedLock lock2 = atomix().lockBuilder("test-blocking-unlock", protocol()).build().async();
    lock1.lock().thenRun(() -> {
        lock1.unlock().join();
    }).join();
    lock2.lock().join();
}
Also used : AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 2 with AsyncDistributedLock

use of io.atomix.core.lock.AsyncDistributedLock in project atomix by atomix.

the class DistributedLockTest method testTryLockSucceedWithTimeout.

/**
 * Tests attempting to acquire a lock with a timeout.
 */
@Test
public void testTryLockSucceedWithTimeout() throws Throwable {
    AsyncDistributedLock lock1 = atomix().lockBuilder("test-try-lock-succeed-with-timeout", protocol()).build().async();
    AsyncDistributedLock lock2 = atomix().lockBuilder("test-try-lock-succeed-with-timeout", protocol()).build().async();
    lock1.lock().join();
    CompletableFuture<Optional<Version>> future = lock2.tryLock(Duration.ofSeconds(1));
    lock1.unlock().join();
    assertTrue(future.join().isPresent());
}
Also used : Optional(java.util.Optional) AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 3 with AsyncDistributedLock

use of io.atomix.core.lock.AsyncDistributedLock 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();
}
Also used : Version(io.atomix.utils.time.Version) AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 4 with AsyncDistributedLock

use of io.atomix.core.lock.AsyncDistributedLock in project atomix by atomix.

the class DistributedLockTest method testLockUnlock.

/**
 * Tests locking and unlocking a lock.
 */
@Test
public void testLockUnlock() throws Throwable {
    AsyncDistributedLock lock = atomix().lockBuilder("test-lock-unlock", protocol()).build().async();
    lock.lock().join();
    lock.unlock().join();
}
Also used : AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Example 5 with AsyncDistributedLock

use of io.atomix.core.lock.AsyncDistributedLock in project atomix by atomix.

the class DistributedLockTest method testTryLockFail.

/**
 * Tests attempting to acquire a lock.
 */
@Test
public void testTryLockFail() throws Throwable {
    AsyncDistributedLock lock1 = atomix().lockBuilder("test-try-lock-fail", protocol()).build().async();
    AsyncDistributedLock lock2 = atomix().lockBuilder("test-try-lock-fail", protocol()).build().async();
    lock1.lock().join();
    assertFalse(lock2.tryLock().join().isPresent());
}
Also used : AsyncDistributedLock(io.atomix.core.lock.AsyncDistributedLock) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)7 AsyncDistributedLock (io.atomix.core.lock.AsyncDistributedLock)7 Test (org.junit.Test)7 Version (io.atomix.utils.time.Version)1 Optional (java.util.Optional)1