Skip to content

Commit

Permalink
Speed up db connection handling tests using sinon fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Jun 13, 2024
1 parent 44b1c57 commit b73ffde
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 44 deletions.
19 changes: 11 additions & 8 deletions db.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {enwikidb} from "./db";
import {sleep} from "mwn/build/utils";
import * as assert from "assert";
import {expect} from "chai";
import * as sinon from 'sinon';

const testDb = new enwikidb({
host: '127.0.0.1',
Expand All @@ -14,18 +14,21 @@ const testDb = new enwikidb({
it('destroy pooled connections on inactivity', async function () {
this.timeout(10000);
let conn1, conn2;
let clock = sinon.useFakeTimers();

conn1 = await testDb.getConnection()
conn1.release();
await sleep(1000);
clock.tick(1000)
conn2 = await testDb.getConnection();
conn2.release()
assert(conn1.threadId === conn2.threadId)
conn2.release();
expect(conn1.threadId).to.eq(conn2.threadId)

conn1 = await testDb.getConnection()
conn1.release();
await sleep(5100);
clock.tick(5100);
conn2 = await testDb.getConnection();
conn2.release()
assert(conn1.threadId !== conn2.threadId)
conn2.release();
expect(conn1.threadId).to.not.eq(conn2.threadId)

clock.restore();
});
Loading

0 comments on commit b73ffde

Please sign in to comment.