16
16
17
17
package org .springframework .boot .actuate .endpoint ;
18
18
19
+ import java .util .concurrent .CountDownLatch ;
20
+ import java .util .concurrent .TimeUnit ;
21
+
19
22
import org .junit .After ;
20
23
import org .junit .Test ;
21
24
import org .springframework .boot .builder .SpringApplicationBuilder ;
22
25
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
26
+ import org .springframework .context .ApplicationListener ;
23
27
import org .springframework .context .ConfigurableApplicationContext ;
24
28
import org .springframework .context .annotation .Bean ;
25
29
import org .springframework .context .annotation .Configuration ;
30
+ import org .springframework .context .event .ContextClosedEvent ;
26
31
27
32
import static org .hamcrest .Matchers .startsWith ;
28
- import static org .junit .Assert .assertFalse ;
29
33
import static org .junit .Assert .assertThat ;
30
34
import static org .junit .Assert .assertTrue ;
31
35
@@ -49,22 +53,22 @@ public void close() {
49
53
public void shutdownChild () throws Exception {
50
54
this .context = new SpringApplicationBuilder (Config .class ).child (Empty .class )
51
55
.web (false ).run ();
56
+ CountDownLatch latch = this .context .getBean (Config .class ).latch ;
52
57
assertThat ((String ) getEndpointBean ().invoke ().get ("message" ),
53
58
startsWith ("Shutting down" ));
54
59
assertTrue (this .context .isActive ());
55
- Thread .sleep (600 );
56
- assertFalse (this .context .isActive ());
60
+ assertTrue (latch .await (10 , TimeUnit .SECONDS ));
57
61
}
58
62
59
63
@ Test
60
64
public void shutdownParent () throws Exception {
61
65
this .context = new SpringApplicationBuilder (Empty .class ).child (Config .class )
62
66
.web (false ).run ();
67
+ CountDownLatch latch = this .context .getBean (Config .class ).latch ;
63
68
assertThat ((String ) getEndpointBean ().invoke ().get ("message" ),
64
69
startsWith ("Shutting down" ));
65
70
assertTrue (this .context .isActive ());
66
- Thread .sleep (600 );
67
- assertFalse (this .context .isActive ());
71
+ assertTrue (latch .await (10 , TimeUnit .SECONDS ));
68
72
}
69
73
70
74
private ShutdownEndpoint getEndpointBean () {
@@ -75,13 +79,25 @@ private ShutdownEndpoint getEndpointBean() {
75
79
@ EnableConfigurationProperties
76
80
public static class Config {
77
81
82
+ private CountDownLatch latch = new CountDownLatch (1 );
83
+
78
84
@ Bean
79
85
public ShutdownEndpoint endpoint () {
80
86
ShutdownEndpoint endpoint = new ShutdownEndpoint ();
81
87
endpoint .setEnabled (true );
82
88
return endpoint ;
83
89
}
84
90
91
+ @ Bean
92
+ public ApplicationListener <ContextClosedEvent > listener () {
93
+ return new ApplicationListener <ContextClosedEvent >() {
94
+ @ Override
95
+ public void onApplicationEvent (ContextClosedEvent event ) {
96
+ Config .this .latch .countDown ();
97
+ }
98
+ };
99
+
100
+ }
85
101
}
86
102
87
103
@ Configuration
0 commit comments