Search in sources :

Example 1 with PutResponse

use of com.coreos.jetcd.api.PutResponse in project jetcd by coreos.

the class LeaseTest method testkeepAlive.

@Test(dependsOnMethods = "testRevoke")
public void testkeepAlive() throws Exception {
    long leaseID = leaseClient.grant(5).get().getID();
    PutResponse putRep = kvClient.put(testKey, testName, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    test.assertEquals(kvClient.get(testKey).get().getCount(), 1);
    leaseClient.startKeepAliveService();
    leaseClient.keepAlive(leaseID, new LeaseHandler() {

        @Override
        public void onKeepAliveRespond(LeaseKeepAliveResponse keepAliveResponse) {
        }

        @Override
        public void onLeaseExpired(long leaseId) {
        }

        @Override
        public void onError(Throwable throwable) {
        }
    });
    Thread.sleep(6000);
    test.assertEquals(kvClient.get(testKey).get().getCount(), 1);
    leaseClient.cancelKeepAlive(leaseID);
    test.assertEquals(kvClient.get(testKey).get().getCount(), 0);
}
Also used : LeaseKeepAliveResponse(com.coreos.jetcd.api.LeaseKeepAliveResponse) LeaseHandler(com.coreos.jetcd.Lease.LeaseHandler) PutResponse(com.coreos.jetcd.api.PutResponse) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 2 with PutResponse

use of com.coreos.jetcd.api.PutResponse in project jetcd by coreos.

the class LeaseTest method testGrant.

@Test
public void testGrant() throws Exception {
    long leaseID = leaseClient.grant(5).get().getID();
    PutResponse putRep = kvClient.put(testKey, testName, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    test.assertEquals(kvClient.get(testKey).get().getCount(), 1);
    Thread.sleep(6000);
    test.assertEquals(kvClient.get(testKey).get().getCount(), 0);
}
Also used : PutResponse(com.coreos.jetcd.api.PutResponse) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 3 with PutResponse

use of com.coreos.jetcd.api.PutResponse in project jetcd by coreos.

the class LeaseTest method testRevoke.

@Test(dependsOnMethods = "testGrant")
public void testRevoke() throws Exception {
    long leaseID = leaseClient.grant(5).get().getID();
    PutResponse putRep = kvClient.put(testKey, testName, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    test.assertEquals(kvClient.get(testKey).get().getCount(), 1);
    leaseClient.revoke(leaseID).get();
    test.assertEquals(kvClient.get(testKey).get().getCount(), 0);
}
Also used : PutResponse(com.coreos.jetcd.api.PutResponse) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 4 with PutResponse

use of com.coreos.jetcd.api.PutResponse in project jetcd by coreos.

the class KVTest method testTxn.

@Test
public void testTxn() throws Exception {
    ByteString sampleKey = ByteString.copyFrom("txn_key", "UTF-8");
    ByteString sampleValue = ByteString.copyFrom("xyz", "UTF-8");
    ByteString cmpValue = ByteString.copyFrom("abc", "UTF-8");
    ByteString putValue = ByteString.copyFrom("XYZ", "UTF-8");
    ByteString putValueNew = ByteString.copyFrom("ABC", "UTF-8");
    ListenableFuture<PutResponse> feature = kvClient.put(sampleKey, sampleValue);
    try {
        // put the original txn key value pair
        PutResponse putResp = feature.get();
        // construct txn operation
        Cmp cmp = new Cmp(sampleKey, Cmp.Op.GREATER, CmpTarget.value(cmpValue));
        Txn txn = Txn.newBuilder().If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).build();
        ListenableFuture<TxnResponse> txnResp = kvClient.commit(txn);
        txnResp.get();
        // get the value
        RangeResponse getResp = kvClient.get(sampleKey).get();
        test.assertEquals(getResp.getKvsList().size(), 1);
        test.assertEquals(getResp.getKvs(0).getValue().toStringUtf8(), "XYZ");
    } catch (Exception e) {
    // empty
    }
}
Also used : DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) Cmp(com.coreos.jetcd.op.Cmp) Txn(com.coreos.jetcd.op.Txn) TxnResponse(com.coreos.jetcd.api.TxnResponse) PutResponse(com.coreos.jetcd.api.PutResponse) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with PutResponse

use of com.coreos.jetcd.api.PutResponse in project jetcd by coreos.

the class KVTest method testGet.

@Test
public void testGet() throws Exception {
    ByteString sampleKey = ByteString.copyFrom("sample_key2", "UTF-8");
    ByteString sampleValue = ByteString.copyFrom("sample_value2", "UTF-8");
    ListenableFuture<PutResponse> feature = kvClient.put(sampleKey, sampleValue);
    try {
        feature.get();
        ListenableFuture<RangeResponse> getFeature = kvClient.get(sampleKey);
        RangeResponse response = getFeature.get();
        test.assertEquals(response.getKvsCount(), 1);
        test.assertEquals(response.getKvs(0).getValue().toStringUtf8(), "sample_value2");
        test.assertTrue(!response.getMore());
    } catch (Exception e) {
    // empty
    }
}
Also used : DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) PutResponse(com.coreos.jetcd.api.PutResponse) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

PutResponse (com.coreos.jetcd.api.PutResponse)8 BeforeTest (org.testng.annotations.BeforeTest)8 Test (org.testng.annotations.Test)8 ByteString (com.google.protobuf.ByteString)5 ExecutionException (java.util.concurrent.ExecutionException)5 DeleteRangeResponse (com.coreos.jetcd.api.DeleteRangeResponse)3 RangeResponse (com.coreos.jetcd.api.RangeResponse)3 LeaseHandler (com.coreos.jetcd.Lease.LeaseHandler)1 LeaseKeepAliveResponse (com.coreos.jetcd.api.LeaseKeepAliveResponse)1 TxnResponse (com.coreos.jetcd.api.TxnResponse)1 Cmp (com.coreos.jetcd.op.Cmp)1 Txn (com.coreos.jetcd.op.Txn)1 GetOption (com.coreos.jetcd.options.GetOption)1 PutOption (com.coreos.jetcd.options.PutOption)1