1616
1717package org .springframework .boot .actuate .endpoint ;
1818
19+ import java .util .concurrent .CountDownLatch ;
20+ import java .util .concurrent .TimeUnit ;
21+
1922import org .junit .After ;
2023import org .junit .Test ;
2124import org .springframework .boot .builder .SpringApplicationBuilder ;
2225import org .springframework .boot .context .properties .EnableConfigurationProperties ;
26+ import org .springframework .context .ApplicationListener ;
2327import org .springframework .context .ConfigurableApplicationContext ;
2428import org .springframework .context .annotation .Bean ;
2529import org .springframework .context .annotation .Configuration ;
30+ import org .springframework .context .event .ContextClosedEvent ;
2631
2732import static org .hamcrest .Matchers .startsWith ;
28- import static org .junit .Assert .assertFalse ;
2933import static org .junit .Assert .assertThat ;
3034import static org .junit .Assert .assertTrue ;
3135
@@ -49,22 +53,22 @@ public void close() {
4953 public void shutdownChild () throws Exception {
5054 this .context = new SpringApplicationBuilder (Config .class ).child (Empty .class )
5155 .web (false ).run ();
56+ CountDownLatch latch = this .context .getBean (Config .class ).latch ;
5257 assertThat ((String ) getEndpointBean ().invoke ().get ("message" ),
5358 startsWith ("Shutting down" ));
5459 assertTrue (this .context .isActive ());
55- Thread .sleep (600 );
56- assertFalse (this .context .isActive ());
60+ assertTrue (latch .await (10 , TimeUnit .SECONDS ));
5761 }
5862
5963 @ Test
6064 public void shutdownParent () throws Exception {
6165 this .context = new SpringApplicationBuilder (Empty .class ).child (Config .class )
6266 .web (false ).run ();
67+ CountDownLatch latch = this .context .getBean (Config .class ).latch ;
6368 assertThat ((String ) getEndpointBean ().invoke ().get ("message" ),
6469 startsWith ("Shutting down" ));
6570 assertTrue (this .context .isActive ());
66- Thread .sleep (600 );
67- assertFalse (this .context .isActive ());
71+ assertTrue (latch .await (10 , TimeUnit .SECONDS ));
6872 }
6973
7074 private ShutdownEndpoint getEndpointBean () {
@@ -75,13 +79,25 @@ private ShutdownEndpoint getEndpointBean() {
7579 @ EnableConfigurationProperties
7680 public static class Config {
7781
82+ private CountDownLatch latch = new CountDownLatch (1 );
83+
7884 @ Bean
7985 public ShutdownEndpoint endpoint () {
8086 ShutdownEndpoint endpoint = new ShutdownEndpoint ();
8187 endpoint .setEnabled (true );
8288 return endpoint ;
8389 }
8490
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+ }
85101 }
86102
87103 @ Configuration
0 commit comments