From 967ea0b123c468e5fb5373fc69fd890ad4f051a9 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 12 Oct 2010 08:57:39 -0400 Subject: [PATCH] INT-1493, polished assertion to make sure that it asserts on atMost 1 invocation of the poller since there is still a natural race condition between stop() and poller loop --- .../integration/endpoint/PollingLifecycleTests.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PollingLifecycleTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PollingLifecycleTests.java index dd9c07252a..e070cc0e8a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PollingLifecycleTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PollingLifecycleTests.java @@ -19,11 +19,13 @@ import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static org.easymock.EasyMock.reset; +import static org.mockito.Mockito.atMost; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.junit.Before; @@ -77,11 +79,16 @@ public class PollingLifecycleTests { consumer.afterPropertiesSet(); consumer.start(); assertTrue(latch.await(2, TimeUnit.SECONDS)); + Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); consumer.stop(); for (int i = 0; i < 10; i++) { channel.send(new GenericMessage("foo")); } - Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); + Thread.sleep(2000); // give enough time for poller to kick in if it didn't stop properly + // we'll still have a natural race condition between call to stop() and poller polling + // so what we really have to assert is that it doesn't poll for more then once after stop() was called + Mockito.reset(handler); + Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class)); } @Test @@ -91,7 +98,6 @@ public class PollingLifecycleTests { SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean(); PollerMetadata pollerMetadata = new PollerMetadata(); - pollerMetadata.setMaxMessagesPerPoll(-1); // should be overriden in FB pollerMetadata.setTrigger(new PeriodicTrigger(2000)); adapterFactory.setPollerMetadata(pollerMetadata); MessageSource source = spy(new MessageSource() {