Search in sources :

Example 11 with ByteSequence

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

the class WatchTest method testWatchFutureRevisionIsNotOverwrittenOnCreation.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchFutureRevisionIsNotOverwrittenOnCreation(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());
    PutResponse putResponse = client.getKVClient().put(key, value).get();
    long lastSeenRevision = putResponse.getHeader().getRevision();
    WatchOption watchOption = WatchOption.newBuilder().withRevision(lastSeenRevision + 1).build();
    try (Watcher watcher = client.getWatchClient().watch(key, watchOption, events::add)) {
        // resumes (recreates) the watch
        cluster.restart();
        // await().duration() would be better but it's broken
        Thread.sleep(2000);
        assertThat(events.isEmpty()).as("verify that received events list is empty").isTrue();
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) PutResponse(io.etcd.jetcd.kv.PutResponse) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with ByteSequence

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

the class WatchTest method getCompactedRevision.

private static long getCompactedRevision(final Client client, final ByteSequence key) throws Exception {
    final ByteSequence value = randomByteSequence();
    // Insert key twice to ensure we have at least two revisions
    client.getKVClient().put(key, value).get();
    final PutResponse putResponse = client.getKVClient().put(key, value).get();
    // Compact until latest revision
    client.getKVClient().compact(putResponse.getHeader().getRevision()).get();
    return putResponse.getHeader().getRevision() - 1;
}
Also used : PutResponse(io.etcd.jetcd.kv.PutResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence)

Example 13 with ByteSequence

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

the class WatchTest method testNamespacedAndNotNamespacedClient.

@Test
public void testNamespacedAndNotNamespacedClient() throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence nsKey = ByteSequence.from(namespace.concat(key).getBytes());
    final Client client = TestUtil.client(cluster).build();
    final Client nsClient = TestUtil.client(cluster).namespace(namespace).build();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();
    // From client with namespace watch for key. Since client is namespaced it should watch for namespaced key.
    try (Watcher watcher = nsClient.getWatchClient().watch(key, ref::set)) {
        // Using non-namespaced client put namespaced key.
        client.getKVClient().put(nsKey, 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) Client(io.etcd.jetcd.Client) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with ByteSequence

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

the class OptionsUtilTest method check.

static void check(byte[] prefix, byte[] expectedPrefixEndOf) {
    ByteSequence actual = OptionsUtil.prefixEndOf(ByteSequence.from(prefix));
    assertThat(actual).isEqualTo(ByteSequence.from(expectedPrefixEndOf));
}
Also used : ByteSequence(io.etcd.jetcd.ByteSequence)

Example 15 with ByteSequence

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

the class KVNamespaceTest method testTxn.

@Test
public void testTxn() throws Exception {
    // kvClient without namespace used as the judge for the final result
    kvClient = TestUtil.client(cluster).build().getKVClient();
    // kvClient with one namespace used to test operations with namespace
    ByteSequence namespace = ByteSequence.from(TestUtil.randomByteSequence().concat(ByteSequence.NAMESPACE_DELIMITER).getBytes());
    kvClientWithNamespace = TestUtil.client(cluster).namespace(namespace).build().getKVClient();
    // put a key in root namespace, assert that it cannot be seen using kvClient with namespace
    ByteSequence cmpKey = getNonexistentKey();
    putKVWithAssertion(kvClient, cmpKey, TestUtil.randomByteSequence(), null);
    ByteSequence key1 = getNonexistentKey();
    ByteSequence value1 = TestUtil.randomByteSequence();
    putKVWithAssertion(kvClientWithNamespace, key1, value1, null);
    ByteSequence key2 = getNonexistentKey();
    ByteSequence value2 = TestUtil.randomByteSequence();
    putKVWithAssertion(kvClientWithNamespace, key2, value2, null);
    // test comparison passes, with put operation.
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(cmpKey, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.put(key1, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).Else(Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getPutResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getPutResponses().get(0).hasPrevKv()).isTrue();
        assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getKey()).isEqualTo(key1);
        assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getValue()).isEqualTo(value1);
    }
    // test comparison fails, with get operation.
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.get(key1, GetOption.newBuilder().build())).Else(Op.get(key2, GetOption.newBuilder().build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getGetResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().size()).isEqualTo(1);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getKey()).isEqualTo(key2);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getValue()).isEqualTo(value2);
    }
    // test delete operation
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.GREATER, CmpTarget.version(0))).Then(Op.delete(key2, DeleteOption.newBuilder().withPrevKV(true).build())).Else(Op.delete(key1, DeleteOption.newBuilder().withPrevKV(true).build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getDeleteResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().size()).isEqualTo(1);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getKey()).isEqualTo(key2);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getValue()).isEqualTo(value2);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) 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