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));
}
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)));
}
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));
}
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));
}
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);
}
Aggregations