1818
1919import de .codecentric .boot .admin .server .domain .entities .Instance ;
2020import de .codecentric .boot .admin .server .domain .entities .InstanceRepository ;
21+ import de .codecentric .boot .admin .server .domain .events .InstanceDeregisteredEvent ;
22+ import de .codecentric .boot .admin .server .domain .events .InstanceEndpointsDetectedEvent ;
2123import de .codecentric .boot .admin .server .domain .events .InstanceEvent ;
2224import de .codecentric .boot .admin .server .domain .events .InstanceStatusChangedEvent ;
25+ import de .codecentric .boot .admin .server .domain .values .Endpoints ;
2326import de .codecentric .boot .admin .server .domain .values .InstanceId ;
2427import de .codecentric .boot .admin .server .domain .values .Registration ;
2528import de .codecentric .boot .admin .server .domain .values .StatusInfo ;
29+ import de .codecentric .boot .admin .server .eventstore .InstanceEventPublisher ;
2630import reactor .core .publisher .Mono ;
2731import reactor .test .StepVerifier ;
2832
2933import java .time .Duration ;
34+ import java .util .Collections ;
3035import org .junit .Before ;
3136import org .junit .Test ;
3237
3338import static org .assertj .core .api .Assertions .assertThat ;
3439import static org .assertj .core .api .Assertions .assertThatThrownBy ;
40+ import static org .mockito .ArgumentMatchers .any ;
3541import static org .mockito .Mockito .mock ;
3642import static org .mockito .Mockito .when ;
3743
@@ -42,63 +48,122 @@ public class RemindingNotifierTest {
4248 private final Instance instance2 = Instance .create (InstanceId .of ("id-2" ))
4349 .register (Registration .create ("App" , "http://health" ).build ())
4450 .withStatusInfo (StatusInfo .ofDown ());
45- private final InstanceEvent appDown = new InstanceStatusChangedEvent (instance1 .getId (), instance1 .getVersion (),
46- StatusInfo .ofDown ());
47- private final InstanceEvent appUp = new InstanceStatusChangedEvent (instance1 .getId (), instance1 .getVersion (),
48- StatusInfo .ofUp ());
51+ private final InstanceEvent appDown = new InstanceStatusChangedEvent (instance1 .getId (), 0L , StatusInfo .ofDown ());
52+ private final InstanceEvent appUp = new InstanceStatusChangedEvent (instance1 .getId (), 0L , StatusInfo .ofUp ());
53+ private final InstanceEvent appEndpointsDiscovered = new InstanceEndpointsDetectedEvent (instance1 .getId (), 0L ,
54+ Endpoints .empty ());
55+ private final InstanceEvent appDeregister = new InstanceDeregisteredEvent (instance1 .getId (), 0L );
4956 private final InstanceEvent otherAppUp = new InstanceStatusChangedEvent (instance2 .getId (), 0L , StatusInfo .ofUp ());
5057 private InstanceRepository repository ;
5158
5259 @ Before
5360 public void setUp () {
5461 repository = mock (InstanceRepository .class );
62+ when (repository .find (any ())).thenReturn (Mono .empty ());
5563 when (repository .find (instance1 .getId ())).thenReturn (Mono .just (instance1 ));
5664 when (repository .find (instance2 .getId ())).thenReturn (Mono .just (instance2 ));
5765 }
5866
5967 @ Test
60- public void test_ctor_assert () {
68+ public void should_throw_on_invalid_ctor () {
6169 assertThatThrownBy (() -> new CompositeNotifier (null )).isInstanceOf (IllegalArgumentException .class );
6270 }
6371
6472 @ Test
65- public void test_remind () {
73+ public void should_remind_only_down_events () throws InterruptedException {
6674 TestNotifier notifier = new TestNotifier ();
6775 RemindingNotifier reminder = new RemindingNotifier (notifier , repository );
6876 reminder .setReminderPeriod (Duration .ZERO );
6977
7078 StepVerifier .create (reminder .notify (appDown )).verifyComplete ();
79+ StepVerifier .create (reminder .notify (appEndpointsDiscovered )).verifyComplete ();
7180 StepVerifier .create (reminder .notify (otherAppUp )).verifyComplete ();
72- reminder .sendReminders ();
73- reminder .sendReminders ();
81+ Thread .sleep (10 );
82+ StepVerifier .create (reminder .sendReminders ()).verifyComplete ();
83+ Thread .sleep (10 );
84+ StepVerifier .create (reminder .sendReminders ()).verifyComplete ();
7485
75- assertThat (notifier .getEvents ()).containsOnly (appDown , otherAppUp , appDown , appDown );
86+ assertThat (notifier .getEvents ()).containsExactlyInAnyOrder (appDown , appEndpointsDiscovered , otherAppUp , appDown ,
87+ appDown );
7688
7789 }
7890
7991 @ Test
80- public void test_no_remind_after_up () {
92+ public void should_not_remind_remind_after_up () {
8193 TestNotifier notifier = new TestNotifier ();
8294 RemindingNotifier reminder = new RemindingNotifier (notifier , repository );
8395 reminder .setReminderPeriod (Duration .ZERO );
8496
8597 StepVerifier .create (reminder .notify (appDown )).verifyComplete ();
8698 StepVerifier .create (reminder .notify (appUp )).verifyComplete ();
87- reminder .sendReminders ();
99+ StepVerifier . create ( reminder .sendReminders ()). verifyComplete ();
88100
89- assertThat (notifier .getEvents ()).containsOnly (appDown , appUp );
101+ assertThat (notifier .getEvents ()).containsExactlyInAnyOrder (appDown , appUp );
102+ }
103+
104+
105+ @ Test
106+ public void should_not_remind_remind_after_deregister () {
107+ TestNotifier notifier = new TestNotifier ();
108+ RemindingNotifier reminder = new RemindingNotifier (notifier , repository );
109+ reminder .setReminderPeriod (Duration .ZERO );
110+
111+ StepVerifier .create (reminder .notify (appDown )).verifyComplete ();
112+ StepVerifier .create (reminder .notify (appDeregister )).verifyComplete ();
113+ StepVerifier .create (reminder .sendReminders ()).verifyComplete ();
114+
115+ assertThat (notifier .getEvents ()).containsExactlyInAnyOrder (appDown , appDeregister );
90116 }
91117
92118 @ Test
93- public void test_no_remind_before_end () {
119+ public void should_not_remind_remind_before_period_ends () {
94120 TestNotifier notifier = new TestNotifier ();
95121 RemindingNotifier reminder = new RemindingNotifier (notifier , repository );
96122 reminder .setReminderPeriod (Duration .ofHours (24 ));
97123
98124 StepVerifier .create (reminder .notify (appDown )).verifyComplete ();
99- reminder .sendReminders ();
125+ StepVerifier .create (reminder .sendReminders ()).verifyComplete ();
126+
127+ assertThat (notifier .getEvents ()).containsExactlyInAnyOrder (appDown );
128+ }
129+
130+ @ Test
131+ public void should_resubscribe_after_error () {
132+ FluxNotifier notifier = new FluxNotifier ();
133+
134+ RemindingNotifier reminder = new RemindingNotifier (notifier , repository );
135+ reminder .setCheckReminderInverval (Duration .ofMillis (1 ));
136+ reminder .setReminderPeriod (Duration .ofMillis (1 ));
137+ reminder .start ();
138+
139+ StepVerifier .create (notifier )
140+ .expectSubscription ()
141+ .then (() -> StepVerifier .create (reminder .notify (appDown )).verifyComplete ())
142+ .expectNext (appDown )
143+ .expectNext (appDown )
144+ .then (() -> StepVerifier .create (
145+ reminder .notify (new InstanceDeregisteredEvent (InstanceId .of ("ERROR" ), 0L ))).verifyComplete ())
146+ .expectNext (appDown )
147+ .expectNext (appDown )
148+ .then (reminder ::stop )
149+ .expectNoEvent (Duration .ofMillis (10 ))
150+ .thenCancel ()
151+ .verify ();
152+
153+ reminder .stop ();
154+ }
155+
156+
157+ private static class FluxNotifier extends InstanceEventPublisher implements Notifier {
100158
101- assertThat (notifier .getEvents ()).containsOnly (appDown );
159+ @ Override
160+ public Mono <Void > notify (InstanceEvent event ) {
161+ if (event .getInstance ().getValue ().equals ("ERROR" )) {
162+ throw new IllegalArgumentException ("TEST-ERROR" );
163+ }
164+ this .publish (Collections .singletonList (event ));
165+ return Mono .empty ();
166+ }
102167 }
103168
104169}
0 commit comments