/
test_child-pool.js
162 lines (135 loc) · 4.12 KB
/
test_child-pool.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const expect = require('chai').expect;
const childPool = require('../lib/process/child-pool');
describe('Child pool', () => {
let pool;
beforeEach(() => {
pool = new childPool();
});
afterEach(() => {
pool.clean();
});
it('should return same child if free', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let child;
return pool
.retain(processor)
.then(_child => {
expect(_child).to.be.ok;
child = _child;
pool.release(child);
expect(pool.retained).to.be.empty;
return pool.retain(processor);
})
.then(newChild => {
expect(child).to.be.eql(newChild);
});
});
it('should return a new child if reused the last free one', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let child;
return pool
.retain(processor)
.then(_child => {
expect(_child).to.be.ok;
child = _child;
pool.release(child);
expect(pool.retained).to.be.empty;
return pool.retain(processor);
})
.then(newChild => {
expect(child).to.be.eql(newChild);
child = newChild;
return pool.retain(processor);
})
.then(newChild => {
expect(child).not.to.be.eql(newChild);
});
});
it('should return a new child if none free', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let child;
return pool
.retain(processor)
.then(_child => {
expect(_child).to.be.ok;
child = _child;
expect(pool.retained).not.to.be.empty;
return pool.retain(processor);
})
.then(newChild => {
expect(child).to.not.be.eql(newChild);
});
});
it('should return a new child if killed', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let child;
return pool
.retain(processor)
.then(_child => {
expect(_child).to.be.ok;
child = _child;
pool.kill(child);
expect(pool.retained).to.be.empty;
return pool.retain(processor);
})
.then(newChild => {
expect(child).to.not.be.eql(newChild);
});
});
it('should return a new child if many retained and none free', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let children;
return Promise.all([
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor)
])
.then(_children => {
children = _children;
expect(children).to.have.length(6);
return pool.retain(processor);
})
.then(child => {
expect(children).not.to.include(child);
});
});
it('should return an old child if many retained and one free', () => {
const processor = __dirname + '/fixtures/fixture_processor_bar.js';
let children;
return Promise.all([
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor),
pool.retain(processor)
])
.then(_children => {
children = _children;
expect(children).to.have.length(6);
pool.release(_children[0]);
return pool.retain(processor);
})
.then(child => {
expect(children).to.include(child);
});
});
it('should returned a shared child pool is isSharedChildPool is true', () => {
expect(childPool(true)).to.be.equal(new childPool(true));
});
it('should return a different childPool if isSharedChildPool is false', () => {
expect(childPool()).to.not.be.equal(childPool());
});
it('should not overwrite the the childPool singleton when isSharedChildPool is false', () => {
const childPoolA = new childPool(true)
const childPoolB = new childPool(false)
const childPoolC = new childPool(true);
expect(childPoolA).to.be.equal(childPoolC)
expect(childPoolB).to.not.be.equal(childPoolA)
expect(childPoolB).to.not.be.equal(childPoolC)
})
});