From 77b0917190ed1dc26424fd891d4d751606bae11d Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 9 Sep 2013 16:35:40 +0300 Subject: [PATCH] INT-3138: add `awaitTermination` to delayer tests `DelayerHandlerRescheduleIntegrationTests` fail sometimes on asserts. Looks like `context.destroy()` works very slow and `` manages to send delayed Messages JIRA: https://jira.springsource.org/browse/INT-3138 --- .../DelayerHandlerRescheduleIntegrationTests.java | 13 ++++++++++--- .../DelayerHandlerRescheduleIntegrationTests.java | 8 ++++++-- .../DelayerHandlerRescheduleIntegrationTests.java | 13 ++++++++++--- .../DelayerHandlerRescheduleIntegrationTests.java | 10 ++++++++-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/DelayerHandlerRescheduleIntegrationTests.java index aeb3aef562..795fdf20fc 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/DelayerHandlerRescheduleIntegrationTests.java @@ -19,23 +19,28 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.concurrent.TimeUnit; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import com.gemstone.gemfire.cache.Cache; + import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.handler.DelayHandler; import org.springframework.integration.store.MessageGroup; import org.springframework.integration.store.MessageGroupStore; import org.springframework.integration.support.MessageBuilder; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.Assert; -import com.gemstone.gemfire.cache.Cache; /** * @author Artem Bilan @@ -73,10 +78,12 @@ public class DelayerHandlerRescheduleIntegrationTests { input.send(MessageBuilder.withPayload("test2").build()); // Emulate restart and check Cache state before next start + // Interrupt taskScheduler as quickly as possible + ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context); + taskScheduler.shutdown(); + taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS); context.destroy(); - Thread.sleep(100); - try { context.getBean("input", MessageChannel.class); fail("IllegalStateException expected"); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/DelayerHandlerRescheduleIntegrationTests.java index 1a4ed4ae94..4692650213 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/DelayerHandlerRescheduleIntegrationTests.java @@ -31,6 +31,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.MessagingException; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.handler.DelayHandler; @@ -41,6 +42,7 @@ import org.springframework.integration.util.UUIDConverter; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.transaction.support.TransactionSynchronization; import org.springframework.transaction.support.TransactionSynchronizationAdapter; import org.springframework.transaction.support.TransactionSynchronizationManager; @@ -80,10 +82,12 @@ public class DelayerHandlerRescheduleIntegrationTests { input.send(MessageBuilder.withPayload("test2").build()); // Emulate restart and check DB state before next start + // Interrupt taskScheduler as quickly as possible + ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context); + taskScheduler.shutdown(); + taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS); context.destroy(); - Thread.sleep(100); - try { context.getBean("input", MessageChannel.class); fail("IllegalStateException expected"); diff --git a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/DelayerHandlerRescheduleIntegrationTests.java index 97487fc79f..2fb73b724f 100644 --- a/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-mongodb/src/test/java/org/springframework/integration/mongodb/store/DelayerHandlerRescheduleIntegrationTests.java @@ -21,13 +21,17 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Iterator; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; import org.hamcrest.Matchers; -import org.junit.Test; + import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.handler.DelayHandler; import org.springframework.integration.mongodb.rules.MongoDbAvailable; @@ -35,6 +39,7 @@ import org.springframework.integration.mongodb.rules.MongoDbAvailableTests; import org.springframework.integration.store.MessageGroup; import org.springframework.integration.store.MessageGroupStore; import org.springframework.integration.support.MessageBuilder; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** * @author Artem Bilan @@ -62,10 +67,12 @@ public class DelayerHandlerRescheduleIntegrationTests extends MongoDbAvailableTe input.send(MessageBuilder.withPayload("test2").build()); // Emulate restart and check DB state before next start + // Interrupt taskScheduler as quickly as possible + ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context); + taskScheduler.shutdown(); + taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS); context.destroy(); - Thread.sleep(100); - try { context.getBean("input", MessageChannel.class); fail("IllegalStateException expected"); diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java index 2e6f2e8ce6..58a68bcb79 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java @@ -19,12 +19,15 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.concurrent.TimeUnit; + import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; +import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.core.PollableChannel; import org.springframework.integration.handler.DelayHandler; import org.springframework.integration.redis.rules.RedisAvailable; @@ -32,6 +35,7 @@ import org.springframework.integration.redis.rules.RedisAvailableTests; import org.springframework.integration.store.MessageGroup; import org.springframework.integration.store.MessageGroupStore; import org.springframework.integration.support.MessageBuilder; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** * @author Artem Bilan @@ -60,10 +64,12 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest input.send(MessageBuilder.withPayload("test2").build()); // Emulate restart and check DB state before next start + // Interrupt taskScheduler as quickly as possible + ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context); + taskScheduler.shutdown(); + taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS); context.destroy(); - Thread.sleep(100); - try { context.getBean("input", MessageChannel.class); fail("IllegalStateException expected");