From 27318c7ee5bc385743f42f8a29dc33a3e8994f30 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 9 Apr 2018 14:15:43 -0400 Subject: [PATCH] INT-4445: Fix JDBC tests for Derby JIRA: https://jira.spring.io/browse/INT-4445 Looks like `/dataSource` temporary directory is busy in between different tests. The thought is like a high-frequently polling endpoint keeps DB resource from removing. * Explicitly `stop()` endpoint in the `JdbcMessageStoreChannelTests` * Optimize `JdbcMessageStoreChannelTests.testSendAndActivateTransactionalSend()` to rely on the short `1` millisecond TX timeout. This safes for us 10 seconds of the tests executions. * Configure all the embedded DB beans for the `ignore-failures="ALL"` as a fallback option if polling endpoint is not a cause of the concurrent resource access. **Cherry-pick to 5.0.x** --- .../AggregatorIntegrationTests-context.xml | 2 +- .../JdbcMetadataStoreTests-context.xml | 2 +- ...geStoreChannelIntegrationTests-context.xml | 2 +- ...annelOnePollerIntegrationTests-context.xml | 2 +- .../JdbcMessageStoreChannelTests-context.xml | 2 +- .../store/JdbcMessageStoreChannelTests.java | 52 +++++++++++++++---- .../store/JdbcMessageStoreTests-context.xml | 2 +- .../channel/DataSource-derby-context.xml | 2 +- 8 files changed, 49 insertions(+), 17 deletions(-) diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml index 5416b0bf78..1483599a4d 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/AggregatorIntegrationTests-context.xml @@ -21,7 +21,7 @@ + ignore-failures="ALL"> diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml index 83887861fc..af11b00230 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/metadata/JdbcMetadataStoreTests-context.xml @@ -9,7 +9,7 @@ - + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml index ac7c86492f..8052f10fb9 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelIntegrationTests-context.xml @@ -16,7 +16,7 @@ - + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml index 5fd50b81a8..c3777c0a63 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelOnePollerIntegrationTests-context.xml @@ -16,7 +16,7 @@ + ignore-failures="ALL"> diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests-context.xml index 8f9b7ed94b..32204f0489 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests-context.xml @@ -14,7 +14,7 @@ - + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests.java index 96f25b8ec2..0a6d383b35 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreChannelTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +24,14 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.endpoint.AbstractEndpoint; import org.springframework.integration.store.MessageGroup; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.support.GenericMessage; @@ -38,6 +42,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; +/** + * @author Dave Syer + * @author Mark Fisher + * @author Oleg Zhurakousky + * @author Gary Russell + * @author Artem Bilan + */ + @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @@ -49,6 +61,21 @@ public class JdbcMessageStoreChannelTests { @Autowired private JdbcMessageStore messageStore; + @Autowired + @Qualifier("service-activator") + private AbstractEndpoint serviceActivator; + + @Before + public void init() { + Service.reset(1); + this.serviceActivator.start(); + } + + @After + public void tearDown() { + this.serviceActivator.stop(); + } + @BeforeTransaction public void clear() { for (MessageGroup group : messageStore) { @@ -58,8 +85,7 @@ public class JdbcMessageStoreChannelTests { @Test public void testSendAndActivate() throws Exception { - Service.reset(1); - input.send(new GenericMessage("foo")); + input.send(new GenericMessage<>("foo")); Service.await(10000); assertEquals(1, Service.messages.size()); assertEquals(0, messageStore.getMessageGroup("JdbcMessageStoreChannelTests").size()); @@ -67,9 +93,8 @@ public class JdbcMessageStoreChannelTests { @Test public void testSendAndActivateWithRollback() throws Exception { - Service.reset(1); Service.fail = true; - input.send(new GenericMessage("foo")); + input.send(new GenericMessage<>("foo")); Service.await(10000); assertEquals(1, Service.messages.size()); // After a rollback in the poller the message is still waiting to be delivered @@ -77,13 +102,12 @@ public class JdbcMessageStoreChannelTests { } @Test - @Transactional + @Transactional(timeout = 1) public void testSendAndActivateTransactionalSend() throws Exception { - Service.reset(1); - input.send(new GenericMessage("foo")); + input.send(new GenericMessage<>("foo")); // This will time out because the transaction has not committed yet try { - Service.await(10000); + Service.await(10); fail("Expected timeout"); } catch (IllegalStateException e) { @@ -96,21 +120,28 @@ public class JdbcMessageStoreChannelTests { } public static class Service { + private static boolean fail = false; + private static boolean alreadyFailed = false; - private static List messages = new CopyOnWriteArrayList(); + + private static List messages = new CopyOnWriteArrayList<>(); + private static CountDownLatch latch = new CountDownLatch(0); + public static void reset(int count) { fail = false; alreadyFailed = false; messages.clear(); latch = new CountDownLatch(count); } + public static void await(long timeout) throws InterruptedException { if (!latch.await(timeout, TimeUnit.MILLISECONDS)) { throw new IllegalStateException("Timed out waiting for message"); } } + public String echo(String input) { if (!alreadyFailed) { messages.add(input); @@ -122,6 +153,7 @@ public class JdbcMessageStoreChannelTests { } return input; } + } } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreTests-context.xml index 83887861fc..af11b00230 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/JdbcMessageStoreTests-context.xml @@ -9,7 +9,7 @@ - + diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-derby-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-derby-context.xml index ed032eb1df..a89601d807 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-derby-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/store/channel/DataSource-derby-context.xml @@ -16,7 +16,7 @@ /> --> - +