Skip to content

Commit

Permalink
[misc] ensuring pool test stability
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jul 13, 2018
1 parent 1f40093 commit fd360c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
13 changes: 11 additions & 2 deletions benchmarks/common_benchmarks.js
Expand Up @@ -59,7 +59,7 @@ function Bench() {
config.charsetNumber = 224;
config.trace = false;

const poolConfig = Object.assign({connectionLimit: 10},config);
const poolConfig = Object.assign({ connectionLimit: 10 }, config);
// config.debug = true;
// if (!mariasql && process.platform === "win32") {
// config.socketPath = "\\\\.\\pipe\\MySQL";
Expand Down Expand Up @@ -336,7 +336,16 @@ Bench.prototype.fill = function(val, length, right) {

Bench.prototype.add = function(title, displaySql, fct, onComplete, isPromise, usePool, conn) {
const self = this;
const addTest = getAddTest(self, this.suite, fct, this.minSamples, title, displaySql, onComplete, usePool);
const addTest = getAddTest(
self,
this.suite,
fct,
this.minSamples,
title,
displaySql,
onComplete,
usePool
);

if (conn) {
addTest(conn, conn.desc);
Expand Down
17 changes: 7 additions & 10 deletions lib/pool.js
Expand Up @@ -136,7 +136,7 @@ function Pool(options) {
//connections are all used, stack demand.
return new Promise((resolve, reject) => {
const task = {
timeout: new Date().getTime() + opts.acquireTimeout,
timeout: Date.now() + opts.acquireTimeout,
reject: reject,
resolve: resolve,
sql: sql,
Expand All @@ -155,10 +155,7 @@ function Pool(options) {

const conn = idleConnections.shift();
activeConnections[conn.threadId] = conn;
if (
opts.minDelayValidation <= 0 ||
new Date().getTime() - conn.lastUse > opts.minDelayValidation
) {
if (opts.minDelayValidation <= 0 || Date.now() - conn.lastUse > opts.minDelayValidation) {
try {
await conn.ping();
return conn;
Expand Down Expand Up @@ -188,7 +185,7 @@ function Pool(options) {
throw err;
}
} else {
conn.lastUse = new Date().getTime();
conn.lastUse = Date.now();
return conn;
}
};
Expand Down Expand Up @@ -258,7 +255,7 @@ function Pool(options) {
*/
const overlayNewConnection = function(conn, self) {
idleConnections.push(conn);
conn.lastUse = new Date().getTime();
conn.lastUse = Date.now();
const initialEndFct = conn.end;
conn.end = async () => {
try {
Expand Down Expand Up @@ -339,7 +336,7 @@ function Pool(options) {
conn
.query(task.sql, task.values)
.then(res => {
conn.lastUse = new Date().getTime();
conn.lastUse = Date.now();
conn.end().catch(err => {});
task.resolve(res);
})
Expand All @@ -348,15 +345,15 @@ function Pool(options) {
task.reject(err);
});
} else {
conn.lastUse = new Date().getTime();
conn.lastUse = Date.now();
task.resolve(conn);
}
}
};

const resetTimeoutToNextTask = () => {
//handle next Timer
const currTime = new Date().getTime();
const currTime = Date.now();
let nextTask;
while ((nextTask = taskQueue.peekFront())) {
if (nextTask.timeout < currTime) {
Expand Down
31 changes: 15 additions & 16 deletions test/integration/test-pool.js
Expand Up @@ -218,22 +218,21 @@ describe("Pool", () => {
assert.equal(pool.idleConnections(), 1);
assert.equal(pool.taskQueueSize(), 0);

pool.query("do 1");
pool.query("do 1").then(() => {
assert.equal(pool.activeConnections(), 1);
assert.equal(pool.totalConnections(), 1);
assert.equal(pool.idleConnections(), 0);
assert.equal(pool.taskQueueSize(), 0);
setTimeout(() => {
//connection recreated
assert.equal(pool.activeConnections(), 0);
assert.equal(pool.totalConnections(), 2);
assert.equal(pool.idleConnections(), 2);
assert.equal(pool.taskQueueSize(), 0);
pool.end();
done();
}, 250);
});
setTimeout(() => {

pool.query("do 1");
pool.query("do 1").then(() => {
setTimeout(() => {
//connection recreated
assert.equal(pool.activeConnections(), 0);
assert.equal(pool.totalConnections(), 2);
assert.equal(pool.idleConnections(), 2);
assert.equal(pool.taskQueueSize(), 0);
pool.end();
done();
}, 250);
});
}, 250);
});
});
}, 500);
Expand Down

0 comments on commit fd360c4

Please sign in to comment.