use of io.etcd.jetcd.Client in project jetcd by coreos.
the class KVTest method testKVClientCanRetryPutOnEtcdRestart.
@Test
@SuppressWarnings("FutureReturnValueIgnored")
public void testKVClientCanRetryPutOnEtcdRestart() throws InterruptedException {
try (Client customClient = TestUtil.client(cluster).retryMaxDuration(Duration.ofMinutes(5)).retryDelay(10).retryMaxDelay(30).retryChronoUnit(ChronoUnit.SECONDS).build()) {
ByteSequence key = ByteSequence.from("retry_dummy_key", StandardCharsets.UTF_8);
int putCount = 1000;
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
// start putting values in a sequence, at least on of them has to be retried
executor.submit(() -> {
for (int i = 0; i < putCount; ++i) {
ByteSequence value = ByteSequence.from(Integer.toString(i), StandardCharsets.UTF_8);
customClient.getKVClient().put(key, value).join();
}
});
// restart the cluster while uploading
executor.schedule(() -> cluster.restart(), 100, TimeUnit.MILLISECONDS);
executor.shutdown();
assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
GetResponse getResponse = kvClient.get(key).join();
assertThat(getResponse.getKvs().size()).as("There should be exactly one KeyValue for the test key").isEqualTo(1);
ByteSequence lastPutValue = ByteSequence.from(Integer.toString(putCount - 1), StandardCharsets.UTF_8);
assertThat(getResponse.getKvs().get(0).getValue()).as("The sequence of put operations should finish successfully. " + "Last seen value should match the expected value.").isEqualTo(lastPutValue);
}
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class AuthUnitTest method testHeaders.
@Test
public void testHeaders() throws Exception {
MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
serviceRegistry.addService(new AuthGrpc.AuthImplBase() {
@Override
public void authenticate(io.etcd.jetcd.api.AuthenticateRequest request, io.grpc.stub.StreamObserver<io.etcd.jetcd.api.AuthenticateResponse> responseObserver) {
responseObserver.onNext(AuthenticateResponse.newBuilder().setToken("token").build());
}
});
serviceRegistry.addService(new KVGrpc.KVImplBase() {
@Override
public void put(io.etcd.jetcd.api.PutRequest request, io.grpc.stub.StreamObserver<io.etcd.jetcd.api.PutResponse> responseObserver) {
responseObserver.onNext(PutResponse.newBuilder().build());
}
});
Server server = null;
Client client = null;
try {
Metadata intercepted = new Metadata();
server = NettyServerBuilder.forPort(0).fallbackHandlerRegistry(serviceRegistry).intercept(new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
if (AUTHENTICATE_METHOD_NAME.equals(call.getMethodDescriptor().getFullMethodName())) {
intercepted.merge(headers);
}
return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<>(call) {
}, headers);
}
}).directExecutor().build().start();
client = Client.builder().endpoints(new URI("http://127.0.0.1:" + server.getPort())).user(user).password(userPass).authHeader("foo-a", "foo-auth").header("bar-h", "bar").build();
client.getKVClient().put(key, value).get(30, TimeUnit.SECONDS);
assertThat(intercepted.keys()).contains("foo-a");
} finally {
if (client != null) {
client.close();
}
if (server != null) {
server.shutdownNow();
}
}
}
use of io.etcd.jetcd.Client 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();
}
}
use of io.etcd.jetcd.Client 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();
}
use of io.etcd.jetcd.Client in project jetcd by coreos.
the class WatchErrorTest method testWatchOnError.
@ParameterizedTest
@ValueSource(strings = { "test-namespace/", "" })
public void testWatchOnError(String ns) {
final Client client = ns != null && ns.length() == 0 ? TestUtil.client(cluster).namespace(bytesOf(ns)).build() : TestUtil.client(cluster).build();
final ByteSequence key = randomByteSequence();
final List<Throwable> events = Collections.synchronizedList(new ArrayList<>());
try (Watcher watcher = client.getWatchClient().watch(key, TestUtil::noOpWatchResponseConsumer, events::add)) {
cluster.cluster().stop();
await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
}
assertThat(events).allMatch(EtcdException.class::isInstance);
}
Aggregations