1616
1717@ Test (timeOut = 10000 , groups = "MULTI_HOST" )
1818public class TestMultiHost {
19- private final String DEFAULT_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default" ;
20- private final String RANDOM_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default?load_balancing_policy=random" ;
21- private final String ROUND_ROBIN_JDBC_URL = "jdbc:databend://localhost:8001,localhost:8002,localhost:8003/default?load_balancing_policy=round_robin" ;
22- private final String FAIL_OVER_JDBC_URL = "jdbc:databend://localhost:7222,localhost:7223,localhost:7224,localhost:8001/default?load_balancing_policy=round_robin&max_failover_retry=4" ;
2319 private final String AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8001/default?load_balancing_policy=round_robin&auto_discovery=true" ;
2420 private final String UNSUPPORT_AUTO_DISCOVERY_JDBC_URL = "jdbc:databend://localhost:8001/default?load_balancing_policy=round_robin&auto_discovery=true&enable_mock=true" ;
2521
@@ -29,85 +25,6 @@ private Connection createConnection(String url)
2925 return DriverManager .getConnection (url , "databend" , "databend" );
3026 }
3127
32- @ Test (groups = {"IT" , "MULTI_HOST" })
33- public void testDefaultLoadBalancing ()
34- throws SQLException {
35- HashMap <Integer , Integer > expect = new HashMap <>();
36- expect .put (8001 , 90 );
37-
38- HashMap <Integer , Integer > actual = get_hosts_used (DEFAULT_JDBC_URL );
39- Assert .assertEquals (expect , actual );
40- }
41-
42- @ Test (groups = {"IT" , "MULTI_HOST" })
43- public void testRandomLoadBalancing ()
44- throws SQLException {
45- HashMap <Integer , Integer > actual = get_hosts_used (RANDOM_JDBC_URL );
46-
47- int node8001 = actual .get (8001 );
48- int node8002 = actual .get (8002 );
49- int node8003 = actual .get (8003 );
50-
51- Assert .assertTrue (node8001 > 0 && node8002 > 0 && node8003 > 0 , "got " + actual );
52- Assert .assertEquals (node8001 + node8002 + node8003 , 90 , "got " + actual );
53- }
54-
55- @ Test (groups = {"IT" , "MULTI_HOST" })
56- public void testRoundRobinLoadBalancing ()
57- throws SQLException {
58- HashMap <Integer , Integer > expect = new HashMap <>();
59- expect .put (8001 , 30 );
60- expect .put (8002 , 30 );
61- expect .put (8003 , 30 );
62-
63- HashMap <Integer , Integer > actual = get_hosts_used (ROUND_ROBIN_JDBC_URL );
64- Assert .assertEquals (expect , actual );
65- }
66-
67- @ Test (groups = {"IT" , "MULTI_HOST" })
68- public void testRoundRobinTransaction ()
69- throws SQLException {
70- // try to connect with three nodes 1000 times and count for each node
71- try (Connection connection = createConnection (ROUND_ROBIN_JDBC_URL )) {
72- DatabendStatement statement = (DatabendStatement ) connection .createStatement ();
73- statement .execute ("drop table if exists test_transaction;" );
74- statement .execute ("create table if not exists test_transaction(id int);" );
75-
76- }
77- for (int i = 0 ; i < 30 ; i ++) {
78- try (Connection connection = createConnection (ROUND_ROBIN_JDBC_URL )) {
79- DatabendStatement statement = (DatabendStatement ) connection .createStatement ();
80- // use transaction select a table, drop a table, insert data into table bring i index
81- statement .execute ("begin;" );
82- statement .execute ("insert into test_transaction values(" + i + ");" );
83- statement .execute ("select * from test_transaction;" );
84- statement .execute ("commit;" );
85- }
86- }
87-
88- // query on test
89- try (Connection connection = createConnection (ROUND_ROBIN_JDBC_URL )) {
90- DatabendStatement statement = (DatabendStatement ) connection .createStatement ();
91- statement .execute ("select * from test_transaction;" );
92- ResultSet r = statement .getResultSet ();
93- int count = 0 ;
94- while (r .next ()) {
95- count ++;
96- }
97- Assert .assertEquals (count , 30 );
98- }
99- }
100- // @Test(groups = {"IT", "MULTI_HOST"})
101- // skip since getConnection not support multihost for now
102- // public void testFailOver()
103- // throws SQLException {
104- // HashMap<Integer, Integer> expect = new HashMap<>();
105- // expect.put(8001, 90);
106- //
107- // HashMap<Integer, Integer> actual = get_hosts_used(FAIL_OVER_JDBC_URL);
108- // Assert.assertEquals(expect, actual);
109- // }
110-
11128 @ Test (groups = {"IT" , "MULTI_HOST" })
11229 public void testAutoDiscovery ()
11330 throws SQLException {
@@ -136,18 +53,20 @@ public void testUnSupportedAutoDiscovery()
13653 @ Test (groups = {"UNIT" })
13754 public void testAutoDiscoveryUriParsing () throws SQLException {
13855 DatabendDriverUri uri = DatabendDriverUri .create ("jdbc:databend://localhost:8001?ssl=true" , null );
139- DatabendDriverUri uri2 = DatabendDriverUri .create ("jdbc:databend://127.0.0.1:8001,127.0.0.1:8002,127.0.0.1:8003?ssl=true" , null );
140- List <URI > uris2 = uri2 .getNodes ().getUris ();
141-
14256 DatabendNodes nodes = uri .getNodes ();
14357 List <DiscoveryNode > discoveryNodes = new ArrayList <>();
14458 discoveryNodes .add (DiscoveryNode .create ("127.0.0.1:8001" ));
14559 discoveryNodes .add (DiscoveryNode .create ("127.0.0.1:8002" ));
14660 discoveryNodes .add (DiscoveryNode .create ("127.0.0.1:8003" ));
14761 List <URI > uris = nodes .parseURI (discoveryNodes );
14862 Assert .assertEquals (uris .size (), 3 );
149- Assert .assertEquals (uris2 .size (), 3 );
150- Assert .assertEquals (uris2 , uris );
63+ Assert .assertEquals (uris .get (0 ).getScheme (), "https" );
64+ Assert .assertEquals (uris .get (0 ).getHost (), "127.0.0.1" );
65+ Assert .assertEquals (uris .get (0 ).getPath (), "" );
66+ Assert .assertEquals (uris .get (0 ).getQuery (), "ssl=true" );
67+ Assert .assertEquals (uris .get (0 ).getPort (), 8001 );
68+ Assert .assertEquals (uris .get (1 ).getPort (), 8002 );
69+ Assert .assertEquals (uris .get (2 ).getPort (), 8003 );
15170 }
15271
15372 private HashMap <Integer , Integer > get_hosts_used (String dsn ) throws SQLException {
0 commit comments