Search in sources :

Example 6 with ByteSequence

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

the class KVTest method testTxn.

@Test
public void testTxn() throws Exception {
    ByteSequence sampleKey = bytesOf("txn_key");
    ByteSequence sampleValue = bytesOf("xyz");
    ByteSequence cmpValue = bytesOf("abc");
    ByteSequence putValue = bytesOf("XYZ");
    ByteSequence putValueNew = bytesOf("ABC");
    // put the original txn key value pair
    kvClient.put(sampleKey, sampleValue).get();
    // construct txn operation
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.GREATER, CmpTarget.value(cmpValue));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).commit();
    txnResp.get();
    // get the value
    GetResponse getResp = kvClient.get(sampleKey).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8));
}
Also used : Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 7 with ByteSequence

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

the class KVTest method testTxnGetAndDeleteWithPrefix.

@Test
void testTxnGetAndDeleteWithPrefix() throws ExecutionException, InterruptedException {
    String prefix = randomString();
    ByteSequence sampleKey = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // always false cmp
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.EQUAL, CmpTarget.value(bytesOf("not_exists")));
    Op.PutOp putOp = Op.put(bytesOf("other_string"), bytesOf("other_value"), PutOption.DEFAULT);
    Op.GetOp getByPrefix = Op.get(sampleKey, GetOption.newBuilder().isPrefix(true).build());
    Op.DeleteOp delete = Op.delete(sampleKey, DeleteOption.newBuilder().isPrefix(true).withPrevKV(true).build());
    TxnResponse txnResponse = kvClient.txn().If(cmp).Then(putOp).Else(getByPrefix, delete).commit().get();
    List<GetResponse> getResponse = txnResponse.getGetResponses();
    assertThat(getResponse).hasSize(1);
    assertThat(getResponse.get(0).getKvs()).hasSize(10);
    assertThat(getResponse.get(0).getKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    List<DeleteResponse> deleteResponses = txnResponse.getDeleteResponses();
    assertThat(deleteResponses).hasSize(1);
    assertThat(deleteResponses.get(0).getDeleted()).isEqualTo(10);
    assertThat(deleteResponses.get(0).getPrevKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    assertThat(txnResponse.getPutResponses()).isEmpty();
}
Also used : Op(io.etcd.jetcd.op.Op) Cmp(io.etcd.jetcd.op.Cmp) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 8 with ByteSequence

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

the class ClientConnectionManagerTest method testAuthHeaders.

@Test
public void testAuthHeaders() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    Auth authClient = TestUtil.client(cluster).build().getAuthClient();
    authClient.userAdd(root, rootPass).get();
    ByteSequence role = TestUtil.bytesOf("root");
    authClient.userGrantRole(root, role).get();
    authClient.authEnable().get();
    final ClientBuilder builder = TestUtil.client(cluster).authHeader("MyAuthHeader", "MyAuthHeaderVal").header("MyHeader2", "MyHeaderVal2").user(root).password(rootPass);
    assertThat(builder.authHeaders().get(Metadata.Key.of("MyAuthHeader", Metadata.ASCII_STRING_MARSHALLER))).isEqualTo("MyAuthHeaderVal");
    try (Client client = builder.build()) {
        CompletableFuture<AuthDisableResponse> future = client.getAuthClient().authDisable();
        latch.await(10, TimeUnit.SECONDS);
        future.get();
    }
    authClient.userRevokeRole(root, role).get();
    authClient.userDelete(root).get();
}
Also used : Auth(io.etcd.jetcd.Auth) CountDownLatch(java.util.concurrent.CountDownLatch) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) ClientBuilder(io.etcd.jetcd.ClientBuilder) AuthDisableResponse(io.etcd.jetcd.auth.AuthDisableResponse) Test(org.junit.jupiter.api.Test)

Example 9 with ByteSequence

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

the class WatchErrorTest method testWatchOnError.

@ParameterizedTest
@ValueSource(strings = { "test-namespace/", "" })
public void testWatchOnError(String ns) {
    final Client client = ns != null && ns.length() == 0 ? TestUtil.client(cluster).namespace(bytesOf(ns)).build() : TestUtil.client(cluster).build();
    final ByteSequence key = randomByteSequence();
    final List<Throwable> events = Collections.synchronizedList(new ArrayList<>());
    try (Watcher watcher = client.getWatchClient().watch(key, TestUtil::noOpWatchResponseConsumer, events::add)) {
        cluster.cluster().stop();
        await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
    }
    assertThat(events).allMatch(EtcdException.class::isInstance);
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) Client(io.etcd.jetcd.Client) EtcdException(io.etcd.jetcd.common.exception.EtcdException) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ByteSequence(io.etcd.jetcd.ByteSequence) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with ByteSequence

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

the class WatchTest method testWatchOnDelete.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnDelete(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();
    client.getKVClient().put(key, value).get();
    try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {
        client.getKVClient().delete(key).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        WatchEvent event = ref.get().getEvents().get(0);
        assertThat(event.getEventType()).isEqualTo(EventType.DELETE);
        assertThat(Arrays.equals(event.getKeyValue().getKey().getBytes(), key.getBytes())).isTrue();
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchEvent(io.etcd.jetcd.watch.WatchEvent) 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)

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