Skip to content

Commit

Permalink
[ISSUE#3356]fix no throw exception when publish event but no subscibe…
Browse files Browse the repository at this point in the history
…r. (alibaba#3363)
  • Loading branch information
zongtanghu authored Jul 17, 2020
1 parent 0163d2d commit ff929b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ private static boolean publishEvent(final Class<? extends Event> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
public class NotifyCenterTest {

private static class TestSlowEvent extends SlowEvent {

}

private static class TestEvent extends Event {
Expand Down Expand Up @@ -330,9 +331,11 @@ public void onEvent(Event event) {
}

private static class TestSlowEvent1 extends SlowEvent {

}

private static class TestSlowEvent2 extends SlowEvent {

}

@Test
Expand All @@ -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<TestSlowEvent1>() {
@Override
public void onEvent(TestSlowEvent1 event) {
Expand All @@ -365,34 +368,36 @@ public Class<? extends Event> subscribeType() {
public void onEvent(TestSlowEvent2 event) {
count2.incrementAndGet();
latch2.countDown();

}

@Override
public Class<? extends Event> 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
Expand All @@ -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) {
Expand All @@ -421,7 +426,7 @@ public void onEvent(Event event) {
latch2.countDown();
}
}

@Override
public List<Class<? extends Event>> subscribeTypes() {
List<Class<? extends Event>> subTypes = new ArrayList<Class<? extends Event>>();
Expand All @@ -447,9 +452,11 @@ public List<Class<? extends Event>> subscribeTypes() {
}

private static class TestSlowEvent5 extends SlowEvent {

}

private static class TestEvent6 extends Event {

}

@Test
Expand Down Expand Up @@ -502,4 +509,16 @@ public List<Class<? extends Event>> 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()));
}
}
}

0 comments on commit ff929b7

Please sign in to comment.