Search in sources :

Example 1 with SmartSubscriber

use of com.alibaba.nacos.common.notify.listener.SmartSubscriber in project nacos by alibaba.

the class NotifyCenterTest method testSeveralEventsPublishedBySinglePublisher.

@Test
public void testSeveralEventsPublishedBySinglePublisher() throws Exception {
    final AtomicInteger count1 = new AtomicInteger(0);
    final AtomicInteger count2 = new AtomicInteger(0);
    final CountDownLatch latch1 = new CountDownLatch(3);
    final CountDownLatch latch2 = new CountDownLatch(3);
    NotifyCenter.registerToPublisher(SmartEvent1.class, 1024);
    NotifyCenter.registerToPublisher(SmartEvent2.class, 1024);
    NotifyCenter.registerSubscriber(new SmartSubscriber() {

        @Override
        public List<Class<? extends Event>> subscribeTypes() {
            List<Class<? extends Event>> list = new ArrayList<Class<? extends Event>>();
            list.add(SmartEvent1.class);
            list.add(SmartEvent2.class);
            return list;
        }

        @Override
        public void onEvent(Event event) {
            if (event instanceof SmartEvent1) {
                count1.incrementAndGet();
                latch1.countDown();
            }
            if (event instanceof SmartEvent2) {
                count2.incrementAndGet();
                latch2.countDown();
            }
        }
    });
    for (int i = 0; i < 3; i++) {
        Assert.assertTrue(NotifyCenter.publishEvent(new SmartEvent1()));
        Assert.assertTrue(NotifyCenter.publishEvent(new SmartEvent2()));
    }
    latch1.await(3000L, TimeUnit.MILLISECONDS);
    latch2.await(3000L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(3, count1.get());
    Assert.assertEquals(3, count2.get());
}
Also used : SmartSubscriber(com.alibaba.nacos.common.notify.listener.SmartSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with SmartSubscriber

use of com.alibaba.nacos.common.notify.listener.SmartSubscriber in project nacos by alibaba.

the class NotifyCenterTest method testMutipleSlowEventsListenedBySmartsubscriber.

@Test
public void testMutipleSlowEventsListenedBySmartsubscriber() throws Exception {
    NotifyCenter.registerToSharePublisher(TestSlowEvent3.class);
    NotifyCenter.registerToSharePublisher(TestSlowEvent4.class);
    final AtomicInteger count1 = new AtomicInteger(0);
    final AtomicInteger count2 = new AtomicInteger(0);
    final CountDownLatch latch1 = new CountDownLatch(3);
    final CountDownLatch latch2 = new CountDownLatch(3);
    NotifyCenter.registerSubscriber(new SmartSubscriber() {

        @Override
        public void onEvent(Event event) {
            if (event instanceof TestSlowEvent3) {
                count1.incrementAndGet();
                latch1.countDown();
            }
            if (event instanceof TestSlowEvent4) {
                count2.incrementAndGet();
                latch2.countDown();
            }
        }

        @Override
        public List<Class<? extends Event>> subscribeTypes() {
            List<Class<? extends Event>> subTypes = new ArrayList<Class<? extends Event>>();
            subTypes.add(TestSlowEvent3.class);
            subTypes.add(TestSlowEvent4.class);
            return subTypes;
        }
    });
    for (int i = 0; i < 3; i++) {
        Assert.assertTrue(NotifyCenter.publishEvent(new TestSlowEvent3()));
        Assert.assertTrue(NotifyCenter.publishEvent(new TestSlowEvent4()));
    }
    ThreadUtils.sleep(2000L);
    latch1.await(3000L, TimeUnit.MILLISECONDS);
    latch2.await(3000L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(3, count1.get());
    Assert.assertEquals(3, count2.get());
}
Also used : SmartSubscriber(com.alibaba.nacos.common.notify.listener.SmartSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with SmartSubscriber

use of com.alibaba.nacos.common.notify.listener.SmartSubscriber in project nacos by alibaba.

the class NotifyCenterTest method testMutipleKindsEventsCanListenBySmartsubscriber.

@Test
public void testMutipleKindsEventsCanListenBySmartsubscriber() throws Exception {
    NotifyCenter.registerToSharePublisher(TestSlowEvent5.class);
    NotifyCenter.registerToPublisher(TestEvent6.class, 1024);
    final AtomicInteger count1 = new AtomicInteger(0);
    final AtomicInteger count2 = new AtomicInteger(0);
    final CountDownLatch latch1 = new CountDownLatch(3);
    final CountDownLatch latch2 = new CountDownLatch(3);
    NotifyCenter.registerSubscriber(new SmartSubscriber() {

        @Override
        public void onEvent(Event event) {
            if (event instanceof TestSlowEvent5) {
                count1.incrementAndGet();
                latch1.countDown();
            }
            if (event instanceof TestEvent6) {
                count2.incrementAndGet();
                latch2.countDown();
            }
        }

        @Override
        public List<Class<? extends Event>> subscribeTypes() {
            List<Class<? extends Event>> subTypes = new ArrayList<Class<? extends Event>>();
            subTypes.add(TestSlowEvent5.class);
            subTypes.add(TestEvent6.class);
            return subTypes;
        }
    });
    for (int i = 0; i < 3; i++) {
        Assert.assertTrue(NotifyCenter.publishEvent(new TestSlowEvent5()));
        Assert.assertTrue(NotifyCenter.publishEvent(new TestEvent6()));
    }
    ThreadUtils.sleep(3000L);
    latch1.await(3000L, TimeUnit.MILLISECONDS);
    latch2.await(3000L, TimeUnit.MILLISECONDS);
    Assert.assertEquals(3, count1.get());
    Assert.assertEquals(3, count2.get());
}
Also used : SmartSubscriber(com.alibaba.nacos.common.notify.listener.SmartSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

SmartSubscriber (com.alibaba.nacos.common.notify.listener.SmartSubscriber)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3