Search in sources :

Example 11 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testNestedTxn.

@Test
public void testNestedTxn() throws Exception {
    ByteSequence foo = bytesOf("txn_foo");
    ByteSequence bar = bytesOf("txn_bar");
    ByteSequence barz = bytesOf("txn_barz");
    ByteSequence abc = bytesOf("txn_abc");
    ByteSequence oneTwoThree = bytesOf("txn_123");
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(foo, Cmp.Op.EQUAL, CmpTarget.version(0));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(foo, bar, PutOption.DEFAULT), Op.txn(null, new Op[] { Op.put(abc, oneTwoThree, PutOption.DEFAULT) }, null)).Else(Op.put(foo, barz, PutOption.DEFAULT)).commit();
    txnResp.get();
    GetResponse getResp = kvClient.get(foo).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8));
    GetResponse getResp2 = kvClient.get(abc).get();
    assertThat(getResp2.getKvs()).hasSize(1);
    assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8));
}
Also used : Op(io.etcd.jetcd.op.Op) 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 12 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testGetAndDeleteWithPrefix.

@Test
public void testGetAndDeleteWithPrefix() throws Exception {
    String prefix = randomString();
    ByteSequence key = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // verify get withPrefix.
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().isPrefix(true).build());
    GetResponse getResp = getFuture.get();
    assertThat(getResp.getCount()).isEqualTo(numPrefixes);
    // verify del withPrefix.
    DeleteOption deleteOpt = DeleteOption.newBuilder().isPrefix(true).build();
    CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
    DeleteResponse delResp = delFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 13 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testGet.

@Test
public void testGet() throws Exception {
    CompletableFuture<PutResponse> feature = kvClient.put(SAMPLE_KEY_2, SAMPLE_VALUE_2);
    feature.get();
    CompletableFuture<GetResponse> getFeature = kvClient.get(SAMPLE_KEY_2);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs()).hasSize(1);
    assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE_2.toString(UTF_8));
    assertThat(!response.isMore()).isTrue();
}
Also used : PutResponse(io.etcd.jetcd.kv.PutResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) Test(org.junit.jupiter.api.Test)

Example 14 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVTest method testGetSortedPrefix.

@Test
public void testGetSortedPrefix() throws Exception {
    String prefix = randomString();
    int numPrefix = 3;
    putKeysWithPrefix(prefix, numPrefix);
    GetOption option = GetOption.newBuilder().withSortField(SortTarget.KEY).withSortOrder(SortOrder.DESCEND).isPrefix(true).build();
    CompletableFuture<GetResponse> getFeature = kvClient.get(bytesOf(prefix), option);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs()).hasSize(numPrefix);
    for (int i = 0; i < numPrefix; i++) {
        assertThat(response.getKvs().get(i).getKey().toString(UTF_8)).isEqualTo(prefix + (numPrefix - i - 1));
        assertThat(response.getKvs().get(i).getValue().toString(UTF_8)).isEqualTo(String.valueOf(numPrefix - i - 1));
    }
}
Also used : GetOption(io.etcd.jetcd.options.GetOption) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) Test(org.junit.jupiter.api.Test)

Example 15 with GetResponse

use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.

the class KVNamespaceTest method assertNonexistentKey.

private static void assertNonexistentKey(KV kvClient, ByteSequence key) throws Exception {
    CompletableFuture<GetResponse> getFeature = kvClient.get(key);
    GetResponse response = getFeature.get();
    assertThat(response.getKvs().size()).isEqualTo(0);
}
Also used : GetResponse(io.etcd.jetcd.kv.GetResponse)

Aggregations

GetResponse (io.etcd.jetcd.kv.GetResponse)21 Test (org.junit.jupiter.api.Test)16 ByteSequence (io.etcd.jetcd.ByteSequence)7 TxnResponse (io.etcd.jetcd.kv.TxnResponse)4 Cmp (io.etcd.jetcd.op.Cmp)4 URL (org.apache.dubbo.common.URL)4 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)4 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)4 Gson (com.google.gson.Gson)3 Txn (io.etcd.jetcd.Txn)3 TestUtil.randomString (io.etcd.jetcd.impl.TestUtil.randomString)3 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)3 PutResponse (io.etcd.jetcd.kv.PutResponse)2 Op (io.etcd.jetcd.op.Op)2 GetOption (io.etcd.jetcd.options.GetOption)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 Client (io.etcd.jetcd.Client)1 KeyValue (io.etcd.jetcd.KeyValue)1 DeleteOption (io.etcd.jetcd.options.DeleteOption)1 Errors (io.etcd.jetcd.support.Errors)1