-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_child-pool.ts
86 lines (76 loc) · 2.83 KB
/
test_child-pool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { expect } from 'chai';
import { ChildPool } from '../src/classes';
describe('Child pool', () => {
let pool: ChildPool;
beforeEach(() => {
pool = new ChildPool();
});
afterEach(() => pool.clean());
it('should return same child if free', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
const child = await pool.retain(processor);
expect(child).to.be.ok;
pool.release(child);
expect(pool.retained).to.be.empty;
const newChild = await pool.retain(processor);
expect(child).to.be.eql(newChild);
});
it('should return a new child if reused the last free one', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let child = await pool.retain(processor);
expect(child).to.be.ok;
pool.release(child);
expect(pool.retained).to.be.empty;
let newChild = await pool.retain(processor);
expect(child).to.be.eql(newChild);
child = newChild;
newChild = await pool.retain(processor);
expect(child).not.to.be.eql(newChild);
});
it('should return a new child if none free', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
const child = await pool.retain(processor);
expect(child).to.be.ok;
expect(pool.retained).not.to.be.empty;
const newChild = await pool.retain(processor);
expect(child).to.not.be.eql(newChild);
});
it('should return a new child if killed', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
const child = await pool.retain(processor);
expect(child).to.be.ok;
await pool.kill(child);
expect(pool.retained).to.be.empty;
const newChild = await pool.retain(processor);
expect(child).to.not.be.eql(newChild);
});
it('should return a new child if many retained and none free', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
const children = await Promise.all([
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
]);
expect(children).to.have.length(6);
const child = await pool.retain(processor);
expect(children).not.to.include(child);
}).timeout(10000);
it('should return an old child if many retained and one free', async () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
const children = await Promise.all([
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
]);
expect(children).to.have.length(6);
pool.release(children[0]);
const child = await pool.retain(processor);
expect(children).to.include(child);
}).timeout(10000);
});