Fix SftpStreamingMessageSourceTests for race

It turns out there is some race condition between tests
against the same source dir.

* Add `DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD` to clean
up the application context state after each test method
This commit is contained in:
Artem Bilan
2020-05-12 11:30:45 -04:00
parent 65b3bdc39d
commit f759f0bacc

View File

@@ -26,6 +26,8 @@ import static org.junit.Assert.assertThat;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,8 +45,10 @@ import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.filters.AcceptAllFileListFilter;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.metadata.SimpleMetadataStore;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.sftp.SftpTestSupport;
import org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter;
import org.springframework.integration.sftp.session.SftpFileInfo;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.integration.transformer.StreamTransformer;
@@ -63,7 +67,7 @@ import com.jcraft.jsch.ChannelSftp.LsEntry;
*
*/
@RunWith(SpringRunner.class)
@DirtiesContext
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class SftpStreamingMessageSourceTests extends SftpTestSupport {
@Autowired
@@ -81,6 +85,9 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
@Autowired
private ApplicationContext context;
@Autowired
private ConcurrentMap<String, String> metadataMap;
@SuppressWarnings("unchecked")
@Test
public void testAllContents() {
@@ -111,6 +118,7 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
this.adapter.stop();
this.source.setFileInfoJson(false);
this.data.purge(null);
this.metadataMap.clear();
this.adapter.start();
received = (Message<byte[]>) this.data.receive(10000);
assertNotNull(received);
@@ -186,12 +194,20 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
return pollerMetadata;
}
@Bean
public ConcurrentMap<String, String> metadataMap() {
return new ConcurrentHashMap<>();
}
@Bean
@InboundChannelAdapter(channel = "stream", autoStartup = "false")
public MessageSource<InputStream> sftpMessageSource() {
SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template(),
Comparator.comparing(LsEntry::getFilename));
messageSource.setFilter(new AcceptAllFileListFilter<>());
messageSource.setFilter(
new SftpPersistentAcceptOnceFileListFilter(
new SimpleMetadataStore(metadataMap()), "testStreaming"));
messageSource.setRemoteDirectory("sftpSource/");
return messageSource;
}