From 9cbf1683f858e240f838bed72704d8617a0b1ea3 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Fri, 8 Mar 2024 06:08:40 -0800 Subject: [PATCH] test(daemon): Add mutex unit tests --- packages/daemon/test/test-mutex.js | 93 ++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 packages/daemon/test/test-mutex.js diff --git a/packages/daemon/test/test-mutex.js b/packages/daemon/test/test-mutex.js new file mode 100644 index 0000000000..62ac3a6129 --- /dev/null +++ b/packages/daemon/test/test-mutex.js @@ -0,0 +1,93 @@ +/* global setTimeout */ +import '@endo/init/debug.js'; + +import rawTest from 'ava'; +import { wrapTest } from '@endo/ses-ava'; + +import { makeMutex } from '../src/mutex.js'; + +const test = wrapTest(rawTest); + +const delay = () => new Promise(resolve => setTimeout(resolve, 1)); + +test('releases lock in expected order (sync functions)', async t => { + const mutex = makeMutex(); + const results = []; + + await Promise.all([ + mutex.enqueue(() => { + results.push(1); + }), + mutex.enqueue(() => { + results.push(2); + }), + mutex.enqueue(() => { + results.push(3); + }), + ]); + + t.deepEqual(results, [1, 2, 3]); +}); + +test('releases lock in expected order (async functions)', async t => { + const mutex = makeMutex(); + const results = []; + + await Promise.all([ + mutex.enqueue(async () => { + results.push(1); + }), + mutex.enqueue(async () => { + results.push(2); + }), + mutex.enqueue(async () => { + results.push(3); + }), + ]); + + t.deepEqual(results, [1, 2, 3]); +}); + +test('releases lock in expected order (async functions with await)', async t => { + const mutex = makeMutex(); + const results = []; + + await Promise.all([ + mutex.enqueue(async () => { + await delay(); + results.push(1); + }), + mutex.enqueue(async () => { + await delay(); + results.push(2); + }), + mutex.enqueue(async () => { + await delay(); + results.push(3); + }), + ]); + + t.deepEqual(results, [1, 2, 3]); +}); + +test('immediately releases the lock to the awaiter', async t => { + const mutex = makeMutex(); + const results = []; + + await Promise.all([ + mutex.enqueue(async () => { + await delay(); + results.push(2); + }), + (async () => { + results.push(1); + await mutex.enqueue(() => delay()); + results.push(3); + })(), + mutex.enqueue(async () => { + results.push(4); + }), + ]); + + t.deepEqual(results, [1, 2, 3, 4]); +});