Search in sources :

Example 1 with KV

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);
        }
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 2 with KV

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");
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) AssertionsForClassTypes.assertThatExceptionOfType(org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType) Cmp(io.etcd.jetcd.op.Cmp) Client(io.etcd.jetcd.Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) EtcdClusterExtension(io.etcd.jetcd.test.EtcdClusterExtension) UTF_8(com.google.common.base.Charsets.UTF_8) TxnResponse(io.etcd.jetcd.kv.TxnResponse) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ByteSequence(io.etcd.jetcd.ByteSequence) Duration(java.time.Duration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) GetResponse(io.etcd.jetcd.kv.GetResponse) GetOption(io.etcd.jetcd.options.GetOption) KV(io.etcd.jetcd.KV) Op(io.etcd.jetcd.op.Op) PutOption(io.etcd.jetcd.options.PutOption) SortOrder(io.etcd.jetcd.options.GetOption.SortOrder) CmpTarget(io.etcd.jetcd.op.CmpTarget) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) SortTarget(io.etcd.jetcd.options.GetOption.SortTarget) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestUtil.bytesOf(io.etcd.jetcd.impl.TestUtil.bytesOf) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Txn(io.etcd.jetcd.Txn) Timeout(org.junit.jupiter.api.Timeout) PutResponse(io.etcd.jetcd.kv.PutResponse) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) Test(org.junit.jupiter.api.Test)

Example 3 with KV

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);
        }
    }
}
Also used : Response(io.etcd.jetcd.Response) PutResponse(io.etcd.jetcd.kv.PutResponse) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) URI(java.net.URI) ClientBuilder(io.etcd.jetcd.ClientBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with KV

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);
    }
}
Also used : KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) PutResponse(io.etcd.jetcd.kv.PutResponse) URI(java.net.URI) ClientBuilder(io.etcd.jetcd.ClientBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with KV

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();
    }
}
Also used : KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EtcdClusterExtension(io.etcd.jetcd.test.EtcdClusterExtension) FileInputStream(java.io.FileInputStream) TestUtil.bytesOf(io.etcd.jetcd.impl.TestUtil.bytesOf) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ByteSequence(io.etcd.jetcd.ByteSequence) URI(java.net.URI) Timeout(org.junit.jupiter.api.Timeout) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) URI(java.net.URI) ByteSequence(io.etcd.jetcd.ByteSequence) FileInputStream(java.io.FileInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Client (io.etcd.jetcd.Client)7 KV (io.etcd.jetcd.KV)7 Test (org.junit.jupiter.api.Test)7 ByteSequence (io.etcd.jetcd.ByteSequence)3 PutResponse (io.etcd.jetcd.kv.PutResponse)3 URI (java.net.URI)3 ClientBuilder (io.etcd.jetcd.ClientBuilder)2 Watch (io.etcd.jetcd.Watch)2 TestUtil.bytesOf (io.etcd.jetcd.impl.TestUtil.bytesOf)2 EtcdClusterExtension (io.etcd.jetcd.test.EtcdClusterExtension)2 TimeUnit (java.util.concurrent.TimeUnit)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Timeout (org.junit.jupiter.api.Timeout)2 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)2 UTF_8 (com.google.common.base.Charsets.UTF_8)1 Response (io.etcd.jetcd.Response)1 Txn (io.etcd.jetcd.Txn)1 Watcher (io.etcd.jetcd.Watch.Watcher)1 AuthRoleGetResponse (io.etcd.jetcd.auth.AuthRoleGetResponse)1 AuthRoleListResponse (io.etcd.jetcd.auth.AuthRoleListResponse)1