Search in sources :

Example 46 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class RegistryProtocolTest method testNotifyOverride_notmatch.

// @Test
// public void testNotifyOverride() throws Exception {
// URL newRegistryUrl = registryUrl.addParameter(EXPORT_KEY, serviceUrl);
// Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl);
// 
// ServiceDescriptor descriptor = ApplicationModel.getServiceRepository().registerService(DemoService.class);
// ApplicationModel.getServiceRepository().registerProvider(service, new DemoServiceImpl(), descriptor, null, null);
// 
// Exporter<?> exporter = protocol.export(invoker);
// RegistryProtocol rprotocol = getRegistryProtocol();
// NotifyListener listener = getListener(rprotocol);
// List<URL> urls = new ArrayList<URL>();
// urls.add(URL.valueOf("override://0.0.0.0/?timeout=1000"));
// urls.add(URL.valueOf("override://0.0.0.0/" + service + "?timeout=100"));
// urls.add(URL.valueOf("override://0.0.0.0/" + service + "?x=y"));
// listener.notify(urls);
// 
// assertTrue(exporter.getInvoker().isAvailable());
// assertEquals("100", exporter.getInvoker().getUrl().getParameter("timeout"));
// assertEquals("y", exporter.getInvoker().getUrl().getParameter("x"));
// 
// exporter.unexport();
// //        int timeout = ConfigUtils.getServerShutdownTimeout();
// //        Thread.sleep(timeout + 1000);
// //        assertEquals(false, exporter.getInvoker().isAvailable());
// destroyRegistryProtocol();
// 
// }
/**
 * The name of the service does not match and can't override invoker
 * Service name matching, service version number mismatch
 */
@Test
public void testNotifyOverride_notmatch() throws Exception {
    URL newRegistryUrl = registryUrl.addParameter(EXPORT_KEY, serviceUrl);
    Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl);
    ServiceDescriptor descriptor = ApplicationModel.getServiceRepository().registerService(DemoService.class);
    ApplicationModel.getServiceRepository().registerProvider(service, new DemoServiceImpl(), descriptor, null, null);
    Exporter<?> exporter = protocol.export(invoker);
    RegistryProtocol rprotocol = getRegistryProtocol();
    NotifyListener listener = getListener(rprotocol);
    List<URL> urls = new ArrayList<URL>();
    urls.add(URL.valueOf("override://0.0.0.0/org.apache.dubbo.registry.protocol.HackService?timeout=100"));
    listener.notify(urls);
    assertTrue(exporter.getInvoker().isAvailable());
    assertNull(exporter.getInvoker().getUrl().getParameter("timeout"));
    exporter.unexport();
    destroyRegistryProtocol();
}
Also used : ServiceDescriptor(org.apache.dubbo.rpc.model.ServiceDescriptor) RegistryProtocol(org.apache.dubbo.registry.integration.RegistryProtocol) ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 47 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener 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)

Example 48 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class EtcdRegistryTest method test_subscribe_when_register0.

@Test
public void test_subscribe_when_register0() throws InterruptedException {
    Assertions.assertEquals(0, registry.getRegistered().size());
    Assertions.assertEquals(0, registry.getSubscribed().size());
    CountDownLatch notNotified = new CountDownLatch(3);
    ConcurrentHashMap<URL, Boolean> notifiedUrls = new ConcurrentHashMap<>();
    registry.subscribe(consumerUrl, new NotifyListener() {

        public void notify(List<URL> urls) {
            if (urls != null && urls.size() > 0) {
                if (!urls.get(0).getProtocol().equals("empty")) {
                    for (Iterator<URL> iterator = urls.iterator(); iterator.hasNext(); ) {
                        notifiedUrls.put(iterator.next(), true);
                    }
                }
            }
            notNotified.countDown();
        }
    });
    registry.register(serviceUrl);
    registry.register(serviceUrl2);
    Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
    Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl));
    Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl2));
    Map<URL, Set<NotifyListener>> subscribed = registry.getSubscribed();
    Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next());
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 49 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class EtcdRegistryTest method test_subscribe_when_register1.

@Test
public void test_subscribe_when_register1() throws InterruptedException {
    Assertions.assertEquals(0, registry.getRegistered().size());
    Assertions.assertEquals(0, registry.getSubscribed().size());
    CountDownLatch notNotified = new CountDownLatch(2);
    final AtomicReference<URL> notifiedUrls = new AtomicReference<URL>();
    registry.subscribe(consumerUrl, new NotifyListener() {

        public void notify(List<URL> urls) {
            notifiedUrls.set(urls.get(0));
            notNotified.countDown();
        }
    });
    registry.register(serviceUrl);
    // register service3 should not trigger notify
    registry.register(serviceUrl3);
    Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
    Assertions.assertEquals(serviceUrl, notifiedUrls.get());
    Map<URL, Set<NotifyListener>> subscribed = registry.getSubscribed();
    Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next());
}
Also used : Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 50 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class EtcdRegistryTest method test_subscribe_when_register2.

@Test
public void test_subscribe_when_register2() throws InterruptedException {
    Assertions.assertEquals(0, registry.getRegistered().size());
    Assertions.assertEquals(0, registry.getSubscribed().size());
    CountDownLatch notNotified = new CountDownLatch(3);
    ConcurrentHashMap<URL, Boolean> notifiedUrls = new ConcurrentHashMap<>();
    registry.subscribe(subscribe, new NotifyListener() {

        public void notify(List<URL> urls) {
            if (urls != null && urls.size() > 0) {
                if (!urls.get(0).getProtocol().equals("empty")) {
                    for (Iterator<URL> iterator = urls.iterator(); iterator.hasNext(); ) {
                        notifiedUrls.put(iterator.next(), true);
                    }
                    notNotified.countDown();
                }
            }
        }
    });
    registry.register(serviceUrl);
    registry.register(serviceUrl2);
    // service3 interface is not equals server2
    registry.register(serviceUrl3);
    Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
    Assertions.assertEquals(3, notifiedUrls.size());
    Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl));
    Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl2));
    Assertions.assertTrue(notifiedUrls.containsKey(serviceUrl3));
}
Also used : Iterator(java.util.Iterator) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Aggregations

NotifyListener (org.apache.dubbo.registry.NotifyListener)51 URL (org.apache.dubbo.common.URL)43 Test (org.junit.jupiter.api.Test)29 Set (java.util.Set)28 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 List (java.util.List)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)11 HashSet (java.util.HashSet)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 EMPTY_PROTOCOL (org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL)9 LinkedHashMap (java.util.LinkedHashMap)8 Objects (java.util.Objects)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 HashMap (java.util.HashMap)7 MatcherAssert (org.hamcrest.MatcherAssert)7 Matchers (org.hamcrest.Matchers)7 AfterEach (org.junit.jupiter.api.AfterEach)7