Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#3356]Fix no throw exception when publish event but no subsciber. #3363

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
}
}
}