postgresql-jdbc.git / summary / log / commit / refs
commit 17a4d6a500d4456c8bcac63d3d0cbb282fc99bea
Author: AlexElin <alexey.elin@list.ru>
Commit: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Date: Fri Jun 01 14:44:18 2018 +0000
refactor: migrate MultiHostsConnectionTest to JUnit4 (#886)
.../test/hostchooser/MultiHostTestSuite.java | 99 +-----------
.../test/hostchooser/MultiHostsConnectionTest.java | 178 ++++++++++++++-------
2 files changed, 124 insertions(+), 153 deletions(-)
diff --git a/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostTestSuite.java b/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostTestSuite.java
index 2486a655..984577e5 100644
--- a/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostTestSuite.java
+++ b/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostTestSuite.java
@@ -5,102 +5,13 @@
package org.postgresql.test.hostchooser;
-import org.postgresql.test.TestUtil;
-import org.postgresql.util.PSQLException;
-
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.util.Properties;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
/*
* Executes multi host tests (aka master/slave connectivity selection).
*/
-public class MultiHostTestSuite extends TestSuite {
-
- public static java.sql.Connection openSecondaryDB() throws Exception {
- TestUtil.initDriver();
-
- Properties props = new Properties();
-
- props.setProperty("user", TestUtil.getUser());
- props.setProperty("password", TestUtil.getPassword());
-
- return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer(), getSecondaryPort()), props);
- }
-
- public static java.sql.Connection openSecondaryDB2() throws Exception {
- TestUtil.initDriver();
-
- Properties props = new Properties();
-
- props.setProperty("user", TestUtil.getUser());
- props.setProperty("password", TestUtil.getPassword());
-
- return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer2(), getSecondaryPort2()), props);
- }
-
- /*
- * Returns the Test server
- */
- public static String getSecondaryServer() {
- return System.getProperty("secondaryServer", TestUtil.getServer());
- }
-
- /*
- * Returns the Test port
- */
- public static int getSecondaryPort() {
- return Integer
- .parseInt(System.getProperty("secondaryPort", String.valueOf(TestUtil.getPort() + 1)));
- }
-
- /*
- * Returns the Test server
- */
- public static String getSecondaryServer2() {
- return System.getProperty("secondaryServer2", TestUtil.getServer());
- }
-
- /*
- * Returns the Test port
- */
- public static int getSecondaryPort2() {
- return Integer
- .parseInt(System.getProperty("secondaryPort2", String.valueOf(TestUtil.getPort() + 2)));
- }
-
- /*
- * The main entry point for JUnit
- */
- public static TestSuite suite() throws Exception {
- TestSuite suite = new TestSuite();
-
- try {
- Connection connection = openSecondaryDB();
- TestUtil.closeDB(connection);
-
- connection = openSecondaryDB2();
- TestUtil.closeDB(connection);
- } catch (PSQLException ex) {
- // replication instance is not available, but suite must have at lest one test case
- suite.addTestSuite(DummyTest.class);
- return suite;
- }
-
- suite.addTestSuite(MultiHostsConnectionTest.class);
- return suite;
- }
-
- public static class DummyTest extends TestCase {
- public DummyTest(String name) {
- super(name);
- }
-
- public void testDummy() {
- }
- }
+@RunWith(Suite.class)
+@Suite.SuiteClasses(MultiHostsConnectionTest.class)
+public class MultiHostTestSuite {
}
-
diff --git a/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostsConnectionTest.java b/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostsConnectionTest.java
index f945ab66..0b945ca6 100644
--- a/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostsConnectionTest.java
+++ b/pgjdbc/src/test/java/org/postgresql/test/hostchooser/MultiHostsConnectionTest.java
@@ -8,6 +8,11 @@ package org.postgresql.test.hostchooser;
import static java.lang.Integer.parseInt;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
import static org.postgresql.hostchooser.HostRequirement.any;
import static org.postgresql.hostchooser.HostRequirement.master;
import static org.postgresql.hostchooser.HostRequirement.preferSecondary;
@@ -22,7 +27,9 @@ import org.postgresql.test.TestUtil;
import org.postgresql.util.HostSpec;
import org.postgresql.util.PSQLException;
-import junit.framework.TestCase;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.Connection;
@@ -34,48 +41,95 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
-public class MultiHostsConnectionTest extends TestCase {
-
- static final String user = TestUtil.getUser();
- static final String password = TestUtil.getPassword();
- static final String master1 = TestUtil.getServer() + ":" + TestUtil.getPort();
- static final String secondary1 =
- MultiHostTestSuite.getSecondaryServer() + ":" + MultiHostTestSuite.getSecondaryPort();
- static final String secondary2 =
- MultiHostTestSuite.getSecondaryServer2() + ":" + MultiHostTestSuite.getSecondaryPort2();
- static final String fake1 = "127.127.217.217:1";
- static String masterIp;
- static String secondaryIP;
- static String secondaryIP2;
- static String fakeIp = fake1;
-
- static Connection con;
- private static Map<HostSpec, Object> hostStatusMap;
-
- static {
- try {
- Field field = GlobalHostStatusTracker.class.getDeclaredField("hostStatusMap");
- field.setAccessible(true);
- hostStatusMap = (Map<HostSpec, Object>) field.get(null);
+public class MultiHostsConnectionTest {
+
+ private static final String user = TestUtil.getUser();
+ private static final String password = TestUtil.getPassword();
+ private static final String master1 = TestUtil.getServer() + ":" + TestUtil.getPort();
+ private static final String secondary1 = getSecondaryServer() + ":" + getSecondaryPort();
+ private static final String secondary2 = getSecondaryServer2() + ":" + getSecondaryPort2();
+ private static final String fake1 = "127.127.217.217:1";
- con = TestUtil.openDB();
- masterIp = getRemoteHostSpec();
- closeDB(con);
+ private String masterIp;
+ private String secondaryIP;
+ private String secondaryIP2;
+ private Connection con;
+ private Map<HostSpec, Object> hostStatusMap;
+
+ @BeforeClass
+ public static void setUpClass() {
+ assumeTrue(isReplicationInstanceAvailable());
+ }
- con = MultiHostTestSuite.openSecondaryDB();
- secondaryIP = getRemoteHostSpec();
- closeDB(con);
+ @Before
+ public void setUp() throws Exception {
+ Field field = GlobalHostStatusTracker.class.getDeclaredField("hostStatusMap");
+ field.setAccessible(true);
+ hostStatusMap = (Map<HostSpec, Object>) field.get(null);
- con = MultiHostTestSuite.openSecondaryDB2();
- secondaryIP2 = getRemoteHostSpec();
- closeDB(con);
+ con = TestUtil.openDB();
+ masterIp = getRemoteHostSpec();
+ closeDB(con);
+
+ con = openSecondaryDB();
+ secondaryIP = getRemoteHostSpec();
+ closeDB(con);
+
+ con = openSecondaryDB2();
+ secondaryIP2 = getRemoteHostSpec();
+ closeDB(con);
+ }
+ private static boolean isReplicationInstanceAvailable() {
+ try {
+ Connection connection = openSecondaryDB();
+ closeDB(connection);
+ return true;
} catch (Exception e) {
- throw new RuntimeException(e);
+ return false;
}
}
- private static Connection getConnection(HostRequirement hostType, String... targets)
+ private static Connection openSecondaryDB() throws Exception {
+ TestUtil.initDriver();
+
+ Properties props = userAndPassword();
+
+ return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer(), getSecondaryPort()), props);
+ }
+
+ private static Properties userAndPassword() {
+ Properties props = new Properties();
+
+ props.setProperty("user", TestUtil.getUser());
+ props.setProperty("password", TestUtil.getPassword());
+ return props;
+ }
+
+ private static String getSecondaryServer() {
+ return System.getProperty("secondaryServer", TestUtil.getServer());
+ }
+
+ private static int getSecondaryPort() {
+ return Integer.parseInt(System.getProperty("secondaryPort", String.valueOf(TestUtil.getPort() + 1)));
+ }
+
+ private static Connection openSecondaryDB2() throws Exception {
+ TestUtil.initDriver();
+
+ Properties props = userAndPassword();
+ return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer2(), getSecondaryPort2()), props);
+ }
+
+ private static String getSecondaryServer2() {
+ return System.getProperty("secondaryServer2", TestUtil.getServer());
+ }
+
+ private static int getSecondaryPort2() {
+ return Integer.parseInt(System.getProperty("secondaryPort2", String.valueOf(TestUtil.getPort() + 2)));
+ }
+
+ private Connection getConnection(HostRequirement hostType, String... targets)
throws SQLException {
return getConnection(hostType, true, targets);
}
@@ -85,12 +139,12 @@ public class MultiHostsConnectionTest extends TestCase {
return new HostSpec(host.substring(0, split), parseInt(host.substring(split + 1)));
}
- private static Connection getConnection(HostRequirement hostType, boolean reset,
+ private Connection getConnection(HostRequirement hostType, boolean reset,
String... targets) throws SQLException {
return getConnection(hostType, reset, false, targets);
}
- private static Connection getConnection(HostRequirement hostType, boolean reset, boolean lb,
+ private Connection getConnection(HostRequirement hostType, boolean reset, boolean lb,
String... targets) throws SQLException {
TestUtil.closeDB(con);
@@ -119,11 +173,11 @@ public class MultiHostsConnectionTest extends TestCase {
return con = DriverManager.getConnection(sb.toString(), props);
}
- private static void assertRemote(String expectedHost) throws SQLException {
+ private void assertRemote(String expectedHost) throws SQLException {
assertEquals(expectedHost, getRemoteHostSpec());
}
- private static String getRemoteHostSpec() throws SQLException {
+ private String getRemoteHostSpec() throws SQLException {
ResultSet rs = con.createStatement()
.executeQuery("select inet_server_addr() || ':' || inet_server_port()");
rs.next();
@@ -136,7 +190,7 @@ public class MultiHostsConnectionTest extends TestCase {
return "off".equals(rs.getString(1));
}
- private static void assertGlobalState(String host, String status) {
+ private void assertGlobalState(String host, String status) {
HostSpec spec = hostSpec(host);
if (status == null) {
assertNull(hostStatusMap.get(spec));
@@ -145,11 +199,12 @@ public class MultiHostsConnectionTest extends TestCase {
}
}
- private static void resetGlobalState() {
+ private void resetGlobalState() {
hostStatusMap.clear();
}
- public static void testConnectToAny() throws SQLException {
+ @Test
+ public void testConnectToAny() throws SQLException {
getConnection(any, fake1, master1);
assertRemote(masterIp);
assertGlobalState(master1, "ConnectOK");
@@ -165,7 +220,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertGlobalState(fake1, "ConnectFail");
}
- public static void testConnectToMaster() throws SQLException {
+ @Test
+ public void testConnectToMaster() throws SQLException {
getConnection(master, true, fake1, master1, secondary1);
assertRemote(masterIp);
assertGlobalState(fake1, "ConnectFail");
@@ -179,7 +235,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertGlobalState(secondary1, "Secondary"); // was unknown, so tried to connect in order
}
- public static void testConnectToSlave() throws SQLException {
+ @Test
+ public void testConnectToSecondary() throws SQLException {
getConnection(secondary, true, fake1, secondary1, master1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
@@ -193,7 +250,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertGlobalState(master1, "Master"); // tried as it was unknown
}
- public static void testConnectToSlaveFirst() throws SQLException {
+ @Test
+ public void testConnectToSecondaryFirst() throws SQLException {
getConnection(preferSecondary, true, fake1, secondary1, master1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
@@ -213,7 +271,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertGlobalState(master1, "Master");
}
- public static void testFailedConnection() throws SQLException {
+ @Test
+ public void testFailedConnection() throws SQLException {
try {
getConnection(any, true, fake1);
fail();
@@ -221,7 +280,8 @@ public class MultiHostsConnectionTest extends TestCase {
}
}
- public static void testLoadBalancing() throws SQLException {
+ @Test
+ public void testLoadBalancing() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
boolean fake1FoundTried = false;
for (int i = 0; i < 20; ++i) {
@@ -237,7 +297,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertTrue("Never tried to connect to fake node", fake1FoundTried);
}
- public static void testLoadBalancing_preferSecondary() throws SQLException {
+ @Test
+ public void testLoadBalancing_preferSecondary() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
Set<HostSpec> tryConnectedHosts = new HashSet<HostSpec>();
for (int i = 0; i < 20; ++i) {
@@ -248,8 +309,7 @@ public class MultiHostsConnectionTest extends TestCase {
break;
}
}
- assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP,
- secondaryIP2)),
+ assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
assertEquals("Never tried to connect to fake node",4, tryConnectedHosts.size());
@@ -263,8 +323,7 @@ public class MultiHostsConnectionTest extends TestCase {
break;
}
}
- assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP,
- secondaryIP2)),
+ assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
// connect to master when there's no secondary
@@ -275,7 +334,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertRemote(masterIp);
}
- public static void testLoadBalancing_slave() throws SQLException {
+ @Test
+ public void testLoadBalancing_secondary() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
Set<HostSpec> tryConnectedHosts = new HashSet<HostSpec>();
for (int i = 0; i < 20; ++i) {
@@ -286,8 +346,7 @@ public class MultiHostsConnectionTest extends TestCase {
break;
}
}
- assertEquals("Did not connect to all secondary hosts", new HashSet<String>(asList(secondaryIP,
- secondaryIP2)),
+ assertEquals("Did not attempt to connect to all salve hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
assertEquals("Did not attempt to connect to master and fake node", 4, tryConnectedHosts.size());
@@ -301,12 +360,12 @@ public class MultiHostsConnectionTest extends TestCase {
break;
}
}
- assertEquals("Did not connect to all secondary hosts", new HashSet<String>(asList(secondaryIP,
- secondaryIP2)),
+ assertEquals("Did not connect to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
}
- public static void testHostRechecks() throws SQLException, InterruptedException {
+ @Test
+ public void testHostRechecks() throws SQLException, InterruptedException {
GlobalHostStatusTracker.reportHostStatus(hostSpec(master1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(secondary1), Master);
GlobalHostStatusTracker.reportHostStatus(hostSpec(fake1), Secondary);
@@ -327,7 +386,8 @@ public class MultiHostsConnectionTest extends TestCase {
assertRemote(masterIp);
}
- public static void testNoGoodHostsRechecksEverything() throws SQLException, InterruptedException {
+ @Test
+ public void testNoGoodHostsRechecksEverything() throws SQLException, InterruptedException {
GlobalHostStatusTracker.reportHostStatus(hostSpec(master1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(secondary1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(fake1), Secondary);
[parent: 9b6506dfa107]