use of io.etcd.jetcd.KV in project jetcd by coreos.
the class WatchResumeTest method testWatchOnPut.
@Test
public void testWatchOnPut() throws Exception {
try (Client client = TestUtil.client(cluster).build()) {
Watch watchClient = client.getWatchClient();
KV kvClient = client.getKVClient();
final ByteSequence key = TestUtil.randomByteSequence();
final ByteSequence value = TestUtil.randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
try (Watcher watcher = watchClient.watch(key, ref::set)) {
cluster.restart();
kvClient.put(key, value).get(1, TimeUnit.SECONDS);
await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> 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.KV in project jetcd by coreos.
the class KVTest method waitForReadySemantics.
@Test()
public void waitForReadySemantics() throws ExecutionException, InterruptedException, TimeoutException {
String nonExistingServer = "http://127.0.0.1:9999";
try (Client customClient = Client.builder().endpoints(nonExistingServer).waitForReady(false).retryMaxDuration(Duration.ofSeconds(3)).retryDelay(1).retryMaxDelay(2).retryChronoUnit(ChronoUnit.SECONDS).connectTimeout(Duration.ofSeconds(1)).build()) {
KV kvClient = customClient.getKVClient();
CompletableFuture<String> future = kvClient.get(ByteSequence.from("/x", StandardCharsets.UTF_8)).thenApply(response -> "we got a response").exceptionally(throwable -> "completed exceptionally");
assertThat(future.get(5, TimeUnit.SECONDS)).isEqualTo("completed exceptionally");
}
}
use of io.etcd.jetcd.KV in project jetcd by coreos.
the class LoadBalancerTest method testPickFirstBalancerFactory.
@Test
public void testPickFirstBalancerFactory() throws Exception {
final List<URI> endpoints = cluster.clientEndpoints();
final ClientBuilder builder = Client.builder().endpoints(endpoints).loadBalancerPolicy("pick_first");
try (Client client = builder.build();
KV kv = client.getKVClient()) {
long lastMemberId = 0;
final String allEndpoints = endpoints.stream().map(URI::toString).collect(Collectors.joining(","));
for (int i = 0; i < allEndpoints.length() * 2; i++) {
Response response = kv.put(TestUtil.randomByteSequence(), TestUtil.randomByteSequence()).get();
if (i == 0) {
lastMemberId = response.getHeader().getMemberId();
}
assertThat(response.getHeader().getMemberId()).isEqualTo(lastMemberId);
}
}
}
use of io.etcd.jetcd.KV in project jetcd by coreos.
the class LoadBalancerTest method testRoundRobinLoadBalancerFactory.
@Test
public void testRoundRobinLoadBalancerFactory() throws Exception {
final List<URI> endpoints = cluster.clientEndpoints();
final ClientBuilder builder = Client.builder().endpoints(endpoints).loadBalancerPolicy("round_robin");
try (Client client = builder.build();
KV kv = client.getKVClient()) {
long lastMemberId = 0;
long differences = 0;
final String allEndpoints = endpoints.stream().map(URI::toString).collect(Collectors.joining(","));
for (int i = 0; i < allEndpoints.length(); i++) {
PutResponse response = kv.put(TestUtil.randomByteSequence(), TestUtil.randomByteSequence()).get();
if (i > 0 && lastMemberId != response.getHeader().getMemberId()) {
differences++;
}
lastMemberId = response.getHeader().getMemberId();
}
assertThat(differences).isNotEqualTo(lastMemberId);
}
}
use of io.etcd.jetcd.KV in project jetcd by coreos.
the class SslTest method testSimpleSllSetup.
@Test
public void testSimpleSllSetup() throws Exception {
final ByteSequence key = bytesOf(TestUtil.randomString());
final ByteSequence val = bytesOf(TestUtil.randomString());
final String capath = System.getProperty("ssl.cert.capath");
final String authority = System.getProperty("ssl.cert.authority", DEFAULT_SSL_AUTHORITY);
final URI endpoint = new URI(System.getProperty("ssl.cert.endpoints", cluster.clientEndpoints().get(0).toString()));
try (InputStream is = Objects.nonNull(capath) ? new FileInputStream(capath) : getClass().getResourceAsStream(DEFAULT_SSL_CA_PATH)) {
Client client = Client.builder().endpoints(endpoint).authority(authority).sslContext(b -> b.trustManager(is)).build();
KV kv = client.getKVClient();
kv.put(key, val).join();
assertThat(kv.get(key).join().getCount()).isEqualTo(1);
assertThat(kv.get(key).join().getKvs().get(0).getValue()).isEqualTo(val);
kv.close();
client.close();
}
}
Aggregations