mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-08 20:30:18 +00:00
Merge pull request #2366 from balena-os/using-infer-steps-lock-unit-test
Add unit test for usingInferStepsLock
This commit is contained in:
commit
6577ded0cd
@ -268,9 +268,11 @@ function emitAsync<T extends keyof DeviceStateEvents>(
|
|||||||
|
|
||||||
const inferStepsLock = () =>
|
const inferStepsLock = () =>
|
||||||
takeGlobalLockRW('inferSteps').disposer((release) => release());
|
takeGlobalLockRW('inferSteps').disposer((release) => release());
|
||||||
function usingInferStepsLock<T extends () => any, U extends ReturnType<T>>(
|
// Exported for unit test
|
||||||
fn: T,
|
export function usingInferStepsLock<
|
||||||
): Bluebird<UnwrappedPromise<U>> {
|
T extends () => any,
|
||||||
|
U extends ReturnType<T>,
|
||||||
|
>(fn: T): Bluebird<UnwrappedPromise<U>> {
|
||||||
return Bluebird.using(inferStepsLock, () => fn());
|
return Bluebird.using(inferStepsLock, () => fn());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
30
test/unit/device-state.spec.ts
Normal file
30
test/unit/device-state.spec.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import * as deviceState from '~/src/device-state';
|
||||||
|
|
||||||
|
describe('usingInferStepsLock', () => {
|
||||||
|
it('should not allow to start a function call without finishing the previous sequential call of the function', async () => {
|
||||||
|
const result: string[] = [];
|
||||||
|
const fn = async (id: number, str: string) => {
|
||||||
|
return deviceState.usingInferStepsLock(async () => {
|
||||||
|
if (id < 3 || id === 20) {
|
||||||
|
result.push(`Started ${id}, ${str} call`);
|
||||||
|
await fn(id + 1, 'first');
|
||||||
|
await fn(id + 1, 'second');
|
||||||
|
result.push(`Finished ${id}, ${str} call`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
await fn(1, 'master');
|
||||||
|
await fn(20, 'master');
|
||||||
|
expect(result).to.deep.equal([
|
||||||
|
'Started 1, master call',
|
||||||
|
'Started 2, first call',
|
||||||
|
'Finished 2, first call',
|
||||||
|
'Started 2, second call',
|
||||||
|
'Finished 2, second call',
|
||||||
|
'Finished 1, master call',
|
||||||
|
'Started 20, master call',
|
||||||
|
'Finished 20, master call',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user