Skip to content

Commit 72cc524

Browse files
author
haotian
committed
AJ-712:add test case about SimpleDBConnectionPool_TryReconnectNums and fix broken case
1 parent 1163ec7 commit 72cc524

File tree

2 files changed

+91
-95
lines changed

2 files changed

+91
-95
lines changed

test/com/xxdb/DBConnectionTest.java

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,6 @@ public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_false_ena
155155
assertEquals("java.lang.RuntimeException: Connect to "+HOST+":"+port+" failed after "+trynums+" reconnect attemps.",R);
156156
}
157157
@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
182158
public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_true_enableLoadBalance_false() throws IOException {
183159
class LogCapture {
184160
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -195,48 +171,15 @@ public String getLogMessages() {
195171
}
196172
int port=7102;
197173
int trynums=3;
198-
String[] N={};
174+
String[] N={"localhost:7300"};
199175
DBConnection conn =new DBConnection();
200176
LogCapture logCapture = new LogCapture();
201177
logCapture.start();
202178
conn.connect(HOST,port,"admin","123456","",true,N,true,false,trynums);
203179
logCapture.stop();
204180
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-
181+
int temp=trynums*(1+N.length);
182+
assertTrue(s.contains("Connect failed after "+temp+" reconnect attemps for every node in high availability sites."));
240183
}
241184
@Test
242185
public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_true_enableLoadBalance_true() throws IOException {
@@ -255,50 +198,18 @@ public String getLogMessages() {
255198
}
256199
int port=7102;
257200
int trynums=3;
258-
String[] N={};
201+
String[] N={"localhost:7300"};
259202
DBConnection conn =new DBConnection();
260203
LogCapture logCapture = new LogCapture();
261204
logCapture.start();
262205
conn.connect(HOST,port,"admin","123456","",true,N,true,true,trynums);
263206
logCapture.stop();
264207
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();
208+
int temp=trynums*(1+N.length);
209+
assertTrue(s.contains("Connect failed after "+temp+" reconnect attemps for every node in high availability sites."));
297210

298-
assertTrue(s.contains("Switch to node:"));
299211

300212
}
301-
302213
@Test
303214
public void Test_Connect_initialScript() throws IOException {
304215
DBConnection conn = new DBConnection();

test/com/xxdb/SimpleDBConnectionPoolTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import org.junit.After;
99
import org.junit.Before;
1010
import org.junit.Test;
11+
12+
import java.io.ByteArrayOutputStream;
1113
import java.io.IOException;
14+
import java.io.PrintStream;
1215
import java.math.BigDecimal;
1316
import java.time.LocalDate;
1417
import java.time.LocalDateTime;
@@ -18,6 +21,7 @@
1821
import java.util.concurrent.atomic.AtomicReference;
1922

2023
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertTrue;
2125

2226
public class SimpleDBConnectionPoolTest {
2327
private static SimpleDBConnectionPoolConfig config;
@@ -1202,6 +1206,87 @@ public void test_SimpleDBConnectionPool_getConnection_setLoadBalance() throws IO
12021206
assertEquals("The loadBalance configuration of connection in connection pool can only be set in SimpleDBConnectionPoolConfig.",re);
12031207
pool.close();
12041208
}
1209+
@Test
1210+
public void test_SimpleDBConnectionPool_getConnection_Failed_TryReconnectNums_enableHighAvailability_false_enableLoadBalance_false(){
1211+
int port=7100;
1212+
int trynums=3;
1213+
config.setMinimumPoolSize(1);
1214+
config.setTryReconnectNums(trynums);
1215+
config.setPort(port);
1216+
String s="";
1217+
try
1218+
{
1219+
pool = new SimpleDBConnectionPool(config);
1220+
}
1221+
catch (Exception e)
1222+
{
1223+
s=e.toString();
1224+
}
1225+
assertEquals("java.lang.RuntimeException: Create connection pool failure, because Connect to "+HOST+":"+port+" failed after "+trynums+" reconnect attemps.",s);
1226+
}
1227+
@Test(expected =RuntimeException.class)
1228+
public void test_SimpleDBConnectionPool_getConnection_Failed_TryReconnectNums_enableHighAvailability_true_enableLoadBalance_false(){
1229+
class LogCapture {
1230+
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
1231+
private final PrintStream originalErr = System.err;
1232+
public void start() {
1233+
System.setErr(new PrintStream(baos));
1234+
}
1235+
public void stop() {
1236+
System.setErr(originalErr);
1237+
}
1238+
public String getLogMessages() {
1239+
return baos.toString();
1240+
}
1241+
}
1242+
int port=7100;
1243+
int trynums=3;
1244+
config.setTryReconnectNums(trynums);
1245+
config.setMinimumPoolSize(2);
1246+
config.setPort(port);
1247+
config.setEnableHighAvailability(true);
1248+
config.setLoadBalance(false);
1249+
String []highAvailabilitySites={"localhost:7200"};
1250+
config.setHighAvailabilitySites(highAvailabilitySites);
1251+
LogCapture logCapture = new LogCapture();
1252+
logCapture.start();
1253+
pool = new SimpleDBConnectionPool(config);
1254+
logCapture.stop();
1255+
String s=logCapture.getLogMessages();
1256+
int temp=trynums*(1+highAvailabilitySites.length);
1257+
assertTrue(s.contains("Connect failed after "+temp+" reconnect attemps for every node in high availability sites."));
1258+
}
1259+
@Test(expected = RuntimeException.class)
1260+
public void test_SimpleDBConnectionPool_getConnection_Failed_TryReconnectNums_enableHighAvailability_true_enableLoadBalance_true(){
1261+
class LogCapture {
1262+
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
1263+
private final PrintStream originalErr = System.err;
1264+
public void start() {
1265+
System.setErr(new PrintStream(baos));
1266+
}
1267+
public void stop() {
1268+
System.setErr(originalErr);
1269+
}
1270+
public String getLogMessages() {
1271+
return baos.toString();
1272+
}
1273+
}
1274+
int port=7100;
1275+
int trynums=3;
1276+
config.setTryReconnectNums(trynums);
1277+
config.setMinimumPoolSize(1);
1278+
config.setPort(port);
1279+
config.setEnableHighAvailability(true);
1280+
config.setLoadBalance(true);
1281+
String []highAvailabilitySites={"localhost:7200","localhost:7300"};
1282+
config.setHighAvailabilitySites(highAvailabilitySites);
1283+
LogCapture logCapture = new LogCapture();
1284+
logCapture.start();
1285+
pool = new SimpleDBConnectionPool(config);
1286+
String s=logCapture.getLogMessages();
1287+
int temp=trynums*(1+highAvailabilitySites.length);
1288+
assertTrue(s.contains("Connect failed after "+temp+" reconnect attemps for every node in high availability sites."));
1289+
}
12051290

12061291
@Test
12071292
public void test_SimpleDBConnectionPool_upload_all_dateType() throws Exception {

0 commit comments

Comments
 (0)