-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: isValid() should not wait longer than network timeout
- Loading branch information
Showing
4 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
pgjdbc/src/test/java/org/postgresql/test/jdbc4/ConnectionValidTimeoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.jdbc4; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.postgresql.test.TestUtil; | ||
import org.postgresql.test.util.StrangeProxyServer; | ||
import org.postgresql.test.util.rules.annotation.HaveMinimalServerVersion; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.Timeout; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.sql.Connection; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@RunWith(Parameterized.class) | ||
@HaveMinimalServerVersion("9.4") | ||
public class ConnectionValidTimeoutTest { | ||
|
||
@Rule | ||
public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); | ||
|
||
@Parameterized.Parameter(0) | ||
public int networkTimeoutMillis; | ||
@Parameterized.Parameter(1) | ||
public int validationTimeoutSeconds; | ||
@Parameterized.Parameter(2) | ||
public int expectedMaxValidationTimeMillis; | ||
|
||
@Parameterized.Parameters(name = "networkTimeoutMillis={0}, validationTimeoutSeconds={1}, expectedMaxValidationTimeMillis={2}") | ||
public static Iterable<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{500, 1, 600}, | ||
{1500, 1, 1100}, | ||
{0, 1, 1100}, | ||
{500, 0, 600}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void testIsValidRespectsSmallerTimeout() throws Exception { | ||
try (StrangeProxyServer proxyServer = new StrangeProxyServer(TestUtil.getServer(), TestUtil.getPort())) { | ||
final Properties props = new Properties(); | ||
props.setProperty(TestUtil.SERVER_HOST_PORT_PROP, String.format("%s:%s", "localhost", proxyServer.getServerPort())); | ||
try (Connection conn = TestUtil.openDB(props)) { | ||
assertTrue("Connection through proxy should be valid", conn.isValid(validationTimeoutSeconds)); | ||
|
||
conn.setNetworkTimeout(null, networkTimeoutMillis); | ||
assertTrue("Connection through proxy should still be valid", conn.isValid(validationTimeoutSeconds)); | ||
|
||
proxyServer.stopForwardingOlderClients(); | ||
|
||
long start = System.currentTimeMillis(); | ||
boolean result = conn.isValid(validationTimeoutSeconds); | ||
long elapsed = System.currentTimeMillis() - start; | ||
|
||
assertFalse("Broken connection should not be valid", result); | ||
|
||
assertTrue(String.format( | ||
"Connection validation should not take longer than %d ms" | ||
+ " when network timeout is %d ms and validation timeout is %d s" | ||
+ " (actual result: %d ms)", | ||
expectedMaxValidationTimeMillis, | ||
networkTimeoutMillis, | ||
validationTimeoutSeconds, | ||
elapsed), | ||
elapsed <= expectedMaxValidationTimeMillis | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters