Search in sources :

Example 16 with Client

use of io.etcd.jetcd.Client in project jetcd by coreos.

the class ClusterClientTest method testMemberManagement.

@Test
public void testMemberManagement() throws ExecutionException, InterruptedException, TimeoutException {
    final Client client = Client.builder().endpoints(n1.clientEndpoints()).build();
    final Cluster clusterClient = client.getClusterClient();
    Member m2 = clusterClient.addMember(n2.peerEndpoints()).get(5, TimeUnit.SECONDS).getMember();
    assertThat(m2).isNotNull();
    assertThat(clusterClient.listMember().get().getMembers()).hasSize(2);
/*
        TODO: check
        Member m3 = clusterClient.addMember(n3.peerEndpoints())
            .get(5, TimeUnit.SECONDS)
            .getMember();
        
        assertThat(m3).isNotNull();
        assertThat(clusterClient.listMember().get().getMembers()).hasSize(3);
        */
}
Also used : Cluster(io.etcd.jetcd.Cluster) Client(io.etcd.jetcd.Client) Member(io.etcd.jetcd.cluster.Member) Test(org.junit.jupiter.api.Test)

Example 17 with Client

use of io.etcd.jetcd.Client 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 18 with Client

use of io.etcd.jetcd.Client 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 19 with Client

use of io.etcd.jetcd.Client in project jetcd by coreos.

the class MaintenanceTest method testMoveLeader.

// @Test
public void testMoveLeader() throws ExecutionException, InterruptedException {
    URI leaderEndpoint = null;
    List<Long> followers = new ArrayList<>();
    for (URI ep : endpoints) {
        StatusResponse statusResponse = maintenance.statusMember(ep).get();
        long memberId = statusResponse.getHeader().getMemberId();
        if (memberId == statusResponse.getLeader()) {
            leaderEndpoint = ep;
            continue;
        }
        followers.add(memberId);
    }
    if (leaderEndpoint == null) {
        fail("leader not found");
    }
    try (Client client = Client.builder().endpoints(leaderEndpoint).build()) {
        client.getMaintenanceClient().moveLeader(followers.get(0)).get();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) StatusResponse(io.etcd.jetcd.maintenance.StatusResponse) Client(io.etcd.jetcd.Client) URI(java.net.URI)

Example 20 with Client

use of io.etcd.jetcd.Client 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)28 Test (org.junit.jupiter.api.Test)20 ByteSequence (io.etcd.jetcd.ByteSequence)13 KV (io.etcd.jetcd.KV)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Watch (io.etcd.jetcd.Watch)5 WatchEvent (io.etcd.jetcd.watch.WatchEvent)5 ByteString (com.google.protobuf.ByteString)4 ClientBuilder (io.etcd.jetcd.ClientBuilder)4 Watcher (io.etcd.jetcd.Watch.Watcher)4 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)4 PutResponse (io.etcd.jetcd.kv.PutResponse)4 URI (java.net.URI)4 TimeUnit (java.util.concurrent.TimeUnit)4 Event (io.etcd.jetcd.api.Event)3 WatchCreateRequest (io.etcd.jetcd.api.WatchCreateRequest)3 EtcdClusterExtension (io.etcd.jetcd.test.EtcdClusterExtension)3 ArrayList (java.util.ArrayList)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 WatchCancelRequest (io.etcd.jetcd.api.WatchCancelRequest)2