@@ -119,6 +119,16 @@ public void daimaTest_Connect_timeout_1() throws IOException {
119
119
DBConnection conn = new DBConnection ();
120
120
boolean re = conn .connect (HOST ,111 ,1 ,true );
121
121
}
122
+ //@Test AJ-770
123
+ public void Test_Connect_timeout_111 () throws IOException , InterruptedException {
124
+ DBConnection conn = new DBConnection ();
125
+ boolean re = conn .connect (HOST ,PORT ,1 ,true ,10 );
126
+ System .out .println ("停止停止节点 " );
127
+ //手工停止节点
128
+ Thread .sleep (10000 );
129
+ System .out .println ("run....." );
130
+ conn .run ("re=1" );
131
+ }
122
132
@ Test
123
133
public void Test_Connect_2 () throws IOException {
124
134
DBConnection conn = new DBConnection ();
@@ -145,15 +155,77 @@ public void Test_Connect_5() throws IOException {
145
155
}
146
156
@ Test
147
157
public void Test_Connect_connectTimeout_negative () throws IOException {
158
+ class LogCapture {
159
+ private final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
160
+ private final PrintStream originalErr = System .err ;
161
+ public void start () {
162
+ System .setErr (new PrintStream (baos ));
163
+ }
164
+ public void stop () {
165
+ System .setErr (originalErr );
166
+ }
167
+ public String getLogMessages () {
168
+ return baos .toString ();
169
+ }
170
+ }
148
171
DBConnection conn = new DBConnection ();
172
+ LogCapture logCapture = new LogCapture ();
173
+ logCapture .start ();
149
174
conn .connect (HOST ,PORT ,-1 ,100 );
150
- //期望报错
175
+ logCapture .stop ();
176
+ String s =logCapture .getLogMessages ();
177
+ assertTrue (s .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
178
+
179
+ LogCapture logCapture1 = new LogCapture ();
180
+ logCapture1 .start ();
181
+ conn .connect (HOST ,PORT ,-1 ,100 ,true );
182
+ logCapture1 .stop ();
183
+ String s1 =logCapture1 .getLogMessages ();
184
+ assertTrue (s1 .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
185
+
186
+ LogCapture logCapture2 = new LogCapture ();
187
+ logCapture2 .start ();
188
+ conn .connect (HOST ,PORT ,-1 ,100 ,true ,10 );
189
+ logCapture2 .stop ();
190
+ String s2 =logCapture2 .getLogMessages ();
191
+ assertTrue (s2 .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
151
192
}
152
193
@ Test
153
194
public void Test_Connect_readTimeout_negative () throws IOException {
195
+ class LogCapture {
196
+ private final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
197
+ private final PrintStream originalErr = System .err ;
198
+ public void start () {
199
+ System .setErr (new PrintStream (baos ));
200
+ }
201
+ public void stop () {
202
+ System .setErr (originalErr );
203
+ }
204
+ public String getLogMessages () {
205
+ return baos .toString ();
206
+ }
207
+ }
154
208
DBConnection conn = new DBConnection ();
209
+ LogCapture logCapture = new LogCapture ();
210
+ logCapture .start ();
155
211
conn .connect (HOST ,PORT ,200 ,-100 );
156
- //期望报错
212
+ logCapture .stop ();
213
+ String s =logCapture .getLogMessages ();
214
+ assertTrue (s .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
215
+
216
+ LogCapture logCapture1 = new LogCapture ();
217
+ logCapture1 .start ();
218
+ conn .connect (HOST ,PORT ,500 ,-100 ,true );
219
+ logCapture1 .stop ();
220
+ String s1 =logCapture1 .getLogMessages ();
221
+ assertTrue (s1 .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
222
+
223
+ LogCapture logCapture2 = new LogCapture ();
224
+ logCapture2 .start ();
225
+ conn .connect (HOST ,PORT ,500 ,-100 ,true ,10 );
226
+ logCapture2 .stop ();
227
+ String s2 =logCapture2 .getLogMessages ();
228
+ assertTrue (s2 .contains ("The param connectTimeout or readTimeout cannot less than zero." ));
157
229
}
158
230
@ Test
159
231
public void Test_Connect_connectTimeout_success () throws IOException {
@@ -209,62 +281,133 @@ public void Test_Connect_connectTimeout_fail() throws IOException {
209
281
Assert .assertEquals (true ,elapsedTime2 >5 && elapsedTime2 <50 );
210
282
}
211
283
284
+ @ Test
285
+ public void Test_Connect_connectTimeout_fail_1 () throws IOException {
286
+ DBConnection conn = new DBConnection ();
287
+ long startTime1 = System .currentTimeMillis ();
288
+ String re1 = null ;
289
+ try {
290
+ conn .connect (HOST ,111 ,500 ,100 ,true ,2 );
291
+ }catch (Exception e ){
292
+ re1 = e .getMessage ();
293
+ }
294
+ long elapsedTime1 = System .currentTimeMillis () - startTime1 ;
295
+ System .out .println ("Timeout after " + elapsedTime1 + " ms" );
296
+ Assert .assertEquals (true ,elapsedTime1 >1500 && elapsedTime1 <4000 );
297
+
298
+ //DBConnection conn1 = new DBConnection();
299
+ long startTime = System .currentTimeMillis ();
300
+ String re = null ;
301
+ try {
302
+ conn .connect (HOST ,111 ,2000 ,100 ,true ,2 );
303
+ }catch (Exception e ){
304
+ re = e .getMessage ();
305
+ }
306
+ long elapsedTime = System .currentTimeMillis () - startTime ;
307
+ System .out .println ("Timeout after " + elapsedTime + " ms" );
308
+ Assert .assertEquals (true ,elapsedTime >6000 && elapsedTime <8000 );
309
+ }
310
+ //@Test//没有设置重连次数,会一直重连
311
+ public void Test_Connect_connectTimeout_fail_2 () throws IOException {
312
+ DBConnection conn = new DBConnection ();
313
+ long startTime1 = System .currentTimeMillis ();
314
+ String re1 = null ;
315
+ try {
316
+ conn .connect (HOST ,111 ,500 ,100 ,true );
317
+ }catch (Exception e ){
318
+ re1 = e .getMessage ();
319
+ }
320
+ long elapsedTime1 = System .currentTimeMillis () - startTime1 ;
321
+ System .out .println ("Timeout after " + elapsedTime1 + " ms" );
322
+ Assert .assertEquals (true ,elapsedTime1 >1500 && elapsedTime1 <4000 );
323
+ }
324
+
212
325
@ Test
213
326
public void Test_Connect_readTimeout_fail () throws IOException , InterruptedException {
214
327
DBConnection conn = new DBConnection ();
215
328
conn .connect (HOST ,PORT ,8000 ,2000 );
216
329
String re = null ;
217
330
long startTime = System .currentTimeMillis ();
218
331
try {
219
- conn .run ("do{\n " +
220
- "print(\" ddd\" )\n " +
332
+ conn .run ("re = time(now())\n " +
333
+ "do{\n " +
334
+ "sleep(1000)\n " +
335
+ "re1=time(now())\n " +
221
336
"}\n " +
222
- "while (true );" );
337
+ "while ((re1-re)/1000<5 );" );
223
338
}catch (Exception e ){
224
339
re = e .getMessage ();
225
340
}
226
341
long elapsedTime = System .currentTimeMillis () - startTime ;
227
342
System .out .println ("Timeout after " + elapsedTime + " ms" );
228
343
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 );
344
+ Assert .assertEquals ("Read timed out" ,re );
230
345
conn .connect (HOST ,PORT ,8000 ,4000 );
231
346
String re1 = null ;
232
347
long startTime1 = System .currentTimeMillis ();
233
348
try {
234
- conn .run ("do{\n " +
235
- "print(\" ddd\" )\n " +
349
+ conn .run ("re = time(now())\n " +
350
+ "do{\n " +
351
+ "sleep(1000)\n " +
352
+ "re1=time(now())\n " +
236
353
"}\n " +
237
- "while (true );" );
354
+ "while ((re1-re)/1000<5 );" );
238
355
}catch (Exception e ){
239
356
re1 = e .getMessage ();
240
357
}
241
358
long elapsedTime2 = System .currentTimeMillis () - startTime1 ;
242
359
System .out .println ("Timeout after " + elapsedTime2 + " ms" );
243
360
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 );
361
+ Assert .assertEquals ("Read timed out" ,re1 );
245
362
}
246
363
247
364
@ Test
248
365
public void Test_Connect_readTimeout_fail_1 () throws IOException , InterruptedException {
249
- DBConnection conn = new DBConnection ();
250
- conn .connect (HOST ,PORT ,8000 ,2000 ,true ,7 );
366
+ DBConnection conn1 = new DBConnection ();
367
+ conn1 .connect (HOST ,PORT ,8000 ,2000 ,true ,2 );
251
368
String re = null ;
369
+ System .out .println ("TESTFFFFFF" );
252
370
long startTime = System .currentTimeMillis ();
253
371
try {
254
- conn .run ("do{\n " +
255
- "print(\" ddd\" )\n " +
372
+ conn1 .run ("re = time(now())\n " +
373
+ "do{\n " +
374
+ "sleep(1000)\n " +
375
+ "re1=time(now())\n " +
256
376
"}\n " +
257
- "while (true );" );
377
+ "while ((re1-re)/1000<10 );" );
258
378
}catch (Exception e ){
259
379
re = e .getMessage ();
260
380
}
261
381
long elapsedTime = System .currentTimeMillis () - startTime ;
262
382
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 );
383
+ Assert .assertEquals (true ,elapsedTime >2000 );
384
+ Assert .assertEquals (true , re . contains ( " read timed out." ) );
265
385
System .out .println (re );
266
386
}
267
387
388
+ @ Test
389
+ public void Test_Connect_readTimeout_fail_2 () throws IOException , InterruptedException {
390
+ DBConnection conn1 = new DBConnection ();
391
+ conn1 .connect (HOST ,PORT ,8000 ,2000 ,true );
392
+ String re = null ;
393
+ System .out .println ("TESTFFFFFF" );
394
+ long startTime = System .currentTimeMillis ();
395
+ try {
396
+ conn1 .run ("re = time(now())\n " +
397
+ "do{\n " +
398
+ "sleep(1000)\n " +
399
+ "re1=time(now())\n " +
400
+ "}\n " +
401
+ "while ((re1-re)/1000<10);" );
402
+ }catch (Exception e ){
403
+ re = e .getMessage ();
404
+ }
405
+ long elapsedTime = System .currentTimeMillis () - startTime ;
406
+ System .out .println ("Timeout after " + elapsedTime + " ms" );
407
+ Assert .assertEquals (true ,elapsedTime >2000 );
408
+ Assert .assertEquals (true ,re .contains ("read timed out." ));
409
+ System .out .println (re );
410
+ }
268
411
@ Test
269
412
public void test_Connect_tryReconnectNums_Filed_enableHighAvailability_false_enableLoadBalance_false () throws IOException {
270
413
int port =7102 ;
0 commit comments