Skip to content

Commit 96e6a5b

Browse files
authored
ci: fix infinite loop on wait-up step (#2348)
1 parent 2419c49 commit 96e6a5b

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

test/common.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ const config = {
99
password: (process.env.CI ? process.env.MYSQL_PASSWORD : '') || '',
1010
database: process.env.MYSQL_DATABASE || 'test',
1111
compress: process.env.MYSQL_USE_COMPRESSION,
12-
port: process.env.MYSQL_PORT || 3306
12+
port: process.env.MYSQL_PORT || 3306,
1313
};
1414

1515
if (process.env.MYSQL_USE_TLS === '1') {
1616
config.ssl = {
1717
rejectUnauthorized: false,
1818
ca: fs.readFileSync(
1919
path.join(__dirname, '../test/fixtures/ssl/certs/ca.pem'),
20-
'utf-8'
21-
)
20+
'utf-8',
21+
),
2222
};
2323
}
2424

@@ -27,37 +27,58 @@ const configURI = `mysql://${config.user}:${config.password}@${config.host}:${co
2727
exports.SqlString = require('sqlstring');
2828
exports.config = config;
2929

30-
exports.waitDatabaseReady = function(callback) {
30+
exports.waitDatabaseReady = function (callback) {
3131
const start = Date.now();
32-
const tryConnect = function() {
33-
const conn = exports.createConnection({ database: 'mysql', password: process.env.MYSQL_PASSWORD });
32+
const timeout = 300000; // 5 minutes in milliseconds
33+
34+
const tryConnect = function () {
35+
if (Date.now() - start > timeout) {
36+
console.log('Connection attempt timed out after 5 minutes.');
37+
process.exit(1);
38+
}
39+
40+
const conn = exports.createConnection({
41+
database: 'mysql',
42+
password: process.env.MYSQL_PASSWORD,
43+
});
44+
3445
conn.once('error', err => {
35-
if (err.code !== 'PROTOCOL_CONNECTION_LOST' && err.code !== 'ETIMEDOUT' && err.code !== 'ECONNREFUSED') {
46+
if (
47+
err.code !== 'PROTOCOL_CONNECTION_LOST' &&
48+
err.code !== 'ETIMEDOUT' &&
49+
err.code !== 'ECONNREFUSED'
50+
) {
3651
console.log('Unexpected error waiting for connection', err);
3752
process.exit(-1);
3853
}
54+
3955
try {
4056
conn.close();
4157
} catch (err) {
4258
console.log(err);
4359
}
60+
4461
console.log('not ready');
4562
setTimeout(tryConnect, 1000);
4663
});
64+
4765
conn.once('connect', () => {
4866
console.log(`ready after ${Date.now() - start}ms!`);
4967
conn.close();
5068
callback();
5169
});
5270
};
71+
5372
tryConnect();
5473
};
5574

56-
exports.createConnection = function(args) {
57-
75+
exports.createConnection = function (args) {
5876
const driver = require('../index.js');
5977
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
60-
return driver.createConnection({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
78+
return driver.createConnection({
79+
...args,
80+
uri: process.env.MYSQL_CONNECTION_URL,
81+
});
6182
}
6283

6384
if (!args) {
@@ -91,7 +112,7 @@ exports.createConnection = function(args) {
91112
return conn;
92113
};
93114

94-
exports.getConfig = function(input) {
115+
exports.getConfig = function (input) {
95116
const args = input || {};
96117
const params = {
97118
host: args.host || config.host,
@@ -118,10 +139,13 @@ exports.getConfig = function(input) {
118139
return params;
119140
};
120141

121-
exports.createPool = function(args) {
142+
exports.createPool = function (args) {
122143
let driver = require('../index.js');
123144
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
124-
return driver.createPool({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
145+
return driver.createPool({
146+
...args,
147+
uri: process.env.MYSQL_CONNECTION_URL,
148+
});
125149
}
126150

127151
if (!args) {
@@ -135,33 +159,36 @@ exports.createPool = function(args) {
135159
return driver.createPool(exports.getConfig(args));
136160
};
137161

138-
exports.createPoolCluster = function(args = {}) {
162+
exports.createPoolCluster = function (args = {}) {
139163
const driver = require('../index.js');
140164
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
141-
return driver.createPoolCluster({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
165+
return driver.createPoolCluster({
166+
...args,
167+
uri: process.env.MYSQL_CONNECTION_URL,
168+
});
142169
}
143-
return driver.createPoolCluster(args)
144-
}
170+
return driver.createPoolCluster(args);
171+
};
145172

146-
exports.createConnectionWithURI = function() {
173+
exports.createConnectionWithURI = function () {
147174
const driver = require('../index.js');
148175

149176
return driver.createConnection({ uri: configURI });
150177
};
151178

152-
exports.createTemplate = function() {
179+
exports.createTemplate = function () {
153180
const jade = require('jade');
154181
const template = require('fs').readFileSync(
155182
`${__dirname}/template.jade`,
156-
'ascii'
183+
'ascii',
157184
);
158185
return jade.compile(template);
159186
};
160187

161188
const ClientFlags = require('../lib/constants/client.js');
162189

163190
const portfinder = require('portfinder');
164-
exports.createServer = function(onListening, handler) {
191+
exports.createServer = function (onListening, handler) {
165192
const server = require('../index.js').createServer();
166193
server.on('connection', conn => {
167194
conn.on('error', () => {
@@ -178,7 +205,7 @@ exports.createServer = function(onListening, handler) {
178205
connectionId: 1234,
179206
statusFlags: 2,
180207
characterSet: 8,
181-
capabilityFlags: flags
208+
capabilityFlags: flags,
182209
});
183210
if (handler) {
184211
handler(conn);
@@ -190,6 +217,6 @@ exports.createServer = function(onListening, handler) {
190217
return server;
191218
};
192219

193-
exports.useTestDb = function() {
220+
exports.useTestDb = function () {
194221
// no-op in my setup, need it for compatibility with node-mysql tests
195222
};

0 commit comments

Comments
 (0)