Search in sources :

Example 31 with ByteSequence

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

the class WatchTest method testMultipleWatch.

@ParameterizedTest
@MethodSource("parameters")
public void testMultipleWatch(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final CountDownLatch latch = new CountDownLatch(2);
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> res = Collections.synchronizedList(new ArrayList<>(2));
    try (Watcher w1 = client.getWatchClient().watch(key, res::add);
        Watcher w2 = client.getWatchClient().watch(key, res::add)) {
        client.getKVClient().put(key, value).get();
        latch.await(4, TimeUnit.SECONDS);
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(res).hasSize(2));
        assertThat(res.get(0)).usingRecursiveComparison().isEqualTo(res.get(1));
        assertThat(res.get(0).getEvents().size()).isEqualTo(1);
        assertThat(res.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(res.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 32 with ByteSequence

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

the class WatchTest method testWatchClose.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchClose(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());
    try (Watcher watcher = client.getWatchClient().watch(key, events::add)) {
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
    }
    client.getKVClient().put(key, randomByteSequence()).get();
    await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).hasSize(1));
    assertThat(events.get(0).getEvents()).hasSize(1);
    assertThat(events.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getValue()).isEqualTo(value);
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 33 with ByteSequence

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

the class WatchTest method testProgressRequest.

@ParameterizedTest
@MethodSource("parameters")
public void testProgressRequest(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final Watch watchClient = client.getWatchClient();
    final AtomicReference<WatchResponse> emptyWatcherEventRef = new AtomicReference<>();
    final AtomicReference<WatchResponse> activeWatcherEventRef = new AtomicReference<>();
    try (Watcher activeWatcher = watchClient.watch(key, activeWatcherEventRef::set);
        Watcher emptyWatcher = watchClient.watch(key.concat(randomByteSequence()), emptyWatcherEventRef::set)) {
        // Check that a requestProgress returns identical revisions initially
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        WatchResponse activeEvent = activeWatcherEventRef.get();
        WatchResponse emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision());
        // Put a value being watched by only the active watcher
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(emptyEvent).isNull();
        assertThat(activeEvent).isNotNull();
        long latestRevision = activeEvent.getHeader().getRevision();
        // verify the next progress notify brings both watchers to the latest revision
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision()).isEqualTo(latestRevision);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 34 with ByteSequence

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

the class WatchTest method testWatchOnPut.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnPut(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();
    try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get()).isNotNull();
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
        assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 35 with ByteSequence

use of io.etcd.jetcd.ByteSequence 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)40 Test (org.junit.jupiter.api.Test)24 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Watcher (io.etcd.jetcd.Watch.Watcher)12 Client (io.etcd.jetcd.Client)11 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Watch (io.etcd.jetcd.Watch)6 TxnResponse (io.etcd.jetcd.kv.TxnResponse)6 Cmp (io.etcd.jetcd.op.Cmp)6 Txn (io.etcd.jetcd.Txn)5 CampaignResponse (io.etcd.jetcd.election.CampaignResponse)5 LeaderResponse (io.etcd.jetcd.election.LeaderResponse)4 GetOption (io.etcd.jetcd.options.GetOption)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Election (io.etcd.jetcd.Election)3