Skip to content

Commit b291332

Browse files
author
Dave Syer
committed
Use CountDownLatch instead of Thread.sleep()
... to wait for ApplicationContext to close in the 3 tests that we needed to do so. Fixes gh-664
1 parent 3304dd1 commit b291332

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownEndpointTests.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.TimeUnit;
21+
1922
import org.junit.Test;
2023
import org.springframework.boot.context.properties.EnableConfigurationProperties;
24+
import org.springframework.context.ApplicationListener;
2125
import org.springframework.context.annotation.Bean;
2226
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.event.ContextClosedEvent;
2328

2429
import static org.hamcrest.Matchers.startsWith;
25-
import static org.junit.Assert.assertFalse;
2630
import static org.junit.Assert.assertThat;
2731
import static org.junit.Assert.assertTrue;
2832

@@ -41,23 +45,36 @@ public ShutdownEndpointTests() {
4145

4246
@Test
4347
public void invoke() throws Exception {
48+
CountDownLatch latch = this.context.getBean(Config.class).latch;
4449
assertThat((String) getEndpointBean().invoke().get("message"),
4550
startsWith("Shutting down"));
4651
assertTrue(this.context.isActive());
47-
Thread.sleep(600);
48-
assertFalse(this.context.isActive());
52+
assertTrue(latch.await(10, TimeUnit.SECONDS));
4953
}
5054

5155
@Configuration
5256
@EnableConfigurationProperties
5357
public static class Config {
5458

59+
private CountDownLatch latch = new CountDownLatch(1);
60+
5561
@Bean
5662
public ShutdownEndpoint endpoint() {
5763
ShutdownEndpoint endpoint = new ShutdownEndpoint();
5864
endpoint.setEnabled(true);
5965
return endpoint;
6066
}
6167

68+
@Bean
69+
public ApplicationListener<ContextClosedEvent> listener() {
70+
return new ApplicationListener<ContextClosedEvent>() {
71+
@Override
72+
public void onApplicationEvent(ContextClosedEvent event) {
73+
Config.this.latch.countDown();
74+
}
75+
};
76+
77+
}
78+
6279
}
6380
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616

1717
package org.springframework.boot.actuate.endpoint;
1818

19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.TimeUnit;
21+
1922
import org.junit.After;
2023
import org.junit.Test;
2124
import org.springframework.boot.builder.SpringApplicationBuilder;
2225
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.context.ApplicationListener;
2327
import org.springframework.context.ConfigurableApplicationContext;
2428
import org.springframework.context.annotation.Bean;
2529
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.event.ContextClosedEvent;
2631

2732
import static org.hamcrest.Matchers.startsWith;
28-
import static org.junit.Assert.assertFalse;
2933
import static org.junit.Assert.assertThat;
3034
import 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

Comments
 (0)