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
This commit is contained in:
Artem Bilan
2018-03-19 12:51:23 -04:00
parent 59d01cb6b3
commit 68490acf6f
2 changed files with 19 additions and 18 deletions

View File

@@ -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'");
}
/**

View File

@@ -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) {