Skip to content

Commit 438e221

Browse files
garyrussellartembilan
authored andcommitted
GH-3199: Fix fail back with Long.MAX_VALUE
Resolves #3199 Wheen 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 44609d3 commit 438e221

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 InterruptedException {
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
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21+
import static org.mockito.ArgumentMatchers.any;
2122
import static org.mockito.Mockito.doAnswer;
2223
import static org.mockito.Mockito.doReturn;
2324
import static org.mockito.Mockito.doThrow;
@@ -40,7 +41,6 @@
4041
import java.util.concurrent.atomic.AtomicInteger;
4142
import java.util.concurrent.atomic.AtomicReference;
4243

43-
import org.junit.Rule;
4444
import org.junit.Test;
4545
import org.mockito.InOrder;
4646
import org.mockito.Mockito;
@@ -56,7 +56,6 @@
5656
import org.springframework.integration.ip.tcp.TcpInboundGateway;
5757
import org.springframework.integration.ip.tcp.TcpOutboundGateway;
5858
import org.springframework.integration.ip.util.TestingUtilities;
59-
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
6059
import org.springframework.integration.test.util.TestUtils;
6160
import org.springframework.integration.util.SimplePool;
6261
import org.springframework.messaging.Message;
@@ -86,12 +85,6 @@ public void publishEvent(Object event) {
8685

8786
};
8887

89-
@Rule
90-
public Log4j2LevelAdjuster adjuster =
91-
Log4j2LevelAdjuster.trace()
92-
.classes(SimplePool.class)
93-
.categories("org.springframework.integration.ip.tcp");
94-
9588
@Test
9689
public void testFailoverGood() throws Exception {
9790
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
@@ -117,15 +110,20 @@ public void testFailoverGood() throws Exception {
117110

118111
@Test
119112
public void testRefreshShared() throws Exception {
120-
testRefreshShared(false);
113+
testRefreshShared(false, 10_000);
121114
}
122115

123116
@Test
124117
public void testRefreshSharedCloseOnRefresh() throws Exception {
125-
testRefreshShared(true);
118+
testRefreshShared(true, 10_000);
119+
}
120+
121+
@Test
122+
public void testRefreshSharedInfinite() throws Exception {
123+
testRefreshShared(false, Long.MAX_VALUE);
126124
}
127125

128-
private void testRefreshShared(boolean closeOnRefresh) throws Exception {
126+
private void testRefreshShared(boolean closeOnRefresh, long interval) throws Exception {
129127
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
130128
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
131129
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
@@ -150,21 +148,29 @@ private void testRefreshShared(boolean closeOnRefresh) throws Exception {
150148
failoverFactory.start();
151149
TcpConnectionSupport connection = failoverFactory.getConnection();
152150
assertThat(TestUtils.getPropertyValue(failoverFactory, "theConnection")).isNotNull();
153-
failoverFactory.setRefreshSharedInterval(10_000);
151+
failoverFactory.setRefreshSharedInterval(interval);
152+
InOrder inOrder = inOrder(factory1, factory2, conn1, conn2);
153+
inOrder.verify(factory1).getConnection();
154+
inOrder.verify(factory2).getConnection();
155+
inOrder.verify(conn1).registerListener(any());
156+
inOrder.verify(conn1).isOpen();
154157
assertThat(failoverFactory.getConnection()).isSameAs(connection);
158+
inOrder.verifyNoMoreInteractions();
155159
failoverFactory.setRefreshSharedInterval(-1);
156160
assertThat(failoverFactory.getConnection()).isNotSameAs(connection);
157-
InOrder inOrder = inOrder(factory1, factory2, conn1);
158-
inOrder.verify(factory1).getConnection();
159-
inOrder.verify(factory2).getConnection();
160161
inOrder.verify(factory1).getConnection();
161162
inOrder.verify(factory2).getConnection();
162163
if (closeOnRefresh) {
164+
inOrder.verify(conn2).registerListener(any());
165+
inOrder.verify(conn2).isOpen();
163166
inOrder.verify(conn1).close();
164167
}
165168
else {
169+
inOrder.verify(conn1).registerListener(any());
170+
inOrder.verify(conn1).isOpen();
166171
inOrder.verify(conn1, never()).close();
167172
}
173+
inOrder.verifyNoMoreInteractions();
168174
}
169175

170176
@Test(expected = UncheckedIOException.class)

0 commit comments

Comments
 (0)