1
1
'use strict' ;
2
2
3
+ const portfinder = require ( 'portfinder' ) ;
4
+ const assert = require ( 'assert' ) ;
3
5
const common = require ( '../../common' ) ;
6
+ const mysql = require ( '../../../index.js' ) ;
7
+
4
8
const connection = common . createConnection ( { debug : false } ) ;
5
- const assert = require ( 'assert' ) ;
9
+
10
+ console . log ( 'test query timeout' ) ;
6
11
7
12
connection . query ( { sql : 'SELECT sleep(3) as a' , timeout : 500 } , ( err , res ) => {
8
13
assert . equal ( res , null ) ;
@@ -35,16 +40,36 @@ connection.execute('SELECT sleep(1) as a', (err, res) => {
35
40
connection . end ( ) ;
36
41
} ) ;
37
42
38
- const connectionTimeout = common . createConnection ( {
39
- host : '10.255.255.1' ,
40
- debug : false ,
41
- connectTimeout : 100 ,
43
+ /**
44
+ * if connect timeout
45
+ * we should return connect timeout error instead of query timeout error
46
+ */
47
+ portfinder . getPort ( ( err , port ) => {
48
+ const server = mysql . createServer ( ) ;
49
+ server . on ( 'connection' , ( ) => {
50
+ // Let connection time out
51
+ } ) ;
52
+ server . listen ( port ) ;
53
+
54
+ const connectionTimeout = mysql . createConnection ( {
55
+ host : 'localhost' ,
56
+ port : port ,
57
+ connectTimeout : 1000 ,
58
+ } ) ;
59
+
60
+ // return connect timeout error first
61
+ connectionTimeout . query ( { sql : 'SELECT sleep(3) as a' , timeout : 50 } , ( err , res ) => {
62
+ console . log ( 'ok' ) ;
63
+ assert . equal ( res , null ) ;
64
+ assert . ok ( err ) ;
65
+ assert . equal ( err . code , 'ETIMEDOUT' ) ;
66
+ assert . equal ( err . message , 'connect ETIMEDOUT' ) ;
67
+ connectionTimeout . destroy ( ) ;
68
+ server . _server . close ( ) ;
69
+ } ) ;
42
70
} ) ;
43
71
44
- // return connect timeout error first
45
- connectionTimeout . query ( { sql : 'SELECT sleep(3) as a' , timeout : 50 } , ( err , res ) => {
46
- assert . equal ( res , null ) ;
47
- assert . ok ( err ) ;
48
- assert . equal ( err . code , 'ETIMEDOUT' ) ;
49
- assert . equal ( err . message , 'connect ETIMEDOUT' ) ;
72
+ process . on ( 'uncaughtException' , err => {
73
+ assert . equal ( err . message , 'Connection lost: The server closed the connection.' ) ;
74
+ assert . equal ( err . code , 'PROTOCOL_CONNECTION_LOST' ) ;
50
75
} ) ;
0 commit comments