Skip to content

Commit 02b1947

Browse files
committed
GH-3199: Fix fail back with Long.MAX_VALUE
Resolves #3199 When the `refreshSharedInterval` was `Long.MAX_VALUE` the test for whether the interval was exceeded always returned true. Use a boolean instead (already in place on master). I will backport to 5.1.x, 4.3.x after merge.
1 parent 61747e5 commit 02b1947

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
5252

5353
private boolean closeOnRefresh;
5454

55+
private boolean failBack = true;
56+
5557
private volatile long creationTime;
5658

5759
/**
@@ -88,6 +90,7 @@ public void setRefreshSharedInterval(long refreshSharedInterval) {
8890
Assert.isTrue(!this.cachingDelegates,
8991
"'refreshSharedInterval' cannot be changed when using 'CachingClientConnectionFactory` delegates");
9092
this.refreshSharedInterval = refreshSharedInterval;
93+
this.failBack = refreshSharedInterval != Long.MAX_VALUE;
9194
}
9295

9396
/**
@@ -154,7 +157,7 @@ public void registerSender(TcpSender sender) {
154157
protected TcpConnectionSupport obtainConnection() throws Exception {
155158
FailoverTcpConnection sharedConnection = (FailoverTcpConnection) getTheConnection();
156159
boolean shared = !isSingleUse() && !this.cachingDelegates;
157-
boolean refreshShared = shared
160+
boolean refreshShared = this.failBack && shared
158161
&& sharedConnection != null
159162
&& System.currentTimeMillis() > this.creationTime + this.refreshSharedInterval;
160163
if (sharedConnection != null && sharedConnection.isOpen() && !refreshShared) {

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertSame;
2323
import static org.junit.Assert.assertTrue;
2424
import static org.junit.Assert.fail;
25+
import static org.mockito.Matchers.any;
2526
import static org.mockito.Mockito.doAnswer;
2627
import static org.mockito.Mockito.doReturn;
2728
import static org.mockito.Mockito.doThrow;
@@ -45,8 +46,6 @@
4546
import java.util.concurrent.atomic.AtomicInteger;
4647
import java.util.concurrent.atomic.AtomicReference;
4748

48-
import org.apache.log4j.Level;
49-
import org.junit.Rule;
5049
import org.junit.Test;
5150
import org.mockito.InOrder;
5251
import org.mockito.Mockito;
@@ -63,7 +62,6 @@
6362
import org.springframework.integration.ip.tcp.TcpInboundGateway;
6463
import org.springframework.integration.ip.tcp.TcpOutboundGateway;
6564
import org.springframework.integration.ip.util.TestingUtilities;
66-
import org.springframework.integration.test.rule.Log4jLevelAdjuster;
6765
import org.springframework.integration.test.util.TestUtils;
6866
import org.springframework.integration.util.SimplePool;
6967
import org.springframework.messaging.Message;
@@ -93,10 +91,6 @@ public void publishEvent(Object event) {
9391

9492
};
9593

96-
@Rule
97-
public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE,
98-
"org.springframework.integration.ip.tcp", "org.springframework.integration.util.SimplePool");
99-
10094
@Test
10195
public void testFailoverGood() throws Exception {
10296
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
@@ -126,15 +120,20 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
126120

127121
@Test
128122
public void testRefreshShared() throws Exception {
129-
testRefreshShared(false);
123+
testRefreshShared(false, 10_000);
130124
}
131125

132126
@Test
133127
public void testRefreshSharedCloseOnRefresh() throws Exception {
134-
testRefreshShared(true);
128+
testRefreshShared(true, 10_000);
129+
}
130+
131+
@Test
132+
public void testRefreshSharedInfinite() throws Exception {
133+
testRefreshShared(false, Long.MAX_VALUE);
135134
}
136135

137-
private void testRefreshShared(boolean closeOnRefresh) throws Exception {
136+
private void testRefreshShared(boolean closeOnRefresh, long interval) throws Exception {
138137
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
139138
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
140139
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
@@ -159,21 +158,29 @@ private void testRefreshShared(boolean closeOnRefresh) throws Exception {
159158
failoverFactory.start();
160159
TcpConnectionSupport connection = failoverFactory.getConnection();
161160
assertNotNull(TestUtils.getPropertyValue(failoverFactory, "theConnection"));
162-
failoverFactory.setRefreshSharedInterval(10_000);
161+
failoverFactory.setRefreshSharedInterval(interval);
162+
InOrder inOrder = inOrder(factory1, factory2, conn1, conn2);
163+
inOrder.verify(factory1).getConnection();
164+
inOrder.verify(factory2).getConnection();
165+
inOrder.verify(conn1).registerListener(any());
166+
inOrder.verify(conn1).isOpen();
163167
assertSame(failoverFactory.getConnection(), connection);
168+
inOrder.verifyNoMoreInteractions();
164169
failoverFactory.setRefreshSharedInterval(-1);
165170
assertNotSame(failoverFactory.getConnection(), connection);
166-
InOrder inOrder = inOrder(factory1, factory2, conn1);
167-
inOrder.verify(factory1).getConnection();
168-
inOrder.verify(factory2).getConnection();
169171
inOrder.verify(factory1).getConnection();
170172
inOrder.verify(factory2).getConnection();
171173
if (closeOnRefresh) {
174+
inOrder.verify(conn2).registerListener(any());
175+
inOrder.verify(conn2).isOpen();
172176
inOrder.verify(conn1).close();
173177
}
174178
else {
179+
inOrder.verify(conn1).registerListener(any());
180+
inOrder.verify(conn1).isOpen();
175181
inOrder.verify(conn1, never()).close();
176182
}
183+
inOrder.verifyNoMoreInteractions();
177184
}
178185

179186
@Test(expected = IOException.class)

0 commit comments

Comments
 (0)