Skip to content

Commit a1e7108

Browse files
committed
No sleep in RegistrationApplicationListenerTest
1 parent 84cfa69 commit a1e7108

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/services/RegistrationApplicationListenerTest.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,94 @@
11
package de.codecentric.boot.admin.services;
22

3+
import static org.mockito.Matchers.eq;
4+
import static org.mockito.Matchers.isA;
35
import static org.mockito.Mockito.mock;
46
import static org.mockito.Mockito.never;
57
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.when;
9+
10+
import java.util.concurrent.ScheduledFuture;
611

712
import org.junit.Test;
813
import org.springframework.boot.SpringApplication;
914
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
1015
import org.springframework.boot.context.event.ApplicationReadyEvent;
1116
import org.springframework.context.event.ContextClosedEvent;
17+
import org.springframework.scheduling.TaskScheduler;
1218

1319
public class RegistrationApplicationListenerTest {
1420

1521
@Test
1622
public void test_register() throws Exception {
1723
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
18-
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator);
24+
TaskScheduler scheduler = mock(TaskScheduler.class);
25+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
26+
scheduler);
1927

2028
listener.onApplicationReady(
2129
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
2230

23-
Thread.sleep(500);
24-
verify(registrator).register();
31+
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
2532
}
2633

2734
@Test
2835
public void test_no_register() throws Exception {
2936
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
30-
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator);
37+
TaskScheduler scheduler = mock(TaskScheduler.class);
38+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
39+
scheduler);
3140
listener.setAutoRegister(false);
3241

3342
listener.onApplicationReady(
3443
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
3544

36-
Thread.sleep(500);
37-
verify(registrator, never()).register();
45+
verify(scheduler, never()).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
46+
}
47+
48+
@SuppressWarnings({ "unchecked", "rawtypes" })
49+
@Test
50+
public void test_no_register_after_close() throws Exception {
51+
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
52+
TaskScheduler scheduler = mock(TaskScheduler.class);
53+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
54+
scheduler);
55+
56+
ScheduledFuture task = mock(ScheduledFuture.class);
57+
when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
58+
59+
listener.onApplicationReady(
60+
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
61+
62+
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
63+
64+
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
65+
verify(task).cancel(true);
66+
}
67+
68+
@SuppressWarnings({ "unchecked", "rawtypes" })
69+
@Test
70+
public void test_start_stop() throws Exception {
71+
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
72+
TaskScheduler scheduler = mock(TaskScheduler.class);
73+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
74+
scheduler);
75+
76+
ScheduledFuture task = mock(ScheduledFuture.class);
77+
when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
78+
79+
listener.startRegisterTask();
80+
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
81+
82+
listener.stopRegisterTask();
83+
verify(task).cancel(true);
3884
}
3985

4086
@Test
4187
public void test_no_deregister() throws Exception {
4288
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
43-
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator);
89+
TaskScheduler scheduler = mock(TaskScheduler.class);
90+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
91+
scheduler);
4492

4593
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
4694

@@ -50,7 +98,9 @@ public void test_no_deregister() throws Exception {
5098
@Test
5199
public void test_deregister() throws Exception {
52100
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
53-
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator);
101+
TaskScheduler scheduler = mock(TaskScheduler.class);
102+
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
103+
scheduler);
54104
listener.setAutoDeregister(true);
55105

56106
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));

0 commit comments

Comments
 (0)