From aa3e3c70987b76f38c30c03038614b24a0273800 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 21 Oct 2015 17:15:23 -0400 Subject: [PATCH] Fix some failing tests * Modify `ControlBusTests.testControlHeaderChannelReaper()` do not rely on the `timeout`. Make artificial expiration for the entry in the `registry` to allow `reaper` to remove it according the test logic. * Fix race condition around `gateway.setRemoteTimeout(5000);` in the `TcpOutboundGatewayTests`. The problem was that the gateway is called concurrently and `setRemoteTimeout` might be changed before the `handleMessage()` even for the first attempt. --- .../integration/config/xml/ControlBusTests.java | 16 ++++++++++++---- .../ip/tcp/TcpOutboundGatewayTests.java | 14 +++++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ControlBusTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ControlBusTests.java index a1dc21d7e6..ca06dc232e 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ControlBusTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ControlBusTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -36,6 +37,7 @@ import org.springframework.integration.channel.DefaultHeaderChannelRegistry; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.TestUtils; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; @@ -49,6 +51,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @author Dave Syer * @author Gunnar Hillert * @author Gary Russell + * @author Artem Bilan * @since 2.0 */ @ContextConfiguration @@ -102,20 +105,25 @@ public class ControlBusTests { messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()"); Message result = this.output.receive(0); assertNotNull(result); + // No channels in the registry assertEquals(0, result.getPayload()); - this.registry.setReaperDelay(10); this.registry.channelToChannelName(new DirectChannel()); + // Sleep a bit to be sure that we aren't reaped by registry TTL as 60000 + Thread.sleep(100); messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()"); result = this.output.receive(0); assertNotNull(result); assertEquals(1, result.getPayload()); - Thread.sleep(100); + // Some DirectFieldAccessor magic to modify 'expireAt' to the past to avoid timing issues on high-loaded build + Object messageChannelWrapper = + TestUtils.getPropertyValue(this.registry, "channels", Map.class).values().iterator().next(); + DirectFieldAccessor dfa = new DirectFieldAccessor(messageChannelWrapper); + dfa.setPropertyValue("expireAt", System.currentTimeMillis() - 60000); messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.runReaper()"); messagingTemplate.convertAndSend(input, "@integrationHeaderChannelRegistry.size()"); result = this.output.receive(0); assertNotNull(result); assertEquals(0, result.getPayload()); - this.registry.setReaperDelay(60000); } @Test diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java index eb9ba722f1..1845bd9d3d 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpOutboundGatewayTests.java @@ -81,6 +81,7 @@ import org.springframework.messaging.support.GenericMessage; /** * @author Gary Russell + * @author Artem Bilan * @since 2.0 */ public class TcpOutboundGatewayTests { @@ -390,15 +391,18 @@ public class TcpOutboundGatewayTests { results[j] = (Executors.newSingleThreadExecutor().submit(new Callable() { @Override public Integer call() throws Exception { - // increase the timeout after the first send - if (j > 0) { - gateway.setRemoteTimeout(5000); + try { + gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build()); + } + finally { + // increase the timeout after the first send + if (j > 0) { + gateway.setRemoteTimeout(5000); + } } - gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build()); return j; } })); - Thread.sleep(50); } // wait until the server side has processed both requests assertTrue(serverLatch.await(10, TimeUnit.SECONDS));