Skip to content

Commit 5aef89b

Browse files
committed
Add another safety net to catch exceptions from notifiers
hopefully fixes codecentric#1175
1 parent 6ac1c67 commit 5aef89b

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/notify/NotificationTrigger.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import reactor.core.scheduler.Schedulers;
2525

2626
import org.reactivestreams.Publisher;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729

2830
public class NotificationTrigger extends AbstractEventHandler<InstanceEvent> {
31+
private static final Logger log = LoggerFactory.getLogger(NotificationTrigger.class);
2932
private final Notifier notifier;
3033

3134
public NotificationTrigger(Notifier notifier, Publisher<InstanceEvent> publisher) {
@@ -40,6 +43,8 @@ protected Publisher<Void> handle(Flux<InstanceEvent> publisher) {
4043
}
4144

4245
protected Mono<Void> sendNotifications(InstanceEvent event) {
43-
return notifier.notify(event);
46+
return this.notifier.notify(event)
47+
.doOnError(e -> log.warn("Couldn't notify for event {} ", event, e))
48+
.onErrorResume(e -> Mono.empty());
4449
}
4550
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/notify/NotificationTriggerTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@
2323
import de.codecentric.boot.admin.server.domain.values.InstanceId;
2424
import de.codecentric.boot.admin.server.domain.values.Registration;
2525
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
26+
import reactor.core.publisher.Mono;
2627
import reactor.test.publisher.TestPublisher;
2728

2829
import org.junit.Test;
2930

31+
import static org.mockito.ArgumentMatchers.any;
3032
import static org.mockito.Mockito.clearInvocations;
3133
import static org.mockito.Mockito.mock;
3234
import static org.mockito.Mockito.never;
3335
import static org.mockito.Mockito.times;
3436
import static org.mockito.Mockito.verify;
37+
import static org.mockito.Mockito.when;
3538

3639
public class NotificationTriggerTest {
3740
private final Instance instance = Instance.create(InstanceId.of("id-1"))
@@ -47,17 +50,44 @@ public void should_notify_on_event() throws InterruptedException {
4750
Thread.sleep(500L); //wait for subscription
4851

4952
//when registered event is emitted
50-
InstanceStatusChangedEvent event = new InstanceStatusChangedEvent(instance.getId(), instance.getVersion(),
51-
StatusInfo.ofDown());
53+
InstanceStatusChangedEvent event = new InstanceStatusChangedEvent(this.instance.getId(),
54+
this.instance.getVersion(),
55+
StatusInfo.ofDown()
56+
);
5257
events.next(event);
5358
//then should notify
5459
verify(notifier, times(1)).notify(event);
5560

5661
//when registered event is emitted but the trigger has been stopped
5762
trigger.stop();
5863
clearInvocations(notifier);
59-
events.next(new InstanceRegisteredEvent(instance.getId(), instance.getVersion(), instance.getRegistration()));
64+
events.next(new InstanceRegisteredEvent(this.instance.getId(), this.instance.getVersion(), this.instance.getRegistration()));
6065
//then should not notify
6166
verify(notifier, never()).notify(event);
6267
}
68+
69+
@Test
70+
public void should_resume_on_exceptopn() throws InterruptedException {
71+
//given
72+
Notifier notifier = mock(Notifier.class);
73+
TestPublisher<InstanceEvent> events = TestPublisher.create();
74+
NotificationTrigger trigger = new NotificationTrigger(notifier, events);
75+
trigger.start();
76+
Thread.sleep(500L); //wait for subscription
77+
78+
79+
when(notifier.notify(any())).thenReturn(Mono.error(new IllegalStateException("Test"))).thenReturn(Mono.empty());
80+
81+
//when exception for the first event is thrown and a subsequent event is fired
82+
InstanceStatusChangedEvent event = new InstanceStatusChangedEvent(this.instance.getId(),
83+
this.instance.getVersion(),
84+
StatusInfo.ofDown()
85+
);
86+
events.next(event);
87+
events.next(event);
88+
89+
//the notifier was after the exception
90+
verify(notifier, times(2)).notify(event);
91+
}
92+
6393
}

0 commit comments

Comments
 (0)