From ff929b75ad8f2d11316c4be8310b56a19828a299 Mon Sep 17 00:00:00 2001 From: Hu Zongtang Date: Fri, 17 Jul 2020 16:12:08 +0800 Subject: [PATCH] [ISSUE#3356]fix no throw exception when publish event but no subsciber. (#3363) --- .../nacos/common/notify/NotifyCenter.java | 4 +- .../nacos/common/notify/NotifyCenterTest.java | 39 ++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java b/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java index 1798203cf40..059fee9fbc3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java +++ b/common/src/main/java/com/alibaba/nacos/common/notify/NotifyCenter.java @@ -290,7 +290,9 @@ private static boolean publishEvent(final Class eventType, fina EventPublisher publisher = INSTANCE.publisherMap.get(topic); return publisher.publish(event); } - throw new NoSuchElementException("There are no [" + topic + "] publishers for this event, please register"); + + LOGGER.warn("There are no [{}] publishers for this event, please register", topic); + return false; } /** diff --git a/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java b/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java index 6bf98fa9819..a7163938d00 100644 --- a/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java @@ -35,6 +35,7 @@ public class NotifyCenterTest { private static class TestSlowEvent extends SlowEvent { + } private static class TestEvent extends Event { @@ -330,9 +331,11 @@ public void onEvent(Event event) { } private static class TestSlowEvent1 extends SlowEvent { + } private static class TestSlowEvent2 extends SlowEvent { + } @Test @@ -343,10 +346,10 @@ public void testMutipleSlowEventsListenedBySubscriber() 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.registerSubscriber(new Subscriber() { @Override public void onEvent(TestSlowEvent1 event) { @@ -365,7 +368,7 @@ public Class subscribeType() { public void onEvent(TestSlowEvent2 event) { count2.incrementAndGet(); latch2.countDown(); - + } @Override @@ -373,26 +376,28 @@ public Class subscribeType() { return TestSlowEvent2.class; } }); - + for (int i = 0; i < 3; i++) { Assert.assertTrue(NotifyCenter.publishEvent(new TestSlowEvent1())); Assert.assertTrue(NotifyCenter.publishEvent(new TestSlowEvent2())); } - + ThreadUtils.sleep(2000L); - + latch1.await(3000L, TimeUnit.MILLISECONDS); latch2.await(3000L, TimeUnit.MILLISECONDS); - + Assert.assertEquals(3, count1.get()); Assert.assertEquals(3, count2.get()); - + } private static class TestSlowEvent3 extends SlowEvent { + } private static class TestSlowEvent4 extends SlowEvent { + } @Test @@ -408,7 +413,7 @@ public void testMutipleSlowEventsListenedBySmartsubscriber() throws Exception { final CountDownLatch latch2 = new CountDownLatch(3); NotifyCenter.registerSubscriber(new SmartSubscriber() { - + @Override public void onEvent(Event event) { if (event instanceof TestSlowEvent3) { @@ -421,7 +426,7 @@ public void onEvent(Event event) { latch2.countDown(); } } - + @Override public List> subscribeTypes() { List> subTypes = new ArrayList>(); @@ -447,9 +452,11 @@ public List> subscribeTypes() { } private static class TestSlowEvent5 extends SlowEvent { + } private static class TestEvent6 extends Event { + } @Test @@ -502,4 +509,16 @@ public List> subscribeTypes() { Assert.assertEquals(3, count2.get()); } + + private static class TestEvent7 extends Event { + + } + + @Test + public void testPublishEventByNoSubscriber() { + + for (int i = 0; i < 3; i++) { + Assert.assertFalse(NotifyCenter.publishEvent(new TestEvent7())); + } + } }