use of io.etcd.jetcd.Client in project jetcd by coreos.
the class WatchTest method testNamespacedAndNotNamespacedClient.
@Test
public void testNamespacedAndNotNamespacedClient() throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence nsKey = ByteSequence.from(namespace.concat(key).getBytes());
final Client client = TestUtil.client(cluster).build();
final Client nsClient = TestUtil.client(cluster).namespace(namespace).build();
final ByteSequence value = randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
// From client with namespace watch for key. Since client is namespaced it should watch for namespaced key.
try (Watcher watcher = nsClient.getWatchClient().watch(key, ref::set)) {
// Using non-namespaced client put namespaced key.
client.getKVClient().put(nsKey, value).get();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
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.Client in project jetcd by coreos.
the class LockTest method setUp.
@BeforeAll
public static void setUp() {
Client client = TestUtil.client(cluster).build();
leaseClient = client.getLeaseClient();
}
use of io.etcd.jetcd.Client 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.Client in project jetcd by coreos.
the class WatchTokenExpireTest method setUpEnvironment.
private void setUpEnvironment() throws Exception {
final File caFile = new File(Objects.requireNonNull(getClass().getResource("/ssl/cert/ca.pem")).toURI());
Client client = TestUtil.client(cluster).authority("etcd0").sslContext(b -> b.trustManager(caFile)).build();
// enable authentication to enforce usage of access token
ByteSequence role = TestUtil.bytesOf("root");
client.getAuthClient().roleAdd(role).get();
client.getAuthClient().userAdd(user, password).get();
// grant access only to given key
client.getAuthClient().roleGrantPermission(role, key, key, Permission.Type.READWRITE).get();
client.getAuthClient().userGrantRole(user, role).get();
client.getAuthClient().authEnable().get();
client.close();
}
use of io.etcd.jetcd.Client in project dubbo by alibaba.
the class JEtcdClientTest method test_watch_on_recoverable_connection.
@Test
public void test_watch_on_recoverable_connection() throws InterruptedException {
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/connection";
String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/connection/demoService1";
final CountDownLatch notNotified = new CountDownLatch(1);
final CountDownLatch notTwiceNotified = new CountDownLatch(2);
final Holder notified = new Holder();
ChildListener childListener = (parent, children) -> {
notTwiceNotified.countDown();
switch(notified.increaseAndGet()) {
case 1:
{
notNotified.countDown();
Assertions.assertEquals(1, children.size());
Assertions.assertEquals(child.substring(child.lastIndexOf("/") + 1), children.get(0));
break;
}
case 2:
{
Assertions.assertEquals(0, children.size());
Assertions.assertEquals(path, parent);
break;
}
default:
Assertions.fail("two many callback invoked.");
}
};
client.addChildListener(path, childListener);
client.createEphemeral(child);
// make sure first time callback successfully
Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
// connection error causes client to release all resources including current watcher
JEtcdClient.EtcdWatcher watcher = client.getChildListener(path, childListener);
watcher.onError(Status.UNAVAILABLE.withDescription("temporary connection issue").asRuntimeException());
// trigger delete after unavailable
client.delete(child);
Assertions.assertTrue(notTwiceNotified.await(15, TimeUnit.SECONDS));
client.removeChildListener(path, childListener);
}
Aggregations