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));
}
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();
}
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();
}
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);
}
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();
}
}
Aggregations