-
Notifications
You must be signed in to change notification settings - Fork 285
/
threads.js
74 lines (60 loc) · 1.94 KB
/
threads.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
const addon = require('..');
const assert = require('chai').assert;
(function () {
// These tests require GC exposed to shutdown properly; skip if it is not
return typeof global.gc === 'function' ? describe : describe.skip;
})()('sync', function() {
afterEach(() => {
// Force garbage collection to shutdown `EventQueue`
global.gc();
});
it('can create and deref a root', function () {
const expected = {};
const result = addon.useless_root(expected);
assert.strictEqual(expected, result);
});
it('should be able to callback from another thread', function (cb) {
addon.thread_callback(cb);
});
it('should be able to callback from multiple threads', function (cb) {
const n = 4;
const set = new Set([...new Array(n)].map((_, i) => i));
addon.multi_threaded_callback(n, function (x) {
if (!set.delete(x)) {
cb(new Error(`Unexpected callback value: ${x}`));
}
if (set.size === 0) {
cb();
}
});
});
it('should be able to use an async greeter', function (cb) {
const greeter = addon.greeter_new('Hello, World!', function (greeting) {
if (greeting === 'Hello, World!') {
cb();
} else {
new Error('Greeting did not match');
}
});
addon.greeter_greet(greeter);
});
it('should run callback on drop', function (cb) {
// IIFE to allow GC
(function () {
addon.greeter_new('Hello, World!', function () {}, function () {
// No assert needed; test will timeout
cb();
})
})();
global.gc();
});
it('should be able to unref event queue', function () {
// If the EventQueue is not unreferenced, the test runner will not cleanly exit
addon.leak_event_queue();
});
it('should drop leaked Root from the global queue', function (cb) {
addon.drop_global_queue(cb);
// Asynchronously GC to give the task queue a chance to execute
setTimeout(() => global.gc(), 10);
});
});