use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testGetWithRev.
@Test
public void testGetWithRev() throws Exception {
CompletableFuture<PutResponse> feature = kvClient.put(SAMPLE_KEY_3, SAMPLE_VALUE);
PutResponse putResp = feature.get();
kvClient.put(SAMPLE_KEY_3, SAMPLE_VALUE_2).get();
GetOption option = GetOption.newBuilder().withRevision(putResp.getHeader().getRevision()).build();
CompletableFuture<GetResponse> getFeature = kvClient.get(SAMPLE_KEY_3, option);
GetResponse response = getFeature.get();
assertThat(response.getKvs()).hasSize(1);
assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE.toString(UTF_8));
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testKVClientCanRetryPutOnEtcdRestart.
@Test
@SuppressWarnings("FutureReturnValueIgnored")
public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
try (Client customClient = TestUtil.client(cluster).retryMaxDuration(Duration.ofMinutes(5)).retryDelay(10).retryMaxDelay(30).retryChronoUnit(ChronoUnit.SECONDS).build()) {
ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.UTF_8);
int putCount = 1000;
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
// start putting values in a sequence, at least on of them has to be retried
executor.submit(() -> {
for (int i = 0; i < putCount; ++i) {
ByteSequence value = ByteSequence.from(Integer.toString(i), StandardCharsets.UTF_8);
customClient.getKVClient().put(key, value).join();
}
});
// restart the cluster while uploading
executor.schedule(() -> cluster.restart(), 100, TimeUnit.MILLISECONDS);
executor.shutdown();
assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
GetResponse getResponse = kvClient.get(key).join();
assertThat(getResponse.getKvs().size()).as("There should be exactly one KeyValue for the test key").isEqualTo(1);
ByteSequence lastPutValue = ByteSequence.from(Integer.toString(putCount - 1), StandardCharsets.UTF_8);
assertThat(getResponse.getKvs().get(0).getValue()).as("The sequence of put operations should finish successfully. " + "Last seen value should match the expected value.").isEqualTo(lastPutValue);
}
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testDelete.
@Test
public void testDelete() throws Exception {
// Put content so that we actually have something to delete
testPut();
ByteSequence keyToDelete = SAMPLE_KEY;
// count keys about to delete
CompletableFuture<GetResponse> getFeature = kvClient.get(keyToDelete);
GetResponse resp = getFeature.get();
// delete the keys
CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(keyToDelete);
DeleteResponse delResp = deleteFuture.get();
assertThat(delResp.getDeleted()).isEqualTo(resp.getKvs().size());
}
use of io.etcd.jetcd.kv.GetResponse 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));
}
use of io.etcd.jetcd.kv.GetResponse 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();
}
Aggregations