Search in sources :

Example 11 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class SofaRegistrySubscribeCallbackTest method handleData.

@Test
public void handleData() throws Exception {
    Subscriber listSub = new MockSubscribe(5);
    Configurator attrSub = new MockConfigurator(2);
    final AtomicInteger ps = new AtomicInteger(0);
    ProviderInfoListener listener = new ProviderInfoListener() {

        @Override
        public void addProvider(ProviderGroup providerGroup) {
            ps.addAndGet(providerGroup.size());
        }

        @Override
        public void removeProvider(ProviderGroup providerGroup) {
            ps.addAndGet(-providerGroup.size());
        }

        @Override
        public void updateProviders(ProviderGroup providerGroup) {
            ps.set(providerGroup.size());
        }

        @Override
        public void updateAllProviders(List<ProviderGroup> providerGroups) {
            ps.set(0);
            for (ProviderGroup providerGroup : providerGroups) {
                ps.addAndGet(providerGroup.size());
            }
        }
    };
    SofaRegistrySubscribeCallback callback = new SofaRegistrySubscribeCallback();
    callback.addProviderInfoListener("xxxxx", new ConsumerConfig(), listener);
    Assert.assertTrue((!callback.flag[0].get()) && (!callback.flag[1].get()));
    callback.handleData("xxxxx", buildConfigPs(2));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag[1].get());
    Assert.assertTrue(ps.get() == 0);
    callback.handleData("xxxxx", buildPs(5));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag == null);
    // default+localZone
    Assert.assertEquals(ps.get(), 5 + 5);
    callback = new SofaRegistrySubscribeCallback();
    ps.set(0);
    callback.addProviderInfoListener("yyyyy", new ConsumerConfig(), listener);
    callback.handleData("yyyyy", buildPs(5));
    try {
        Thread.sleep(200);
    } finally {
    }
    callback.handleData("yyyyy", buildConfigPs(2));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag == null);
    Assert.assertEquals(ps.get(), 5 + 5);
}
Also used : Subscriber(com.alipay.sofa.registry.client.api.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) Configurator(com.alipay.sofa.registry.client.api.Configurator) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Aggregations

ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 ArrayList (java.util.ArrayList)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 Configurator (com.alipay.sofa.registry.client.api.Configurator)2 Subscriber (com.alipay.sofa.registry.client.api.Subscriber)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)2 List (java.util.List)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Event (com.alibaba.nacos.api.naming.listener.Event)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ConfiguratorRegistration (com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration)1 SubscriberRegistration (com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)1 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 Registry (com.alipay.sofa.rpc.registry.Registry)1