|
11 | 11 | * limitations under the License. |
12 | 12 | */ |
13 | 13 |
|
14 | | -import TaskWorklet from '../src/index.mjs'; |
| 14 | +import TaskQueue from '../src/index.mjs'; |
15 | 15 |
|
16 | | -describe('TaskWorklet', () => { |
| 16 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 17 | + |
| 18 | +describe('TaskQueue', () => { |
17 | 19 | it('should pass smoketest', async () => { |
18 | | - expect(TaskWorklet).toBe(expect.any(Function)); |
| 20 | + expect(typeof TaskQueue).toBe('function'); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('task execution and chaining', () => { |
| 24 | + let queue; |
| 25 | + |
| 26 | + it('should be instantiable via a Blob URL', async () => { |
| 27 | + queue = new TaskQueue(); |
| 28 | + await queue.addModule(URL.createObjectURL(new Blob([` |
| 29 | + registerTask('add', class { |
| 30 | + process(a, b) { |
| 31 | + return a + b; |
| 32 | + } |
| 33 | + }) |
| 34 | + `]))); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should execute a single task', async () => { |
| 38 | + const sum1 = queue.postTask('add', 1, 2); |
| 39 | + |
| 40 | + await sleep(1); |
| 41 | + expect(sum1.state).toBe('scheduled'); |
| 42 | + |
| 43 | + const result = await sum1.result; |
| 44 | + expect(sum1.state).toBe('fulfilled'); |
| 45 | + expect(result).toBe(3); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should execute tasks with task dependencies', async () => { |
| 49 | + const sum1 = queue.postTask('add', 1, 2); |
| 50 | + const sum2 = queue.postTask('add', sum1, 2); |
| 51 | + |
| 52 | + await sleep(1); |
| 53 | + expect(sum1.state).toBe('scheduled'); |
| 54 | + expect(sum2.state).toBe('scheduled'); |
| 55 | + |
| 56 | + const result = await sum2.result; |
| 57 | + expect(sum1.state).toBe('fulfilled'); |
| 58 | + expect(sum2.state).toBe('fulfilled'); |
| 59 | + expect(result).toBe(5); |
| 60 | + }); |
19 | 61 | }); |
20 | 62 | }); |
0 commit comments