Search in sources :

Example 1 with ClientBuilder

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

the class ClientBuilderTest method testNamespace.

@ParameterizedTest
@MethodSource("namespaceProvider")
public void testNamespace(ByteSequence namespaceSetting, ByteSequence expectedNamespace) throws URISyntaxException {
    final ClientBuilder builder = Client.builder().endpoints(new URI("http://127.0.0.1:2379")).namespace(namespaceSetting);
    final ClientConnectionManager connectionManager = new ClientConnectionManager(builder);
    assertThat(connectionManager.getNamespace()).isEqualTo(expectedNamespace);
}
Also used : URI(java.net.URI) ClientBuilder(io.etcd.jetcd.ClientBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ClientBuilder

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

the class ClientConnectionManagerTest method testHeaders.

@Test
public void testHeaders() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientBuilder builder = TestUtil.client(cluster).header("MyHeader1", "MyHeaderVal1").header("MyHeader2", "MyHeaderVal2").interceptor(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

                @Override
                public void start(Listener<RespT> responseListener, Metadata headers) {
                    super.start(responseListener, headers);
                    assertThat(headers.get(Metadata.Key.of("MyHeader1", Metadata.ASCII_STRING_MARSHALLER))).isEqualTo("MyHeaderVal1");
                    assertThat(headers.get(Metadata.Key.of("MyHeader2", Metadata.ASCII_STRING_MARSHALLER))).isEqualTo("MyHeaderVal2");
                    latch.countDown();
                }
            };
        }
    });
    try (Client client = builder.build()) {
        CompletableFuture<PutResponse> future = client.getKVClient().put(bytesOf("sample_key"), bytesOf("sample_key"));
        latch.await(1, TimeUnit.MINUTES);
        future.get();
    }
}
Also used : Channel(io.grpc.Channel) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PutResponse(io.etcd.jetcd.kv.PutResponse) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) Client(io.etcd.jetcd.Client) ClientBuilder(io.etcd.jetcd.ClientBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with ClientBuilder

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

the class ClientConnectionManagerTest method testAuthHeaders.

@Test
public void testAuthHeaders() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    Auth authClient = TestUtil.client(cluster).build().getAuthClient();
    authClient.userAdd(root, rootPass).get();
    ByteSequence role = TestUtil.bytesOf("root");
    authClient.userGrantRole(root, role).get();
    authClient.authEnable().get();
    final ClientBuilder builder = TestUtil.client(cluster).authHeader("MyAuthHeader", "MyAuthHeaderVal").header("MyHeader2", "MyHeaderVal2").user(root).password(rootPass);
    assertThat(builder.authHeaders().get(Metadata.Key.of("MyAuthHeader", Metadata.ASCII_STRING_MARSHALLER))).isEqualTo("MyAuthHeaderVal");
    try (Client client = builder.build()) {
        CompletableFuture<AuthDisableResponse> future = client.getAuthClient().authDisable();
        latch.await(10, TimeUnit.SECONDS);
        future.get();
    }
    authClient.userRevokeRole(root, role).get();
    authClient.userDelete(root).get();
}
Also used : Auth(io.etcd.jetcd.Auth) CountDownLatch(java.util.concurrent.CountDownLatch) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) ClientBuilder(io.etcd.jetcd.ClientBuilder) AuthDisableResponse(io.etcd.jetcd.auth.AuthDisableResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ClientBuilder

use of io.etcd.jetcd.ClientBuilder 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 5 with ClientBuilder

use of io.etcd.jetcd.ClientBuilder 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)

Aggregations

ClientBuilder (io.etcd.jetcd.ClientBuilder)8 Test (org.junit.jupiter.api.Test)6 URI (java.net.URI)5 Client (io.etcd.jetcd.Client)4 PutResponse (io.etcd.jetcd.kv.PutResponse)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 KV (io.etcd.jetcd.KV)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Auth (io.etcd.jetcd.Auth)1 ByteSequence (io.etcd.jetcd.ByteSequence)1 Response (io.etcd.jetcd.Response)1 AuthDisableResponse (io.etcd.jetcd.auth.AuthDisableResponse)1 CallOptions (io.grpc.CallOptions)1 Channel (io.grpc.Channel)1 ClientCall (io.grpc.ClientCall)1 ClientInterceptor (io.grpc.ClientInterceptor)1 ForwardingClientCall (io.grpc.ForwardingClientCall)1 Metadata (io.grpc.Metadata)1 VertxChannelBuilder (io.vertx.grpc.VertxChannelBuilder)1 Random (java.util.Random)1