Skip to content

Commit 1f463a1

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 d8db112 commit 1f463a1

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
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
/**
@@ -82,6 +84,7 @@ public void setRefreshSharedInterval(long refreshSharedInterval) {
8284
Assert.isTrue(!this.cachingDelegates,
8385
"'refreshSharedInterval' cannot be changed when using 'CachingClientConnectionFactory` delegates");
8486
this.refreshSharedInterval = refreshSharedInterval;
87+
this.failBack = refreshSharedInterval != Long.MAX_VALUE;
8588
}
8689

8790
/**
@@ -148,7 +151,7 @@ public void registerSender(TcpSender sender) {
148151
protected TcpConnectionSupport obtainConnection() throws Exception {
149152
FailoverTcpConnection sharedConnection = (FailoverTcpConnection) getTheConnection();
150153
boolean shared = !isSingleUse() && !this.cachingDelegates;
151-
boolean refreshShared = shared
154+
boolean refreshShared = this.failBack && shared
152155
&& sharedConnection != null
153156
&& System.currentTimeMillis() > this.creationTime + this.refreshSharedInterval;
154157
if (sharedConnection != null && sharedConnection.isOpen() && !refreshShared) {

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

Lines changed: 21 additions & 15 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.ArgumentMatchers.any;
2526
import static org.mockito.Mockito.doAnswer;
2627
import static org.mockito.Mockito.doReturn;
2728
import static org.mockito.Mockito.doThrow;
@@ -44,7 +45,6 @@
4445
import java.util.concurrent.atomic.AtomicInteger;
4546
import java.util.concurrent.atomic.AtomicReference;
4647

47-
import org.junit.Rule;
4848
import org.junit.Test;
4949
import org.mockito.InOrder;
5050
import org.mockito.Mockito;
@@ -60,7 +60,6 @@
6060
import org.springframework.integration.ip.tcp.TcpInboundGateway;
6161
import org.springframework.integration.ip.tcp.TcpOutboundGateway;
6262
import org.springframework.integration.ip.util.TestingUtilities;
63-
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
6463
import org.springframework.integration.test.util.TestUtils;
6564
import org.springframework.integration.util.SimplePool;
6665
import org.springframework.messaging.Message;
@@ -90,12 +89,6 @@ public void publishEvent(Object event) {
9089

9190
};
9291

93-
@Rule
94-
public Log4j2LevelAdjuster adjuster =
95-
Log4j2LevelAdjuster.trace()
96-
.classes(SimplePool.class)
97-
.categories("org.springframework.integration.ip.tcp");
98-
9992
@Test
10093
public void testFailoverGood() throws Exception {
10194
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
@@ -120,15 +113,20 @@ public void testFailoverGood() throws Exception {
120113

121114
@Test
122115
public void testRefreshShared() throws Exception {
123-
testRefreshShared(false);
116+
testRefreshShared(false, 10_000);
124117
}
125118

126119
@Test
127120
public void testRefreshSharedCloseOnRefresh() throws Exception {
128-
testRefreshShared(true);
121+
testRefreshShared(true, 10_000);
122+
}
123+
124+
@Test
125+
public void testRefreshSharedInfinite() throws Exception {
126+
testRefreshShared(false, Long.MAX_VALUE);
129127
}
130128

131-
private void testRefreshShared(boolean closeOnRefresh) throws Exception {
129+
private void testRefreshShared(boolean closeOnRefresh, long interval) throws Exception {
132130
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
133131
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
134132
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
@@ -153,21 +151,29 @@ private void testRefreshShared(boolean closeOnRefresh) throws Exception {
153151
failoverFactory.start();
154152
TcpConnectionSupport connection = failoverFactory.getConnection();
155153
assertNotNull(TestUtils.getPropertyValue(failoverFactory, "theConnection"));
156-
failoverFactory.setRefreshSharedInterval(10_000);
154+
failoverFactory.setRefreshSharedInterval(interval);
155+
InOrder inOrder = inOrder(factory1, factory2, conn1, conn2);
156+
inOrder.verify(factory1).getConnection();
157+
inOrder.verify(factory2).getConnection();
158+
inOrder.verify(conn1).registerListener(any());
159+
inOrder.verify(conn1).isOpen();
157160
assertSame(failoverFactory.getConnection(), connection);
161+
inOrder.verifyNoMoreInteractions();
158162
failoverFactory.setRefreshSharedInterval(-1);
159163
assertNotSame(failoverFactory.getConnection(), connection);
160-
InOrder inOrder = inOrder(factory1, factory2, conn1);
161-
inOrder.verify(factory1).getConnection();
162-
inOrder.verify(factory2).getConnection();
163164
inOrder.verify(factory1).getConnection();
164165
inOrder.verify(factory2).getConnection();
165166
if (closeOnRefresh) {
167+
inOrder.verify(conn2).registerListener(any());
168+
inOrder.verify(conn2).isOpen();
166169
inOrder.verify(conn1).close();
167170
}
168171
else {
172+
inOrder.verify(conn1).registerListener(any());
173+
inOrder.verify(conn1).isOpen();
169174
inOrder.verify(conn1, never()).close();
170175
}
176+
inOrder.verifyNoMoreInteractions();
171177
}
172178

173179
@Test(expected = IOException.class)

0 commit comments

Comments
 (0)