|
8 | 8 | import com.xxdb.io.Long2;
|
9 | 9 | import com.xxdb.io.ProgressListener;
|
10 | 10 | import org.junit.*;
|
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
11 | 13 |
|
12 |
| -import java.io.BufferedOutputStream; |
13 |
| -import java.io.File; |
14 |
| -import java.io.FileOutputStream; |
15 |
| -import java.io.IOException; |
| 14 | +import java.io.*; |
16 | 15 | import java.lang.reflect.*;
|
17 | 16 | import java.math.BigDecimal;
|
18 | 17 | import java.net.InetAddress;
|
@@ -46,7 +45,7 @@ public class DBConnectionTest {
|
46 | 45 | static int[] port_list = Arrays.stream(bundle.getString("PORTS").split(",")).mapToInt(Integer::parseInt).toArray();
|
47 | 46 | private double load = -1.0;
|
48 | 47 | public int getConnCount() throws IOException {
|
49 |
| - return ((BasicInt) conn.run("(exec connectionNum from rpc(getControllerAlias(),getClusterPerf) where port = getNodePort())[0]")).getInt(); |
| 48 | + return ((BasicInt) conn.run("(exec connectionNum from rpc(getControllerAlias(),getClusterPerf) where PORT = getNodePort())[0]")).getInt(); |
50 | 49 | }
|
51 | 50 | static void compareBasicTable(BasicTable table, BasicTable newTable)
|
52 | 51 | {
|
@@ -142,6 +141,163 @@ public void Test_Connect_5() throws IOException {
|
142 | 141 | boolean re = conn.connect(HOST,PORT,"","",ipports);
|
143 | 142 | Assert.assertEquals(true,re);
|
144 | 143 | }
|
| 144 | + @Test |
| 145 | + public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_false_enableLoadBalance_false() throws IOException { |
| 146 | + int port=7102; |
| 147 | + int trynums=3; |
| 148 | + DBConnection conn =new DBConnection(); |
| 149 | + String R=""; |
| 150 | + try { |
| 151 | + conn.connect(HOST,port,0,true,trynums); |
| 152 | + }catch (Exception e){ |
| 153 | + R=e.toString(); |
| 154 | + } |
| 155 | + assertEquals("java.lang.RuntimeException: Connect to "+HOST+":"+port+" failed after "+trynums+" reconnect attemps.",R); |
| 156 | + } |
| 157 | + @Test |
| 158 | + public void test_Connect_tryReconnectNums_Success_enableHighAvailability_false_enableLoadBalance_false() throws IOException { |
| 159 | + class LogCapture { |
| 160 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 161 | + private final PrintStream originalErr = System.err; |
| 162 | + public void start() { |
| 163 | + System.setErr(new PrintStream(baos)); |
| 164 | + } |
| 165 | + public void stop() { |
| 166 | + System.setErr(originalErr); |
| 167 | + } |
| 168 | + public String getLogMessages() { |
| 169 | + return baos.toString(); |
| 170 | + } |
| 171 | + } |
| 172 | + int trynums=3; |
| 173 | + LogCapture logCapture = new LogCapture(); |
| 174 | + logCapture.start(); |
| 175 | + DBConnection conn =new DBConnection(); |
| 176 | + conn.connect(HOST,PORT,0,true,trynums); |
| 177 | + logCapture.stop(); |
| 178 | + String s=logCapture.getLogMessages(); |
| 179 | + assertTrue(s.contains("Connect to "+HOST+":"+PORT+" successfully.")); |
| 180 | + } |
| 181 | + @Test |
| 182 | + public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_true_enableLoadBalance_false() throws IOException { |
| 183 | + class LogCapture { |
| 184 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 185 | + private final PrintStream originalErr = System.err; |
| 186 | + public void start() { |
| 187 | + System.setErr(new PrintStream(baos)); |
| 188 | + } |
| 189 | + public void stop() { |
| 190 | + System.setErr(originalErr); |
| 191 | + } |
| 192 | + public String getLogMessages() { |
| 193 | + return baos.toString(); |
| 194 | + } |
| 195 | + } |
| 196 | + int port=7102; |
| 197 | + int trynums=3; |
| 198 | + String[] N={}; |
| 199 | + DBConnection conn =new DBConnection(); |
| 200 | + LogCapture logCapture = new LogCapture(); |
| 201 | + logCapture.start(); |
| 202 | + conn.connect(HOST,port,"admin","123456","",true,N,true,false,trynums); |
| 203 | + logCapture.stop(); |
| 204 | + String s=logCapture.getLogMessages(); |
| 205 | + String searchString = "拒绝连接"; |
| 206 | + int actualCount = 0; |
| 207 | + int fromIndex = 0; |
| 208 | + while ((fromIndex = s.indexOf(searchString, fromIndex)) != -1) { |
| 209 | + actualCount++; |
| 210 | + fromIndex++; |
| 211 | + } |
| 212 | + assertEquals(trynums,actualCount); |
| 213 | + } |
| 214 | + @Test |
| 215 | + public void test_Connect_tryReconnectNums_Success_enableHighAvailability_true_enableLoadBalance_false() throws IOException { |
| 216 | + class LogCapture { |
| 217 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 218 | + private final PrintStream originalErr = System.err; |
| 219 | + public void start() { |
| 220 | + System.setErr(new PrintStream(baos)); |
| 221 | + } |
| 222 | + public void stop() { |
| 223 | + System.setErr(originalErr); |
| 224 | + } |
| 225 | + public String getLogMessages() { |
| 226 | + return baos.toString(); |
| 227 | + } |
| 228 | + } |
| 229 | + int port=7102; |
| 230 | + int trynums=3; |
| 231 | + DBConnection conn =new DBConnection(); |
| 232 | + LogCapture logCapture = new LogCapture(); |
| 233 | + logCapture.start(); |
| 234 | + conn.connect(HOST,port,"admin","123456","",true,ipports,true,false,trynums); |
| 235 | + logCapture.stop(); |
| 236 | + String s=logCapture.getLogMessages(); |
| 237 | + assertTrue(s.contains("successfully")); |
| 238 | + assertFalse(s.contains("Switch to node:")); |
| 239 | + |
| 240 | + } |
| 241 | + @Test |
| 242 | + public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_true_enableLoadBalance_true() throws IOException { |
| 243 | + class LogCapture { |
| 244 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 245 | + private final PrintStream originalErr = System.err; |
| 246 | + public void start() { |
| 247 | + System.setErr(new PrintStream(baos)); |
| 248 | + } |
| 249 | + public void stop() { |
| 250 | + System.setErr(originalErr); |
| 251 | + } |
| 252 | + public String getLogMessages() { |
| 253 | + return baos.toString(); |
| 254 | + } |
| 255 | + } |
| 256 | + int port=7102; |
| 257 | + int trynums=3; |
| 258 | + String[] N={}; |
| 259 | + DBConnection conn =new DBConnection(); |
| 260 | + LogCapture logCapture = new LogCapture(); |
| 261 | + logCapture.start(); |
| 262 | + conn.connect(HOST,port,"admin","123456","",true,N,true,true,trynums); |
| 263 | + logCapture.stop(); |
| 264 | + String s=logCapture.getLogMessages(); |
| 265 | + String searchString = "拒绝连接"; |
| 266 | + int actualCount = 0; |
| 267 | + int fromIndex = 0; |
| 268 | + while ((fromIndex = s.indexOf(searchString, fromIndex)) != -1) { |
| 269 | + actualCount++; |
| 270 | + fromIndex++; |
| 271 | + } |
| 272 | + assertEquals(trynums,actualCount); |
| 273 | + } |
| 274 | + @Test |
| 275 | + public void test_Connect_tryReconnectNums_Success_enableHighAvailability_true_enableLoadBalance_true() throws IOException { |
| 276 | + class LogCapture { |
| 277 | + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 278 | + private final PrintStream originalErr = System.err; |
| 279 | + public void start() { |
| 280 | + System.setErr(new PrintStream(baos)); |
| 281 | + } |
| 282 | + public void stop() { |
| 283 | + System.setErr(originalErr); |
| 284 | + } |
| 285 | + public String getLogMessages() { |
| 286 | + return baos.toString(); |
| 287 | + } |
| 288 | + } |
| 289 | + int trynums=3; |
| 290 | + int port=7102; |
| 291 | + DBConnection conn =new DBConnection(); |
| 292 | + LogCapture logCapture = new LogCapture(); |
| 293 | + logCapture.start(); |
| 294 | + conn.connect(HOST,port,"admin","123456","",true,ipports,true,true,trynums); |
| 295 | + logCapture.stop(); |
| 296 | + String s=logCapture.getLogMessages(); |
| 297 | + |
| 298 | + assertTrue(s.contains("Switch to node:")); |
| 299 | + |
| 300 | + } |
145 | 301 |
|
146 | 302 | @Test
|
147 | 303 | public void Test_Connect_initialScript() throws IOException {
|
|
0 commit comments