Skip to content

Commit

Permalink
Add deadlock test
Browse files Browse the repository at this point in the history
Refs: #7
PR-URL: #18
  • Loading branch information
tshemsedinov committed Mar 15, 2020
1 parent 8eb0ee7 commit 54e82e3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

const tests = ['simple', 'nested', 'steps', 'exclusive', 'thread-main'];
const tests = [
'simple',
'nested',
'steps',
'exclusive',
'deadlock',
'thread-main',
];

(async () => {
for (const name of tests) {
Expand Down
43 changes: 43 additions & 0 deletions test/deadlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const assert = require('assert').strict;

const { locks } = require('..');

const TEST_TIMEOUT = 100;

module.exports = async () =>
new Promise(resolve => {
let flag1 = false;
let flag2 = false;
let flag3 = false;
let flag4 = false;

(async () => {
await locks.request('C', async () => {
flag1 = true;
await locks.request('D', async () => {
flag2 = true;
});
});
})();

(async () => {
await locks.request('D', async () => {
flag3 = true;
await locks.request('C', async () => {
flag4 = true;
});
});
})();

(async () => {
setTimeout(() => {
assert.strictEqual(flag1, true);
assert.strictEqual(flag2, false);
assert.strictEqual(flag3, true);
assert.strictEqual(flag4, false);
resolve();
}, TEST_TIMEOUT);
})();
});

0 comments on commit 54e82e3

Please sign in to comment.