Search in sources :

Example 41 with NotifyListener

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

the class ConsulRegistryTest method testSubscribe.

@Test
public void testSubscribe() {
    NotifyListener listener = mock(NotifyListener.class);
    consulRegistry.subscribe(serviceUrl, listener);
    Map<URL, Set<NotifyListener>> subscribed = consulRegistry.getSubscribed();
    assertThat(subscribed.size(), is(1));
    assertThat(subscribed.get(serviceUrl).size(), is(1));
    consulRegistry.unsubscribe(serviceUrl, listener);
    subscribed = consulRegistry.getSubscribed();
    assertThat(subscribed.size(), is(1));
    assertThat(subscribed.get(serviceUrl).size(), is(0));
}
Also used : Set(java.util.Set) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 42 with NotifyListener

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

the class AbstractRegistryTest method testSubscribeAndUnsubscribe.

/**
 * test subscribe and unsubscribe
 */
@Test
public void testSubscribeAndUnsubscribe() throws Exception {
    // test subscribe
    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(url, listener);
    Set<NotifyListener> subscribeListeners = abstractRegistry.getSubscribed().get(url);
    MatcherAssert.assertThat(true, Matchers.equalTo(subscribeListeners.contains(listener)));
    // test unsubscribe
    abstractRegistry.unsubscribe(url, listener);
    Set<NotifyListener> unsubscribeListeners = abstractRegistry.getSubscribed().get(url);
    MatcherAssert.assertThat(false, Matchers.equalTo(unsubscribeListeners.contains(listener)));
}
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 43 with NotifyListener

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

the class RedisRegistryTest method testSubscribeExpireCache.

@Test
public void testSubscribeExpireCache() throws Exception {
    redisRegistry.register(PROVIDER_URL_A);
    redisRegistry.register(PROVIDER_URL_B);
    NotifyListener listener = new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
        }
    };
    redisRegistry.subscribe(SERVICE_URL, listener);
    Field expireCache = RedisRegistry.class.getDeclaredField("expireCache");
    expireCache.setAccessible(true);
    Map<URL, Long> cacheExpire = (Map<URL, Long>) expireCache.get(redisRegistry);
    assertThat(cacheExpire.get(PROVIDER_URL_A) > 0, is(true));
    assertThat(cacheExpire.get(PROVIDER_URL_B) > 0, is(true));
    redisRegistry.unregister(PROVIDER_URL_A);
    boolean success = false;
    for (int i = 0; i < 30; i++) {
        cacheExpire = (Map<URL, Long>) expireCache.get(redisRegistry);
        if (cacheExpire.get(PROVIDER_URL_A) == null) {
            success = true;
            break;
        }
        Thread.sleep(500);
    }
    assertThat(success, is(true));
}
Also used : Field(java.lang.reflect.Field) List(java.util.List) Map(java.util.Map) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 44 with NotifyListener

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

the class RedisRegistryTest method testSubscribeWhenProviderCrash.

@Test
public void testSubscribeWhenProviderCrash() throws Exception {
    // unit test will fail if doExpire=false
    // Field doExpireField = RedisRegistry.class.getDeclaredField("doExpire");
    // doExpireField.setAccessible(true);
    // doExpireField.set(redisRegistry, false);
    redisRegistry.register(PROVIDER_URL_A);
    redisRegistry.register(PROVIDER_URL_B);
    assertThat(redisRegistry.getRegistered().contains(PROVIDER_URL_A), is(true));
    assertThat(redisRegistry.getRegistered().contains(PROVIDER_URL_B), is(true));
    Set<URL> notifiedUrls = new HashSet<>();
    Object lock = new Object();
    NotifyListener listener = new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
            synchronized (lock) {
                notifiedUrls.clear();
                notifiedUrls.addAll(urls);
            }
        }
    };
    redisRegistry.subscribe(SERVICE_URL, listener);
    assertThat(redisRegistry.getSubscribed().size(), is(1));
    boolean firstOk = false;
    boolean secondOk = false;
    for (int i = 0; i < 30; i++) {
        synchronized (lock) {
            if (notifiedUrls.contains(PROVIDER_URL_A) && notifiedUrls.contains(PROVIDER_URL_B)) {
                firstOk = true;
                break;
            }
        }
        Thread.sleep(500);
    }
    assertThat(firstOk, is(true));
    // kill -9 to providerB
    Field registeredField = AbstractRegistry.class.getDeclaredField("registered");
    registeredField.setAccessible(true);
    ((Set<URL>) registeredField.get(redisRegistry)).remove(PROVIDER_URL_B);
    for (int i = 0; i < 30; i++) {
        synchronized (lock) {
            if (notifiedUrls.contains(PROVIDER_URL_A) && notifiedUrls.size() == 1) {
                secondOk = true;
                break;
            }
        }
        Thread.sleep(500);
    }
    assertThat(secondOk, is(true));
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) List(java.util.List) URL(org.apache.dubbo.common.URL) HashSet(java.util.HashSet) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 45 with NotifyListener

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

the class NacosRegistry method unsubscribeEventListener.

private void unsubscribeEventListener(String serviceName, final URL url, final NotifyListener listener) throws NacosException {
    ConcurrentMap<NotifyListener, EventListener> notifyListenerEventListenerConcurrentMap = nacosListeners.get(url);
    if (notifyListenerEventListenerConcurrentMap == null) {
        return;
    }
    EventListener nacosListener = notifyListenerEventListenerConcurrentMap.get(listener);
    if (nacosListener == null) {
        return;
    }
    namingService.unsubscribe(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), nacosListener);
}
Also used : EventListener(com.alibaba.nacos.api.naming.listener.EventListener) 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