Skip to content

Commit

Permalink
Add watch and get test
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Burgazzoli <lburgazzoli@gmail.com>
  • Loading branch information
lburgazzoli committed Oct 11, 2022
1 parent f103693 commit cadde88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ logs/
.sdkmanrc
build/
bin/
out/
34 changes: 34 additions & 0 deletions jetcd-core/src/test/java/io/etcd/jetcd/impl/WatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +35,7 @@

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.Watch;
import io.etcd.jetcd.Watch.Watcher;
import io.etcd.jetcd.common.exception.CompactedException;
Expand Down Expand Up @@ -266,6 +268,38 @@ public void testWatchFutureRevisionIsNotOverwrittenOnCreation(final Client clien
}
}

@ParameterizedTest
@MethodSource("parameters")
public void testWatchAndGet(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence value = randomByteSequence();
final AtomicReference<KeyValue> ref = new AtomicReference<>();

final Consumer<WatchResponse> consumer = response -> {
for (WatchEvent event : response.getEvents()) {
if (event.getEventType() == EventType.PUT) {
ByteSequence key1 = event.getKeyValue().getKey();

client.getKVClient().get(key1).whenComplete((r, t) -> {
if (!r.getKvs().isEmpty()) {
ref.set(r.getKvs().get(0));
}
});
}
}
};

try (Watcher watcher = client.getWatchClient().watch(key, consumer)) {
client.getKVClient().put(key, value).get();

await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());

assertThat(ref.get()).isNotNull();
assertThat(ref.get().getKey()).isEqualTo(key);
assertThat(ref.get().getValue()).isEqualTo(value);
}
}

private static long getCompactedRevision(final Client client, final ByteSequence key) throws Exception {
final ByteSequence value = randomByteSequence();

Expand Down

0 comments on commit cadde88

Please sign in to comment.