Skip to content

Commit 160d609

Browse files
committed
Protect against race condition where output file exists but it is empty
Previously, there was a timing window where the output file had been created but it was empty. This would cause the test to fail as the output was read from the empty file and didn’t match the expected “Hello World”. This commit updates the test to only process the resources in the output directory when all the resolved resources have a non-zero content length. An @before method has also been added to delete the output produced by the test so that the outcome of the test isn’t affected by files generated by previous runs. Fixes spring-projectsgh-1735
1 parent 9a2d654 commit 160d609

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

spring-boot-samples/spring-boot-sample-integration/src/test/java/sample/integration/consumer/SampleIntegrationApplicationTests.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@
1616

1717
package sample.integration.consumer;
1818

19+
import java.io.File;
20+
import java.io.IOException;
1921
import java.util.concurrent.Callable;
2022
import java.util.concurrent.Executors;
2123
import java.util.concurrent.Future;
2224
import java.util.concurrent.TimeUnit;
2325

2426
import org.junit.AfterClass;
27+
import org.junit.Before;
2528
import org.junit.BeforeClass;
2629
import org.junit.Test;
2730
import org.springframework.boot.SpringApplication;
2831
import org.springframework.context.ConfigurableApplicationContext;
2932
import org.springframework.core.io.DefaultResourceLoader;
3033
import org.springframework.core.io.Resource;
3134
import org.springframework.core.io.support.ResourcePatternUtils;
35+
import org.springframework.util.FileSystemUtils;
3236
import org.springframework.util.StreamUtils;
3337

3438
import sample.integration.SampleIntegrationApplication;
@@ -40,6 +44,7 @@
4044
* Basic integration tests for service demo application.
4145
*
4246
* @author Dave Syer
47+
* @author Andy Wilkinson
4348
*/
4449
public class SampleIntegrationApplicationTests {
4550

@@ -57,6 +62,11 @@ public static void stop() {
5762
}
5863
}
5964

65+
@Before
66+
public void deleteOutput() {
67+
FileSystemUtils.deleteRecursively(new File("target/output"));
68+
}
69+
6070
@Test
6171
public void testVanillaExchange() throws Exception {
6272
SpringApplication.run(ProducerApplication.class, "World");
@@ -69,12 +79,10 @@ private String getOutput() throws Exception {
6979
new Callable<String>() {
7080
@Override
7181
public String call() throws Exception {
72-
Resource[] resources = new Resource[0];
82+
Resource[] resources = getResourcesWithContent();
7383
while (resources.length == 0) {
7484
Thread.sleep(200);
75-
resources = ResourcePatternUtils.getResourcePatternResolver(
76-
new DefaultResourceLoader()).getResources(
77-
"file:target/output/**");
85+
resources = getResourcesWithContent();
7886
}
7987
StringBuilder builder = new StringBuilder();
8088
for (Resource resource : resources) {
@@ -86,4 +94,15 @@ public String call() throws Exception {
8694
});
8795
return future.get(30, TimeUnit.SECONDS);
8896
}
97+
98+
private Resource[] getResourcesWithContent() throws IOException {
99+
Resource[] candidates = ResourcePatternUtils.getResourcePatternResolver(
100+
new DefaultResourceLoader()).getResources("file:target/output/**");
101+
for (Resource candidate : candidates) {
102+
if (candidate.contentLength() == 0) {
103+
return new Resource[0];
104+
}
105+
}
106+
return candidates;
107+
}
89108
}

0 commit comments

Comments
 (0)