Search in sources :

Example 6 with Client

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

Example 7 with Client

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();
}
Also used : Client(io.etcd.jetcd.Client) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with Client

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);
        }
    }
}
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 9 with Client

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();
}
Also used : KV(io.etcd.jetcd.KV) Arrays(java.util.Arrays) Awaitility.await(org.awaitility.Awaitility.await) Client(io.etcd.jetcd.Client) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EtcdClusterExtension(io.etcd.jetcd.test.EtcdClusterExtension) Watch(io.etcd.jetcd.Watch) File(java.io.File) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ByteSequence(io.etcd.jetcd.ByteSequence) Permission(io.etcd.jetcd.auth.Permission) Timeout(org.junit.jupiter.api.Timeout) ExecutorService(java.util.concurrent.ExecutorService) Client(io.etcd.jetcd.Client) File(java.io.File) ByteSequence(io.etcd.jetcd.ByteSequence)

Example 10 with Client

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);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) ManagedChannel(io.grpc.ManagedChannel) Watch(io.etcd.jetcd.Watch) Disabled(org.junit.jupiter.api.Disabled) WatchRequest(io.etcd.jetcd.api.WatchRequest) URL(org.apache.dubbo.common.URL) StreamObserver(io.grpc.stub.StreamObserver) SESSION_TIMEOUT_KEY(org.apache.dubbo.remoting.etcd.Constants.SESSION_TIMEOUT_KEY) ByteSequence(io.etcd.jetcd.ByteSequence) ReflectUtils(org.apache.dubbo.common.utils.ReflectUtils) ChildListener(org.apache.dubbo.remoting.etcd.ChildListener) Status(io.grpc.Status) Method(java.lang.reflect.Method) WatchResponse(io.etcd.jetcd.api.WatchResponse) WatchCancelRequest(io.etcd.jetcd.api.WatchCancelRequest) UTF_8(java.nio.charset.StandardCharsets.UTF_8) WatchGrpc(io.etcd.jetcd.api.WatchGrpc) WatchCreateRequest(io.etcd.jetcd.api.WatchCreateRequest) Field(java.lang.reflect.Field) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) AfterEach(org.junit.jupiter.api.AfterEach) Event(io.etcd.jetcd.api.Event) Assertions(org.junit.jupiter.api.Assertions) ChildListener(org.apache.dubbo.remoting.etcd.ChildListener) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) 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