Search in sources :

Example 1 with Op

use of io.etcd.jetcd.op.Op 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 2 with Op

use of io.etcd.jetcd.op.Op in project jetcd by coreos.

the class KVNamespaceTest method testNestedTxn.

@Test
public void testNestedTxn() 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();
    ByteSequence cmpKey1 = getNonexistentKey();
    putKVWithAssertion(kvClient, cmpKey1, TestUtil.randomByteSequence(), null);
    ByteSequence cmpKey2 = getNonexistentKey();
    putKVWithAssertion(kvClientWithNamespace, cmpKey2, 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);
    {
        Txn txn = kvClientWithNamespace.txn();
        ByteSequence nextValue1 = TestUtil.randomByteSequence();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(cmpKey1, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.txn(new Cmp[] { new Cmp(cmpKey2, Cmp.Op.GREATER, CmpTarget.version(0)) }, new Op[] { Op.put(key1, nextValue1, PutOption.newBuilder().withPrevKV().build()) }, new Op[] { Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) })).Else(Op.txn(new Cmp[] { new Cmp(cmpKey2, Cmp.Op.GREATER, CmpTarget.version(0)) }, new Op[] { Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) }, new Op[] { Op.put(key1, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build()) })).commit();
        TxnResponse response = txnFuture.get();
        assertThat(response.getTxnResponses().size()).isEqualTo(1);
        assertThat(response.getTxnResponses().get(0).getPutResponses().size()).isEqualTo(1);
        assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).hasPrevKv()).isTrue();
        assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).getPrevKv().getKey()).isEqualTo(key1);
        assertThat(response.getTxnResponses().get(0).getPutResponses().get(0).getPrevKv().getValue()).isEqualTo(value1);
        value1 = nextValue1;
        assertExistentKey(kvClient, ByteSequence.from(namespace.concat(key1).getBytes()), value1);
    }
}
Also used : Op(io.etcd.jetcd.op.Op) 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)2 Txn (io.etcd.jetcd.Txn)2 TxnResponse (io.etcd.jetcd.kv.TxnResponse)2 Cmp (io.etcd.jetcd.op.Cmp)2 Op (io.etcd.jetcd.op.Op)2 Test (org.junit.jupiter.api.Test)2 GetResponse (io.etcd.jetcd.kv.GetResponse)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1