Skip to content

Commit

Permalink
ci: fix infinite loop on wait-up step
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Jan 9, 2024
1 parent 2419c49 commit fbc970c
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const config = {
password: (process.env.CI ? process.env.MYSQL_PASSWORD : '') || '',
database: process.env.MYSQL_DATABASE || 'test',
compress: process.env.MYSQL_USE_COMPRESSION,
port: process.env.MYSQL_PORT || 3306
port: process.env.MYSQL_PORT || 3306,
};

if (process.env.MYSQL_USE_TLS === '1') {
config.ssl = {
rejectUnauthorized: false,
ca: fs.readFileSync(
path.join(__dirname, '../test/fixtures/ssl/certs/ca.pem'),
'utf-8'
)
'utf-8',
),
};
}

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

exports.waitDatabaseReady = function(callback) {
exports.waitDatabaseReady = function (callback) {
const start = Date.now();
const tryConnect = function() {
const conn = exports.createConnection({ database: 'mysql', password: process.env.MYSQL_PASSWORD });
const timeout = 300000; // 5 minutes in milliseconds

const tryConnect = function () {
if (Date.now() - start > timeout) {
console.log('Connection attempt timed out after 5 minutes.');
process.exit(1);
}

const conn = exports.createConnection({
database: 'mysql',
password: process.env.MYSQL_PASSWORD,
});

conn.once('error', err => {
if (err.code !== 'PROTOCOL_CONNECTION_LOST' && err.code !== 'ETIMEDOUT' && err.code !== 'ECONNREFUSED') {
if (
err.code !== 'PROTOCOL_CONNECTION_LOST' &&
err.code !== 'ETIMEDOUT' &&
err.code !== 'ECONNREFUSED'
) {
console.log('Unexpected error waiting for connection', err);
process.exit(-1);
}

try {
conn.close();
} catch (err) {
console.log(err);
}

console.log('not ready');
setTimeout(tryConnect, 1000);
});

conn.once('connect', () => {
console.log(`ready after ${Date.now() - start}ms!`);
conn.close();
callback();
});
};

tryConnect();
};

exports.createConnection = function(args) {

exports.createConnection = function (args) {
const driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createConnection({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
return driver.createConnection({
...args,
uri: process.env.MYSQL_CONNECTION_URL,
});
}

if (!args) {
Expand Down Expand Up @@ -91,7 +112,7 @@ exports.createConnection = function(args) {
return conn;
};

exports.getConfig = function(input) {
exports.getConfig = function (input) {
const args = input || {};
const params = {
host: args.host || config.host,
Expand All @@ -118,10 +139,13 @@ exports.getConfig = function(input) {
return params;
};

exports.createPool = function(args) {
exports.createPool = function (args) {
let driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createPool({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
return driver.createPool({
...args,
uri: process.env.MYSQL_CONNECTION_URL,
});
}

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

exports.createPoolCluster = function(args = {}) {
exports.createPoolCluster = function (args = {}) {
const driver = require('../index.js');
if (!args?.port && process.env.MYSQL_CONNECTION_URL) {
return driver.createPoolCluster({ ...args, uri: process.env.MYSQL_CONNECTION_URL })
return driver.createPoolCluster({
...args,
uri: process.env.MYSQL_CONNECTION_URL,
});
}
return driver.createPoolCluster(args)
}
return driver.createPoolCluster(args);
};

exports.createConnectionWithURI = function() {
exports.createConnectionWithURI = function () {
const driver = require('../index.js');

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

exports.createTemplate = function() {
exports.createTemplate = function () {
const jade = require('jade');
const template = require('fs').readFileSync(
`${__dirname}/template.jade`,
'ascii'
'ascii',
);
return jade.compile(template);
};

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

const portfinder = require('portfinder');
exports.createServer = function(onListening, handler) {
exports.createServer = function (onListening, handler) {
const server = require('../index.js').createServer();
server.on('connection', conn => {
conn.on('error', () => {
Expand All @@ -178,7 +205,7 @@ exports.createServer = function(onListening, handler) {
connectionId: 1234,
statusFlags: 2,
characterSet: 8,
capabilityFlags: flags
capabilityFlags: flags,
});
if (handler) {
handler(conn);
Expand All @@ -190,6 +217,6 @@ exports.createServer = function(onListening, handler) {
return server;
};

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

0 comments on commit fbc970c

Please sign in to comment.