|
1 | | -import { expect } from 'chai'; |
2 | | -import { ChildPool } from '../src/classes'; |
3 | | -import { join } from 'path'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { ChildPool } from '../src/classes' |
4 | 4 |
|
5 | | -const NoopProc = () => {}; |
6 | | -describe('Child pool for Child Processes', () => { |
7 | | - sandboxProcessTests(); |
8 | | -}); |
| 5 | +describe('child pool for Child Processes', () => { |
| 6 | + sandboxProcessTests() |
| 7 | +}) |
9 | 8 |
|
10 | | -describe('Child pool for Worker Threads', () => { |
| 9 | +describe('child pool for Worker Threads', () => { |
11 | 10 | sandboxProcessTests({ |
12 | 11 | mainFile: join(process.cwd(), 'dist/cjs/classes/main-worker.js'), |
13 | 12 | useWorkerThreads: true, |
14 | | - }); |
15 | | -}); |
| 13 | + }) |
| 14 | +}) |
16 | 15 |
|
17 | 16 | function sandboxProcessTests( |
18 | 17 | { |
19 | 18 | mainFile, |
20 | 19 | useWorkerThreads, |
21 | | - }: { mainFile?: string; useWorkerThreads?: boolean } = { |
| 20 | + }: { mainFile?: string, useWorkerThreads?: boolean } = { |
22 | 21 | useWorkerThreads: false, |
23 | 22 | }, |
24 | 23 | ) { |
25 | | - describe('Child pool', () => { |
26 | | - let pool: ChildPool; |
| 24 | + describe('child pool', () => { |
| 25 | + let pool: ChildPool |
27 | 26 |
|
28 | 27 | beforeEach(() => { |
29 | | - pool = new ChildPool({ mainFile, useWorkerThreads }); |
30 | | - }); |
| 28 | + pool = new ChildPool({ mainFile, useWorkerThreads }) |
| 29 | + }) |
31 | 30 |
|
32 | | - afterEach(() => pool.clean()); |
| 31 | + afterEach(() => pool.clean()) |
33 | 32 |
|
34 | 33 | it('should return same child if free', async () => { |
35 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
36 | | - const child = await pool.retain(processor, NoopProc); |
37 | | - expect(child).to.be.ok; |
38 | | - pool.release(child); |
39 | | - expect(pool.retained).to.be.empty; |
40 | | - const newChild = await pool.retain(processor, NoopProc); |
41 | | - expect(child).toBe(newChild); |
42 | | - }); |
| 34 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
| 35 | + const child = await pool.retain(processor) |
| 36 | + expect(child).toBeTruthy() |
| 37 | + pool.release(child) |
| 38 | + expect(pool.retained).toEqual({}) |
| 39 | + const newChild = await pool.retain(processor) |
| 40 | + expect(child).toBe(newChild) |
| 41 | + }) |
43 | 42 |
|
44 | 43 | it('should return a new child if reused the last free one', async () => { |
45 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
46 | | - let child = await pool.retain(processor, NoopProc); |
47 | | - expect(child).to.be.ok; |
48 | | - pool.release(child); |
49 | | - expect(pool.retained).to.be.empty; |
50 | | - let newChild = await pool.retain(processor, NoopProc); |
51 | | - expect(child).toBe(newChild); |
52 | | - child = newChild; |
53 | | - newChild = await pool.retain(processor, NoopProc); |
54 | | - expect(child).not.toBe(newChild); |
55 | | - }); |
| 44 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
| 45 | + let child = await pool.retain(processor) |
| 46 | + expect(child).toBeTruthy() |
| 47 | + pool.release(child) |
| 48 | + expect(pool.retained).toEqual({}) |
| 49 | + let newChild = await pool.retain(processor) |
| 50 | + expect(child).toBe(newChild) |
| 51 | + child = newChild |
| 52 | + newChild = await pool.retain(processor) |
| 53 | + expect(child).not.toBe(newChild) |
| 54 | + }) |
56 | 55 |
|
57 | 56 | it('should return a new child if none free', async () => { |
58 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
59 | | - const child = await pool.retain(processor, NoopProc); |
60 | | - expect(child).to.be.ok; |
61 | | - expect(pool.retained).not.to.be.empty; |
62 | | - const newChild = await pool.retain(processor, NoopProc); |
63 | | - expect(child).to.not.be.eql(newChild); |
64 | | - }); |
| 57 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
| 58 | + const child = await pool.retain(processor) |
| 59 | + expect(child).toBeTruthy() |
| 60 | + expect(pool.retained).not.toEqual({}) |
| 61 | + const newChild = await pool.retain(processor) |
| 62 | + expect(child).not.toEqual(newChild) |
| 63 | + }) |
65 | 64 |
|
66 | 65 | it('should return a new child if killed', async () => { |
67 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
68 | | - const child = await pool.retain(processor, NoopProc); |
69 | | - expect(child).to.be.ok; |
70 | | - await pool.kill(child); |
71 | | - expect(pool.retained).to.be.empty; |
72 | | - const newChild = await pool.retain(processor, NoopProc); |
73 | | - expect(child).to.not.be.eql(newChild); |
74 | | - }); |
| 66 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
| 67 | + const child = await pool.retain(processor) |
| 68 | + expect(child).toBeTruthy() |
| 69 | + await pool.kill(child) |
| 70 | + expect(pool.retained).toEqual({}) |
| 71 | + const newChild = await pool.retain(processor) |
| 72 | + expect(child).not.toEqual(newChild) |
| 73 | + }) |
75 | 74 |
|
76 | 75 | it('should return a new child if many retained and none free', async () => { |
77 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
| 76 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
78 | 77 | const children = await Promise.all([ |
79 | | - pool.retain(processor, NoopProc), |
80 | | - pool.retain(processor, NoopProc), |
81 | | - pool.retain(processor, NoopProc), |
82 | | - pool.retain(processor, NoopProc), |
83 | | - pool.retain(processor, NoopProc), |
84 | | - pool.retain(processor, NoopProc), |
85 | | - ]); |
86 | | - expect(children).to.have.length(6); |
87 | | - const child = await pool.retain(processor, NoopProc); |
88 | | - expect(children).not.to.include(child); |
89 | | - }).timeout(10000); |
| 78 | + pool.retain(processor), |
| 79 | + pool.retain(processor), |
| 80 | + pool.retain(processor), |
| 81 | + pool.retain(processor), |
| 82 | + pool.retain(processor), |
| 83 | + pool.retain(processor), |
| 84 | + ]) |
| 85 | + expect(children).toHaveLength(6) |
| 86 | + const child = await pool.retain(processor) |
| 87 | + expect(children).not.toContain(child) |
| 88 | + }, 10000) |
90 | 89 |
|
91 | 90 | it('should return an old child if many retained and one free', async () => { |
92 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
| 91 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
93 | 92 | const children = await Promise.all([ |
94 | | - pool.retain(processor, NoopProc), |
95 | | - pool.retain(processor, NoopProc), |
96 | | - pool.retain(processor, NoopProc), |
97 | | - pool.retain(processor, NoopProc), |
98 | | - pool.retain(processor, NoopProc), |
99 | | - pool.retain(processor, NoopProc), |
100 | | - ]); |
| 93 | + pool.retain(processor), |
| 94 | + pool.retain(processor), |
| 95 | + pool.retain(processor), |
| 96 | + pool.retain(processor), |
| 97 | + pool.retain(processor), |
| 98 | + pool.retain(processor), |
| 99 | + ]) |
101 | 100 |
|
102 | | - expect(children).to.have.length(6); |
103 | | - pool.release(children[0]); |
104 | | - const child = await pool.retain(processor); |
105 | | - expect(children).to.include(child); |
106 | | - }).timeout(10000); |
| 101 | + expect(children).toHaveLength(6) |
| 102 | + pool.release(children[0]) |
| 103 | + const child = await pool.retain(processor) |
| 104 | + expect(children).toContain(child) |
| 105 | + }, 10000) |
107 | 106 |
|
108 | 107 | it('should consume execArgv array from process', async () => { |
109 | | - const processor = __dirname + '/fixtures/fixture_processor_bar.js'; |
110 | | - process.execArgv.push('--no-warnings'); |
| 108 | + const processor = join(__dirname, 'fixtures/fixture_processor_bar.js') |
| 109 | + process.execArgv.push('--no-warnings') |
111 | 110 |
|
112 | | - const child = await pool.retain(processor, NoopProc); |
113 | | - expect(child).to.be.ok; |
| 111 | + const child = await pool.retain(processor) |
| 112 | + expect(child).toBeTruthy() |
114 | 113 | if (!useWorkerThreads) { |
115 | | - expect(child.childProcess.spawnargs).to.include('--no-warnings'); |
| 114 | + expect(child.childProcess.spawnargs).toContain('--no-warnings') |
116 | 115 | } |
117 | | - }); |
118 | | - }); |
| 116 | + }) |
| 117 | + }) |
119 | 118 | } |
0 commit comments