Search in sources :

Example 1 with LockResponse

use of io.etcd.jetcd.lock.LockResponse in project jetcd by coreos.

the class LockTest method testLockWithLease.

@ParameterizedTest
@MethodSource("parameters")
public void testLockWithLease(boolean useNamespace) throws Exception {
    initializeLockCLient(useNamespace);
    long lease = grantLease(5);
    CompletableFuture<LockResponse> feature = lockClient.lock(SAMPLE_NAME, lease);
    LockResponse response = feature.get();
    long startMillis = System.currentTimeMillis();
    CompletableFuture<LockResponse> feature2 = lockClient.lock(SAMPLE_NAME, 0);
    LockResponse response2 = feature2.get();
    long time = System.currentTimeMillis() - startMillis;
    assertThat(response2.getKey()).isNotEqualTo(response.getKey());
    assertThat(time >= 4500 && time <= 6000).withFailMessage(String.format("Lease not runned out after 5000ms, was %dms", time)).isTrue();
    locksToRelease.add(response.getKey());
    locksToRelease.add(response2.getKey());
}
Also used : LockResponse(io.etcd.jetcd.lock.LockResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with LockResponse

use of io.etcd.jetcd.lock.LockResponse in project jetcd by coreos.

the class LockImpl method lock.

@Override
public CompletableFuture<LockResponse> lock(ByteSequence name, long leaseId) {
    checkNotNull(name);
    LockRequest request = LockRequest.newBuilder().setName(Util.prefixNamespace(name, namespace)).setLease(leaseId).build();
    return execute(() -> stub.lock(request), response -> new LockResponse(response, namespace), Errors::isRetryable);
}
Also used : Errors(io.etcd.jetcd.support.Errors) LockResponse(io.etcd.jetcd.lock.LockResponse) LockRequest(io.etcd.jetcd.api.lock.LockRequest)

Example 3 with LockResponse

use of io.etcd.jetcd.lock.LockResponse in project jetcd by coreos.

the class LockTest method testLockAndUnlock.

@ParameterizedTest
@MethodSource("parameters")
public void testLockAndUnlock(boolean useNamespace) throws Exception {
    initializeLockCLient(useNamespace);
    long lease = grantLease(20);
    CompletableFuture<LockResponse> feature = lockClient.lock(SAMPLE_NAME, lease);
    LockResponse response = feature.get();
    lockClient.unlock(response.getKey()).get();
    long startTime = System.currentTimeMillis();
    CompletableFuture<LockResponse> feature2 = lockClient.lock(SAMPLE_NAME, 0);
    LockResponse response2 = feature2.get();
    long time = System.currentTimeMillis() - startTime;
    locksToRelease.add(response2.getKey());
    assertThat(response2.getKey()).isNotEqualTo(response.getKey());
    assertThat(time <= 500).withFailMessage(String.format("Lease not unlocked, wait time was too long (%dms)", time)).isTrue();
}
Also used : LockResponse(io.etcd.jetcd.lock.LockResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with LockResponse

use of io.etcd.jetcd.lock.LockResponse in project jetcd by coreos.

the class LockTest method testLockWithoutLease.

@ParameterizedTest
@MethodSource("parameters")
public void testLockWithoutLease(boolean useNamespace) throws Exception {
    initializeLockCLient(useNamespace);
    CompletableFuture<LockResponse> feature = lockClient.lock(SAMPLE_NAME, 0);
    LockResponse response = feature.get();
    locksToRelease.add(response.getKey());
    assertThat(response.getHeader()).isNotNull();
    assertThat(response.getKey()).isNotNull();
}
Also used : LockResponse(io.etcd.jetcd.lock.LockResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with LockResponse

use of io.etcd.jetcd.lock.LockResponse 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

LockResponse (io.etcd.jetcd.lock.LockResponse)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 ByteSequence (io.etcd.jetcd.ByteSequence)1 Client (io.etcd.jetcd.Client)1 Lock (io.etcd.jetcd.Lock)1 LockRequest (io.etcd.jetcd.api.lock.LockRequest)1 Errors (io.etcd.jetcd.support.Errors)1 Test (org.junit.jupiter.api.Test)1