Search in sources :

Example 11 with NotifyListener

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

the class AbstractRegistryTest method testUnsubscribeIfUrlNull.

@Test
public void testUnsubscribeIfUrlNull() throws Exception {
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
        NotifyListener listener = urls -> notified.set(Boolean.TRUE);
        abstractRegistry.unsubscribe(null, listener);
        Assertions.fail("unsubscribe url == null");
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers(org.hamcrest.Matchers) Set(java.util.Set) NotifyListener(org.apache.dubbo.registry.NotifyListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) URL(org.apache.dubbo.common.URL) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) EMPTY_PROTOCOL(org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) AtomicReference(java.util.concurrent.atomic.AtomicReference) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 12 with NotifyListener

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

the class AbstractRegistryTest method testNotify.

/**
 * Test method for
 * {@link org.apache.dubbo.registry.support.AbstractRegistry#notify(List)}.
 */
@Test
public void testNotify() throws Exception {
    final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
    NotifyListener listener1 = urls -> notified.set(Boolean.TRUE);
    URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
    abstractRegistry.subscribe(url1, listener1);
    NotifyListener listener2 = urls -> notified.set(Boolean.TRUE);
    URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
    abstractRegistry.subscribe(url2, listener2);
    NotifyListener listener3 = urls -> notified.set(Boolean.TRUE);
    URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
    abstractRegistry.subscribe(url3, listener3);
    List<URL> urls = new ArrayList<>();
    urls.add(url1);
    urls.add(url2);
    urls.add(url3);
    abstractRegistry.notify(url1, listener1, urls);
    Map<URL, Map<String, List<URL>>> map = abstractRegistry.getNotified();
    MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url1)));
    MatcherAssert.assertThat(false, Matchers.equalTo(map.containsKey(url2)));
    MatcherAssert.assertThat(false, Matchers.equalTo(map.containsKey(url3)));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers(org.hamcrest.Matchers) Set(java.util.Set) NotifyListener(org.apache.dubbo.registry.NotifyListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) URL(org.apache.dubbo.common.URL) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) EMPTY_PROTOCOL(org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 13 with NotifyListener

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

the class AbstractRegistryTest method testNotifyList.

/**
 * test notifyList
 */
@Test
public void testNotifyList() throws Exception {
    final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
    NotifyListener listener1 = urls -> notified.set(Boolean.TRUE);
    URL url1 = new URL("dubbo", "192.168.0.1", 2200, parametersConsumer);
    abstractRegistry.subscribe(url1, listener1);
    NotifyListener listener2 = urls -> notified.set(Boolean.TRUE);
    URL url2 = new URL("dubbo", "192.168.0.2", 2201, parametersConsumer);
    abstractRegistry.subscribe(url2, listener2);
    NotifyListener listener3 = urls -> notified.set(Boolean.TRUE);
    URL url3 = new URL("dubbo", "192.168.0.3", 2202, parametersConsumer);
    abstractRegistry.subscribe(url3, listener3);
    List<URL> urls = new ArrayList<>();
    urls.add(url1);
    urls.add(url2);
    urls.add(url3);
    abstractRegistry.notify(urls);
    Map<URL, Map<String, List<URL>>> map = abstractRegistry.getNotified();
    MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url1)));
    MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url2)));
    MatcherAssert.assertThat(true, Matchers.equalTo(map.containsKey(url3)));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers(org.hamcrest.Matchers) Set(java.util.Set) NotifyListener(org.apache.dubbo.registry.NotifyListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) URL(org.apache.dubbo.common.URL) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) EMPTY_PROTOCOL(org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 14 with NotifyListener

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

the class MulticastRegistry method unregistered.

protected void unregistered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.get(key);
            if (urls != null) {
                urls.remove(url);
            }
            if (CollectionUtils.isEmpty(urls)) {
                if (urls == null) {
                    urls = new ConcurrentHashSet<URL>();
                }
                URL empty = url.setProtocol(EMPTY_PROTOCOL);
                urls.add(empty);
            }
            List<URL> list = toList(urls);
            for (NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 15 with NotifyListener

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

the class MulticastRegistry method registered.

protected void registered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.computeIfAbsent(key, k -> new ConcurrentHashSet<>());
            urls.add(url);
            List<URL> list = toList(urls);
            for (final NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
                synchronized (listener) {
                    listener.notify();
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

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