Search in sources :

Example 1 with Lock

use of io.etcd.jetcd.Lock in project jetcd by coreos.

the class LockTest method testLockSegregationByNamespaces.

@Test
public void testLockSegregationByNamespaces() throws Exception {
    initializeLockCLient(false);
    // prepare two LockClients with different namespaces, lock operations on one LockClient
    // should have no effect on the other client.
    Client clientWithNamespace = TestUtil.client(cluster).namespace(namespace).build();
    Lock lockClientWithNamespace = clientWithNamespace.getLockClient();
    long lease = grantLease(5);
    CompletableFuture<LockResponse> feature = lockClientWithNamespace.lock(SAMPLE_NAME, lease);
    LockResponse response = feature.get();
    assertThat(response.getKey().startsWith(SAMPLE_NAME)).isTrue();
    // Unlock by full key name using LockClient without namespace, thus it should not take
    // much time to lock the same key again.
    ByteSequence nsKey = ByteSequence.from(namespace.concat(response.getKey()).getBytes());
    lockClient.unlock(nsKey).get();
    lease = grantLease(30);
    CompletableFuture<LockResponse> feature2 = lockClientWithNamespace.lock(SAMPLE_NAME, lease);
    LockResponse response2 = feature2.get();
    long timestamp2 = System.currentTimeMillis();
    long startTime = System.currentTimeMillis();
    assertThat(response2.getKey().startsWith(SAMPLE_NAME)).isTrue();
    assertThat(response2.getKey()).isNotEqualTo(response.getKey());
    assertThat((timestamp2 - startTime) <= 1000).withFailMessage(String.format("Lease not unlocked, wait time was too long (%dms)", timestamp2 - startTime)).isTrue();
    locksToRelease.add(ByteSequence.from(namespace.concat(response2.getKey()).getBytes()));
    // Lock the same key using LockClient with another namespace, it also should not take much time.
    lease = grantLease(5);
    Client clientWithNamespace2 = TestUtil.client(cluster).namespace(namespace2).build();
    Lock lockClientWithNamespace2 = clientWithNamespace2.getLockClient();
    CompletableFuture<LockResponse> feature3 = lockClientWithNamespace2.lock(SAMPLE_NAME, lease);
    LockResponse response3 = feature3.get();
    long timestamp3 = System.currentTimeMillis();
    assertThat(response3.getKey().startsWith(SAMPLE_NAME)).isTrue();
    assertThat(response3.getKey()).isNotEqualTo(response2.getKey());
    assertThat((timestamp3 - timestamp2) <= 1000).withFailMessage(String.format("wait time for requiring the lock was too long (%dms)", timestamp3 - timestamp2)).isTrue();
    locksToRelease.add(ByteSequence.from(namespace2.concat(response3.getKey()).getBytes()));
}
Also used : LockResponse(io.etcd.jetcd.lock.LockResponse) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) Lock(io.etcd.jetcd.Lock) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)1 Client (io.etcd.jetcd.Client)1 Lock (io.etcd.jetcd.Lock)1 LockResponse (io.etcd.jetcd.lock.LockResponse)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1