-
-
Notifications
You must be signed in to change notification settings - Fork 497
Expand file tree
/
Copy paththreadsafe_function.js
More file actions
225 lines (203 loc) · 6.82 KB
/
threadsafe_function.js
File metadata and controls
225 lines (203 loc) · 6.82 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
const assert = require('assert');
const common = require('../common');
module.exports = common.runTest(test);
// Main test body
async function test (binding) {
const expectedArray = (function (arrayLength) {
const result = [];
for (let index = 0; index < arrayLength; index++) {
result.push(arrayLength - 1 - index);
}
return result;
})(binding.threadsafe_function.ARRAY_LENGTH);
const expectedDefaultArray = Array.from({ length: binding.threadsafe_function.ARRAY_LENGTH }, (_, i) => 42);
function testWithJSMarshaller ({
threadStarter,
quitAfter,
abort,
maxQueueSize,
launchSecondary
}) {
return new Promise((resolve) => {
const array = [];
binding.threadsafe_function[threadStarter](function testCallback (value) {
array.push(value);
if (array.length === quitAfter) {
binding.threadsafe_function.stopThread(common.mustCall(() => {
resolve(array);
}), !!abort);
}
}, !!abort, !!launchSecondary, maxQueueSize);
if ((threadStarter === 'startThreadNonblocking' || threadStarter === 'startThreadNonblockSingleArg')) {
// Let's make this thread really busy for a short while to ensure that
// the queue fills and the thread receives a napi_queue_full.
const start = Date.now();
while (Date.now() - start < 200);
}
});
}
function testWithoutJSMarshallers (nativeFunction) {
return new Promise((resolve) => {
let callCount = 0;
nativeFunction(function testCallback () {
callCount++;
// The default call-into-JS implementation passes no arguments.
assert.strictEqual(arguments.length, 0);
if (callCount === binding.threadsafe_function.ARRAY_LENGTH) {
setImmediate(() => {
binding.threadsafe_function.stopThread(common.mustCall(() => {
resolve();
}), false);
});
}
}, false /* abort */, false /* launchSecondary */,
binding.threadsafe_function.MAX_QUEUE_SIZE);
});
}
await testWithoutJSMarshallers(binding.threadsafe_function.startThreadNoNative);
await testWithoutJSMarshallers(binding.threadsafe_function.startThreadNonblockingNoNative);
// Start the thread in blocking mode, and assert that all values are passed.
// Quit after it's done.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray
);
// Start the thread in blocking mode with an infinite queue, and assert that
// all values are passed. Quit after it's done.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray
);
// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit after it's done.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray
);
// Start the thread in blocking mode, and assert that all values are passed.
// Quit early, but let the thread finish.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}),
expectedArray
);
// Start the thread in blocking mode with an infinite queue, and assert that
// all values are passed. Quit early, but let the thread finish.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: 1
}),
expectedArray
);
// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit early, but let the thread finish.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}),
expectedArray
);
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblockSingleArg',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}),
expectedDefaultArray
);
// Start the thread in blocking mode, and assert that all values are passed.
// Quit early, but let the thread finish. Launch a secondary thread to test
// the reference counter incrementing functionality.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}),
expectedArray
);
// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit early, but let the thread finish. Launch a secondary thread
// to test the reference counter incrementing functionality.
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}),
expectedArray
);
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblockSingleArg',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1,
launchSecondary: true
}),
expectedDefaultArray
);
// Start the thread in blocking mode, and assert that it could not finish.
// Quit early by aborting.
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
})).indexOf(0),
-1
);
// Start the thread in blocking mode with an infinite queue, and assert that
// it could not finish. Quit early by aborting.
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: 0,
abort: true
})).indexOf(0),
-1
);
// Start the thread in non-blocking mode, and assert that it could not finish.
// Quit early and aborting.
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
})).indexOf(0),
-1
);
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThreadNonblockSingleArg',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
})).indexOf(0),
-1
);
}