Search in sources :

Example 6 with NotifyListener

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

the class AbstractRegistryTest method testNotifyIfURLNull.

@Test
public void testNotifyIfURLNull() throws Exception {
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        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(null, listener1, urls);
        Assertions.fail("notify 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) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 7 with NotifyListener

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

the class AbstractRegistryTest method testSubscribeIfUrlNull.

@Test
public void testSubscribeIfUrlNull() throws Exception {
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
        NotifyListener listener = urls -> notified.set(Boolean.TRUE);
        URL url = new URL("dubbo", "192.168.0.1", 2200);
        abstractRegistry.subscribe(null, listener);
        Assertions.fail("subscribe 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) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 8 with NotifyListener

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

the class FailbackRegistry method recover.

@Override
protected void recover() throws Exception {
    // register
    Set<URL> recoverRegistered = new HashSet<URL>(getRegistered());
    if (!recoverRegistered.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover register url " + recoverRegistered);
        }
        for (URL url : recoverRegistered) {
            // remove fail registry or unRegistry task first.
            removeFailedRegistered(url);
            removeFailedUnregistered(url);
            addFailedRegistered(url);
        }
    }
    // subscribe
    Map<URL, Set<NotifyListener>> recoverSubscribed = new HashMap<URL, Set<NotifyListener>>(getSubscribed());
    if (!recoverSubscribed.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover subscribe url " + recoverSubscribed.keySet());
        }
        for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                // First remove other tasks to ensure that addFailedSubscribed can succeed.
                removeFailedSubscribed(url, listener);
                addFailedSubscribed(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) URL(org.apache.dubbo.common.URL) HashSet(java.util.HashSet) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 9 with NotifyListener

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

the class DubboRegistryTest method setUp.

@BeforeEach
public void setUp() {
    registryURL = new URL(REGISTRY_PROTOCOL, NetUtils.getLocalHost(), NetUtils.getAvailablePort()).addParameter(Constants.CHECK_KEY, false).setServiceInterface(RegistryService.class.getName());
    serviceURL = new URL(DubboProtocol.NAME, NetUtils.getLocalHost(), NetUtils.getAvailablePort()).addParameter(Constants.CHECK_KEY, false).setServiceInterface(RegistryService.class.getName());
    registryService = new MockDubboRegistry(registryURL);
    invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(serviceURL);
    given(invoker.getInterface()).willReturn(RegistryService.class);
    given(invoker.invoke(new RpcInvocation())).willReturn(null);
    dubboRegistry = new DubboRegistry(invoker, registryService);
    notifyListener = mock(NotifyListener.class);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with NotifyListener

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

the class MulticastRegistryTest method testUnsubscribe.

/**
 * Test method for {@link org.apache.dubbo.registry.multicast.MulticastRegistry#unsubscribe(URL, NotifyListener)}
 */
@Test
public void testUnsubscribe() {
    // subscribe first
    registry.subscribe(consumerUrl, new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
        // do nothing
        }
    });
    // then unsubscribe
    registry.unsubscribe(consumerUrl, new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
            Map<URL, Set<NotifyListener>> subscribed = registry.getSubscribed();
            Set<NotifyListener> listeners = subscribed.get(consumerUrl);
            assertTrue(listeners.isEmpty());
            Map<URL, Set<URL>> received = registry.getReceived();
            assertTrue(received.get(consumerUrl).isEmpty());
        }
    });
}
Also used : Set(java.util.Set) Map(java.util.Map) 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