Skip to content

Commit d65e51c

Browse files
author
chengyitian
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 66e2607 + 1be4f55 commit d65e51c

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

test/com/xxdb/DBConnectionTest.java

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import java.io.*;
1717
import java.lang.reflect.*;
1818
import java.math.BigDecimal;
19-
import java.net.InetAddress;
20-
import java.net.UnknownHostException;
19+
import java.net.*;
2120
import java.sql.*;
2221
import java.text.SimpleDateFormat;
2322
import java.time.*;
@@ -144,6 +143,128 @@ public void Test_Connect_5() throws IOException {
144143
boolean re = conn.connect(HOST,PORT,"","",ipports);
145144
Assert.assertEquals(true,re);
146145
}
146+
@Test
147+
public void Test_Connect_connectTimeout_negative() throws IOException {
148+
DBConnection conn = new DBConnection();
149+
conn.connect(HOST,PORT,-1,100);
150+
//期望报错
151+
}
152+
@Test
153+
public void Test_Connect_readTimeout_negative() throws IOException {
154+
DBConnection conn = new DBConnection();
155+
conn.connect(HOST,PORT,200,-100);
156+
//期望报错
157+
}
158+
@Test
159+
public void Test_Connect_connectTimeout_success() throws IOException {
160+
DBConnection conn = new DBConnection();
161+
boolean re = conn.connect(HOST,PORT,100,100);
162+
Assert.assertEquals(true,re);
163+
164+
boolean re1 = conn.connect(HOST,PORT,0,100);
165+
Assert.assertEquals(true,re1);
166+
}
167+
168+
@Test
169+
public void Test_Connect_connectTimeout_success_1() throws IOException {
170+
DBConnection conn = new DBConnection();
171+
boolean re = conn.connect(HOST,PORT,100,100,true);
172+
Assert.assertEquals(true,re);
173+
174+
boolean re1 = conn.connect(HOST,PORT,0,100,true);
175+
Assert.assertEquals(true,re1);
176+
}
177+
178+
@Test
179+
public void Test_Connect_connectTimeout_success_2() throws IOException {
180+
DBConnection conn = new DBConnection();
181+
boolean re = conn.connect(HOST,PORT,100,100,true,10);
182+
Assert.assertEquals(true,re);
183+
184+
boolean re1 = conn.connect(HOST,PORT,0,100,true,10);
185+
Assert.assertEquals(true,re1);
186+
}
187+
@Test
188+
public void Test_Connect_connectTimeout_fail() throws IOException {
189+
DBConnection conn = new DBConnection();
190+
long startTime = System.currentTimeMillis();
191+
boolean re = conn.connect(HOST,111,1000,100);
192+
long elapsedTime = System.currentTimeMillis() - startTime;
193+
System.out.println("Timeout after " + elapsedTime + " ms");
194+
Assert.assertEquals(false,re);
195+
Assert.assertEquals(true,elapsedTime>1000 && elapsedTime<1050);
196+
197+
long startTime1 = System.currentTimeMillis();
198+
boolean re1 = conn.connect(HOST,111,2000,100);
199+
long elapsedTime1 = System.currentTimeMillis() - startTime1;
200+
System.out.println("Timeout after " + elapsedTime1 + " ms");
201+
Assert.assertEquals(false,re1);
202+
Assert.assertEquals(true,elapsedTime1>2000 && elapsedTime1<2050);
203+
204+
long startTime2 = System.currentTimeMillis();
205+
boolean re2 = conn.connect(HOST,111,5,100);
206+
long elapsedTime2 = System.currentTimeMillis() - startTime2;
207+
System.out.println("Timeout after " + elapsedTime2 + " ms");
208+
Assert.assertEquals(false,re2);
209+
Assert.assertEquals(true,elapsedTime2>5 && elapsedTime2<50);
210+
}
211+
212+
@Test
213+
public void Test_Connect_readTimeout_fail() throws IOException, InterruptedException {
214+
DBConnection conn = new DBConnection();
215+
conn.connect(HOST,PORT,8000,2000);
216+
String re = null;
217+
long startTime = System.currentTimeMillis();
218+
try{
219+
conn.run("do{\n" +
220+
"print(\"ddd\")\n" +
221+
"}\n" +
222+
"while (true);");
223+
}catch(Exception e){
224+
re = e.getMessage();
225+
}
226+
long elapsedTime = System.currentTimeMillis() - startTime;
227+
System.out.println("Timeout after " + elapsedTime + " ms");
228+
Assert.assertEquals(true,elapsedTime>2000 && elapsedTime<2050);
229+
Assert.assertEquals("Failed to read response header from the socket with IO error Read timed out",re);
230+
conn.connect(HOST,PORT,8000,4000);
231+
String re1 = null;
232+
long startTime1 = System.currentTimeMillis();
233+
try{
234+
conn.run("do{\n" +
235+
"print(\"ddd\")\n" +
236+
"}\n" +
237+
"while (true);");
238+
}catch(Exception e){
239+
re1 = e.getMessage();
240+
}
241+
long elapsedTime2 = System.currentTimeMillis() - startTime1;
242+
System.out.println("Timeout after " + elapsedTime2 + " ms");
243+
Assert.assertEquals(true,elapsedTime2>4000 && elapsedTime2<4050);
244+
Assert.assertEquals("Failed to read response header from the socket with IO error Read timed out",re1);
245+
}
246+
247+
@Test
248+
public void Test_Connect_readTimeout_fail_1() throws IOException, InterruptedException {
249+
DBConnection conn = new DBConnection();
250+
conn.connect(HOST,PORT,8000,2000,true,7);
251+
String re = null;
252+
long startTime = System.currentTimeMillis();
253+
try{
254+
conn.run("do{\n" +
255+
"print(\"ddd\")\n" +
256+
"}\n" +
257+
"while (true);");
258+
}catch(Exception e){
259+
re = e.getMessage();
260+
}
261+
long elapsedTime = System.currentTimeMillis() - startTime;
262+
System.out.println("Timeout after " + elapsedTime + " ms");
263+
//Assert.assertEquals(true,elapsedTime>2000 && elapsedTime<2050);
264+
//Assert.assertEquals("Failed to read response header from the socket with IO error Read timed out",re);
265+
System.out.println(re);
266+
}
267+
147268
@Test
148269
public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_false_enableLoadBalance_false() throws IOException {
149270
int port=7102;

0 commit comments

Comments
 (0)