From 68490acf6f7debb6c9135533a0101d66a2e5109b Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 19 Mar 2018 12:51:23 -0400 Subject: [PATCH] Fix race condition with in file-split-ftp https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2398/ * Remove `FileExistsMode.APPEND_NO_FLUSH` from the app. It's not reliable for unit testing: keeps resources opened and cause false positive assertions but at the same time no any interactions with mock `session` * Clean up test directories before and after tests --- .../samples/filesplit/Application.java | 4 +-- .../samples/filesplit/ApplicationTests.java | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/applications/file-split-ftp/src/main/java/org/springframework/integration/samples/filesplit/Application.java b/applications/file-split-ftp/src/main/java/org/springframework/integration/samples/filesplit/Application.java index 8cd372ee..995e9c56 100644 --- a/applications/file-split-ftp/src/main/java/org/springframework/integration/samples/filesplit/Application.java +++ b/applications/file-split-ftp/src/main/java/org/springframework/integration/samples/filesplit/Application.java @@ -37,7 +37,6 @@ import org.springframework.integration.file.dsl.FileWritingMessageHandlerSpec; import org.springframework.integration.file.dsl.Files; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.file.splitter.FileSplitter; -import org.springframework.integration.file.support.FileExistsMode; import org.springframework.integration.ftp.dsl.Ftp; import org.springframework.integration.ftp.session.DefaultFtpSessionFactory; import org.springframework.integration.http.config.EnableIntegrationGraphController; @@ -94,8 +93,7 @@ public class Application { public FileWritingMessageHandlerSpec fileOut() { return Files.outboundAdapter("'/tmp/out'") .appendNewLine(true) - .fileNameExpression("payload.substring(1, 4) + '.txt'") - .fileExistsMode(FileExistsMode.APPEND_NO_FLUSH); // files remain open for efficiency + .fileNameExpression("payload.substring(1, 4) + '.txt'"); } /** diff --git a/applications/file-split-ftp/src/test/java/org/springframework/integration/samples/filesplit/ApplicationTests.java b/applications/file-split-ftp/src/test/java/org/springframework/integration/samples/filesplit/ApplicationTests.java index 5f9cbfe6..d2261cc6 100644 --- a/applications/file-split-ftp/src/test/java/org/springframework/integration/samples/filesplit/ApplicationTests.java +++ b/applications/file-split-ftp/src/test/java/org/springframework/integration/samples/filesplit/ApplicationTests.java @@ -22,7 +22,6 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.willThrow; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import java.io.BufferedReader; @@ -33,7 +32,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; +import org.apache.commons.io.FileUtils; import org.apache.commons.net.ftp.FTPFile; +import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -72,21 +73,27 @@ public class ApplicationTests { public static void setup() { // Configure the boot property to send email to the test email server. System.setProperty("spring.mail.port", Integer.toString(smtpServer.getPort())); - new File("/tmp/in/foo.txt").delete(); - new File("/tmp/in/foo.txt.success").delete(); - new File("/tmp/in/foo.txt.failed").delete(); } @Before - public void beforeTest() { + public void beforeTest() throws IOException { smtpServer.getMessages().clear(); + + FileUtils.cleanDirectory(new File("/tmp/in")); + FileUtils.cleanDirectory(new File("/tmp/out")); + this.fileInboundChannelAdapter.start(); } + @After + public void tearDown() throws IOException { + FileUtils.cleanDirectory(new File("/tmp/in")); + FileUtils.cleanDirectory(new File("/tmp/out")); + } + @SuppressWarnings("unchecked") @Test public void testSuccess() throws Exception { - reset(this.session); String message = runTest(false); assertThat(message).contains("File successfully split and transferred"); assertThat(message).contains(TestUtils.applySystemFileSeparator("/tmp/in/foo.txt")); @@ -95,7 +102,7 @@ public class ApplicationTests { @Test public void testFailure() throws Exception { willThrow(new RuntimeException("fail test exception")) - .given(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); + .given(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); String message = runTest(true); assertThat(message).contains("File split and transfer failed"); assertThat(message).contains("fail test exception"); @@ -116,34 +123,31 @@ public class ApplicationTests { in.renameTo(new File("/tmp/in/", "foo.txt")); File out = new File("/tmp/out/002.txt"); int n = 0; - while(n++ < 100 && (!out.exists() || out.length() < 12)) { + while (n++ < 100 && (!out.exists() || out.length() < 12)) { Thread.sleep(100); } assertThat(out.exists()).isTrue(); BufferedReader br = new BufferedReader(new FileReader(out)); assertThat(br.readLine()).isEqualTo("*002,foo,bar"); br.close(); - out.delete(); out = new File("/tmp/out/006.txt"); n = 0; - while(n++ < 100 && (!out.exists() || out.length() < 12)) { + while (n++ < 100 && (!out.exists() || out.length() < 12)) { Thread.sleep(100); } assertThat(out.exists()).isTrue(); br = new BufferedReader(new FileReader(out)); assertThat(br.readLine()).isEqualTo("*006,baz,qux"); br.close(); - out.delete(); out = new File("/tmp/out/009.txt"); n = 0; - while(n++ < 100 && (!out.exists() || out.length() < 12)) { + while (n++ < 100 && (!out.exists() || out.length() < 12)) { Thread.sleep(100); } assertThat(out.exists()).isTrue(); br = new BufferedReader(new FileReader(out)); assertThat(br.readLine()).isEqualTo("*009,fiz,buz"); br.close(); - out.delete(); if (!fail) { in = new File("/tmp/in/", "foo.txt.success"); } @@ -151,11 +155,10 @@ public class ApplicationTests { in = new File("/tmp/in/", "foo.txt.failed"); } n = 0; - while(n++ < 100 && !in.exists()) { + while (n++ < 100 && !in.exists()) { Thread.sleep(100); } assertThat(in.exists()).isTrue(); - in.delete(); // verify FTP verify(this.session).write(any(InputStream.class), eq("foo/002.txt.writing")); if (!fail) {