GH-3199: Fix fail back with Long.MAX_VALUE
Resolves https://github.com/spring-projects/spring-integration/issues/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.
This commit is contained in:
committed by
Artem Bilan
parent
44609d3b13
commit
438e2215b3
@@ -52,6 +52,8 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
|
||||
|
||||
private boolean closeOnRefresh;
|
||||
|
||||
private boolean failBack = true;
|
||||
|
||||
private volatile long creationTime;
|
||||
|
||||
/**
|
||||
@@ -82,6 +84,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
|
||||
Assert.isTrue(!this.cachingDelegates,
|
||||
"'refreshSharedInterval' cannot be changed when using 'CachingClientConnectionFactory` delegates");
|
||||
this.refreshSharedInterval = refreshSharedInterval;
|
||||
this.failBack = refreshSharedInterval != Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,7 +151,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
|
||||
protected TcpConnectionSupport obtainConnection() throws InterruptedException {
|
||||
FailoverTcpConnection sharedConnection = (FailoverTcpConnection) getTheConnection();
|
||||
boolean shared = !isSingleUse() && !this.cachingDelegates;
|
||||
boolean refreshShared = shared
|
||||
boolean refreshShared = this.failBack && shared
|
||||
&& sharedConnection != null
|
||||
&& System.currentTimeMillis() > this.creationTime + this.refreshSharedInterval;
|
||||
if (sharedConnection != null && sharedConnection.isOpen() && !refreshShared) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.ip.tcp.connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
@@ -40,7 +41,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mockito;
|
||||
@@ -56,7 +56,6 @@ import org.springframework.integration.ip.IpHeaders;
|
||||
import org.springframework.integration.ip.tcp.TcpInboundGateway;
|
||||
import org.springframework.integration.ip.tcp.TcpOutboundGateway;
|
||||
import org.springframework.integration.ip.util.TestingUtilities;
|
||||
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.util.SimplePool;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -86,12 +85,6 @@ public class FailoverClientConnectionFactoryTests {
|
||||
|
||||
};
|
||||
|
||||
@Rule
|
||||
public Log4j2LevelAdjuster adjuster =
|
||||
Log4j2LevelAdjuster.trace()
|
||||
.classes(SimplePool.class)
|
||||
.categories("org.springframework.integration.ip.tcp");
|
||||
|
||||
@Test
|
||||
public void testFailoverGood() throws Exception {
|
||||
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
|
||||
@@ -117,15 +110,20 @@ public class FailoverClientConnectionFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testRefreshShared() throws Exception {
|
||||
testRefreshShared(false);
|
||||
testRefreshShared(false, 10_000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshSharedCloseOnRefresh() throws Exception {
|
||||
testRefreshShared(true);
|
||||
testRefreshShared(true, 10_000);
|
||||
}
|
||||
|
||||
private void testRefreshShared(boolean closeOnRefresh) throws Exception {
|
||||
@Test
|
||||
public void testRefreshSharedInfinite() throws Exception {
|
||||
testRefreshShared(false, Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private void testRefreshShared(boolean closeOnRefresh, long interval) throws Exception {
|
||||
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
|
||||
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
|
||||
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
|
||||
@@ -150,21 +148,29 @@ public class FailoverClientConnectionFactoryTests {
|
||||
failoverFactory.start();
|
||||
TcpConnectionSupport connection = failoverFactory.getConnection();
|
||||
assertThat(TestUtils.getPropertyValue(failoverFactory, "theConnection")).isNotNull();
|
||||
failoverFactory.setRefreshSharedInterval(10_000);
|
||||
assertThat(failoverFactory.getConnection()).isSameAs(connection);
|
||||
failoverFactory.setRefreshSharedInterval(-1);
|
||||
assertThat(failoverFactory.getConnection()).isNotSameAs(connection);
|
||||
InOrder inOrder = inOrder(factory1, factory2, conn1);
|
||||
failoverFactory.setRefreshSharedInterval(interval);
|
||||
InOrder inOrder = inOrder(factory1, factory2, conn1, conn2);
|
||||
inOrder.verify(factory1).getConnection();
|
||||
inOrder.verify(factory2).getConnection();
|
||||
inOrder.verify(conn1).registerListener(any());
|
||||
inOrder.verify(conn1).isOpen();
|
||||
assertThat(failoverFactory.getConnection()).isSameAs(connection);
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
failoverFactory.setRefreshSharedInterval(-1);
|
||||
assertThat(failoverFactory.getConnection()).isNotSameAs(connection);
|
||||
inOrder.verify(factory1).getConnection();
|
||||
inOrder.verify(factory2).getConnection();
|
||||
if (closeOnRefresh) {
|
||||
inOrder.verify(conn2).registerListener(any());
|
||||
inOrder.verify(conn2).isOpen();
|
||||
inOrder.verify(conn1).close();
|
||||
}
|
||||
else {
|
||||
inOrder.verify(conn1).registerListener(any());
|
||||
inOrder.verify(conn1).isOpen();
|
||||
inOrder.verify(conn1, never()).close();
|
||||
}
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test(expected = UncheckedIOException.class)
|
||||
|
||||
Reference in New Issue
Block a user