use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class WatchTest method testWatchFutureRevisionIsNotOverwrittenOnCreation.
@ParameterizedTest
@MethodSource("parameters")
public void testWatchFutureRevisionIsNotOverwrittenOnCreation(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence value = randomByteSequence();
final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());
PutResponse putResponse = client.getKVClient().put(key, value).get();
long lastSeenRevision = putResponse.getHeader().getRevision();
WatchOption watchOption = WatchOption.newBuilder().withRevision(lastSeenRevision + 1).build();
try (Watcher watcher = client.getWatchClient().watch(key, watchOption, events::add)) {
// resumes (recreates) the watch
cluster.restart();
// await().duration() would be better but it's broken
Thread.sleep(2000);
assertThat(events.isEmpty()).as("verify that received events list is empty").isTrue();
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class WatchTest method getCompactedRevision.
private static long getCompactedRevision(final Client client, final ByteSequence key) throws Exception {
final ByteSequence value = randomByteSequence();
// Insert key twice to ensure we have at least two revisions
client.getKVClient().put(key, value).get();
final PutResponse putResponse = client.getKVClient().put(key, value).get();
// Compact until latest revision
client.getKVClient().compact(putResponse.getHeader().getRevision()).get();
return putResponse.getHeader().getRevision() - 1;
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class WatchTest method testNamespacedAndNotNamespacedClient.
@Test
public void testNamespacedAndNotNamespacedClient() throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence nsKey = ByteSequence.from(namespace.concat(key).getBytes());
final Client client = TestUtil.client(cluster).build();
final Client nsClient = TestUtil.client(cluster).namespace(namespace).build();
final ByteSequence value = randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
// From client with namespace watch for key. Since client is namespaced it should watch for namespaced key.
try (Watcher watcher = nsClient.getWatchClient().watch(key, ref::set)) {
// Using non-namespaced client put namespaced key.
client.getKVClient().put(nsKey, value).get();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get()).isNotNull();
assertThat(ref.get().getEvents().size()).isEqualTo(1);
assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
}
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class OptionsUtilTest method check.
static void check(byte[] prefix, byte[] expectedPrefixEndOf) {
ByteSequence actual = OptionsUtil.prefixEndOf(ByteSequence.from(prefix));
assertThat(actual).isEqualTo(ByteSequence.from(expectedPrefixEndOf));
}
use of io.etcd.jetcd.ByteSequence in project jetcd by coreos.
the class KVNamespaceTest method testTxn.
@Test
public void testTxn() 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();
// put a key in root namespace, assert that it cannot be seen using kvClient with namespace
ByteSequence cmpKey = getNonexistentKey();
putKVWithAssertion(kvClient, cmpKey, 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);
// test comparison passes, with put operation.
{
Txn txn = kvClientWithNamespace.txn();
CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(cmpKey, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.put(key1, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).Else(Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).commit();
TxnResponse txnResponse = txnFuture.get();
assertThat(txnResponse.getPutResponses().size()).isEqualTo(1);
assertThat(txnResponse.getPutResponses().get(0).hasPrevKv()).isTrue();
assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getKey()).isEqualTo(key1);
assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getValue()).isEqualTo(value1);
}
// test comparison fails, with get operation.
{
Txn txn = kvClientWithNamespace.txn();
CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.get(key1, GetOption.newBuilder().build())).Else(Op.get(key2, GetOption.newBuilder().build())).commit();
TxnResponse txnResponse = txnFuture.get();
assertThat(txnResponse.getGetResponses().size()).isEqualTo(1);
assertThat(txnResponse.getGetResponses().get(0).getKvs().size()).isEqualTo(1);
assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getKey()).isEqualTo(key2);
assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getValue()).isEqualTo(value2);
}
// test delete operation
{
Txn txn = kvClientWithNamespace.txn();
CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.GREATER, CmpTarget.version(0))).Then(Op.delete(key2, DeleteOption.newBuilder().withPrevKV(true).build())).Else(Op.delete(key1, DeleteOption.newBuilder().withPrevKV(true).build())).commit();
TxnResponse txnResponse = txnFuture.get();
assertThat(txnResponse.getDeleteResponses().size()).isEqualTo(1);
assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().size()).isEqualTo(1);
assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getKey()).isEqualTo(key2);
assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getValue()).isEqualTo(value2);
}
}
Aggregations