This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Financial-Times/matth/fix-semaphore
Fix semaphore
- Loading branch information
Showing
2 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const subject = require('../../src/run-parallel'); | ||
|
||
const wait = (ms = 10) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const noop = () => {}; | ||
|
||
function createTask(name, onStart, onEnd, time = 10, allDependencies = []) { | ||
return { | ||
apply: () => { | ||
onStart(); | ||
return wait(time).then(onEnd); | ||
}, | ||
pkg: { | ||
name, | ||
allDependencies | ||
} | ||
}; | ||
} | ||
|
||
describe('src/run-parallel', () => { | ||
it('executes each task', async () => { | ||
const complete = []; | ||
|
||
const tasks = [ | ||
createTask('a', noop, () => complete.push('a')), | ||
createTask('b', noop, () => complete.push('b')), | ||
createTask('c', noop, () => complete.push('c')) | ||
]; | ||
|
||
await subject(tasks); | ||
|
||
expect(complete.length).toEqual(3); | ||
}); | ||
|
||
describe('with preserve order option', () => { | ||
it('waits for dependent tasks to finish', async () => { | ||
const complete = [] | ||
|
||
const tasks = [ | ||
createTask( | ||
'a', | ||
() => expect(complete.length).toEqual(0), | ||
() => complete.push('a'), | ||
100 | ||
), | ||
createTask( | ||
'b', | ||
() => expect(complete.length).toEqual(0), | ||
() => complete.push('b'), | ||
10 | ||
), | ||
createTask( | ||
'c', | ||
() => expect(complete).toEqual(expect.arrayContaining(['a', 'b'])), | ||
() => complete.push('c'), | ||
10, | ||
['a', 'b'] | ||
) | ||
]; | ||
|
||
await subject(tasks, 10, true); | ||
}); | ||
}); | ||
|
||
describe('without preserve order option', () => { | ||
it('waits for dependent tasks to finish', async () => { | ||
const complete = [] | ||
|
||
const tasks = [ | ||
createTask( | ||
'a', | ||
() => expect(complete.length).toEqual(0), | ||
() => complete.push('a'), | ||
100 | ||
), | ||
createTask( | ||
'b', | ||
() => expect(complete.length).toEqual(0), | ||
() => complete.push('b'), | ||
10 | ||
), | ||
createTask( | ||
'c', | ||
() => expect(complete.length).toEqual(0), | ||
() => complete.push('c'), | ||
10, | ||
['a', 'b'] | ||
) | ||
]; | ||
|
||
await subject(tasks, 10, false); | ||
}); | ||
}); | ||
}); |