Search in sources :

Example 6 with ChildListener

use of org.apache.dubbo.remoting.etcd.ChildListener in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_when_delete_path.

@Test
public void test_watch_when_delete_path() throws InterruptedException {
    String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
    String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers/demoService1";
    final CountDownLatch notNotified = new CountDownLatch(1);
    ChildListener childListener = (parent, children) -> {
        Assertions.assertEquals(0, children.size());
        notNotified.countDown();
    };
    client.createEphemeral(child);
    client.addChildListener(path, childListener);
    client.delete(child);
    Assertions.assertTrue(notNotified.await(10, 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)

Example 7 with ChildListener

use of org.apache.dubbo.remoting.etcd.ChildListener in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_then_unwatch.

@Test
public void test_watch_then_unwatch() throws InterruptedException {
    String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
    String child = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers/demoService2";
    final CountDownLatch notNotified = new CountDownLatch(1);
    final CountDownLatch notTwiceNotified = new CountDownLatch(2);
    final Holder notified = new Holder();
    ChildListener childListener = (parent, children) -> {
        Assertions.assertEquals(1, children.size());
        Assertions.assertEquals(child.substring(child.lastIndexOf("/") + 1), children.get(0));
        notNotified.countDown();
        notTwiceNotified.countDown();
        notified.getAndIncrease();
    };
    client.addChildListener(path, childListener);
    client.createEphemeral(child);
    Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
    client.removeChildListener(path, childListener);
    client.delete(child);
    Assertions.assertFalse(notTwiceNotified.await(5, TimeUnit.SECONDS));
    Assertions.assertEquals(1, notified.value);
    client.delete(child);
}
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)

Example 8 with ChildListener

use of org.apache.dubbo.remoting.etcd.ChildListener in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_on_unrecoverable_connection.

@Test
public void test_watch_on_unrecoverable_connection() throws InterruptedException {
    String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
    JEtcdClient.EtcdWatcher watcher = null;
    try {
        ChildListener childListener = (parent, children) -> {
            Assertions.assertEquals(path, parent);
        };
        client.addChildListener(path, childListener);
        watcher = client.getChildListener(path, childListener);
        watcher.watchRequest.onError(Status.ABORTED.withDescription("connection error").asRuntimeException());
        watcher.watchRequest.onNext(watcher.nextRequest());
    } catch (Exception e) {
        Assertions.assertTrue(e.getMessage().contains("call was cancelled"));
    }
}
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) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) Test(org.junit.jupiter.api.Test)

Example 9 with ChildListener

use of org.apache.dubbo.remoting.etcd.ChildListener in project dubbo by alibaba.

the class EtcdRegistry method doUnsubscribe.

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
    ConcurrentMap<NotifyListener, ChildListener> listeners = etcdListeners.get(url);
    if (listeners != null) {
        ChildListener etcdListener = listeners.remove(listener);
        if (etcdListener != null) {
            if (ANY_VALUE.equals(url.getServiceInterface())) {
                String root = toRootPath();
                etcdClient.removeChildListener(root, etcdListener);
            } else {
                // maybe url has many subscribed paths
                for (String path : toUnsubscribedPath(url)) {
                    etcdClient.removeChildListener(path, etcdListener);
                }
            }
        }
        if (listeners.isEmpty()) {
            etcdListeners.remove(url);
        }
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.etcd.ChildListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Aggregations

ChildListener (org.apache.dubbo.remoting.etcd.ChildListener)9 URL (org.apache.dubbo.common.URL)7 ByteString (com.google.protobuf.ByteString)6 ByteSequence (io.etcd.jetcd.ByteSequence)6 Client (io.etcd.jetcd.Client)6 Watch (io.etcd.jetcd.Watch)6 Event (io.etcd.jetcd.api.Event)6 WatchCancelRequest (io.etcd.jetcd.api.WatchCancelRequest)6 WatchCreateRequest (io.etcd.jetcd.api.WatchCreateRequest)6 WatchGrpc (io.etcd.jetcd.api.WatchGrpc)6 WatchRequest (io.etcd.jetcd.api.WatchRequest)6 WatchResponse (io.etcd.jetcd.api.WatchResponse)6 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)6 WatchEvent (io.etcd.jetcd.watch.WatchEvent)6 ManagedChannel (io.grpc.ManagedChannel)6 Status (io.grpc.Status)6 StreamObserver (io.grpc.stub.StreamObserver)6 Field (java.lang.reflect.Field)6 Method (java.lang.reflect.Method)6 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)6