-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
34 lines (28 loc) · 1.03 KB
/
test.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
const platform = require('./index.js');
const assert = require('assert').strict;
const { Worker, isMainThread, workerData } = require('worker_threads');
if (isMainThread) {
const threadId = new platform.ThreadId();
const otherThreadId = new platform.ThreadId();
// Test for same thread id
assert(threadId.equals(otherThreadId) === true, 'Thread Ids must be equal');
// Test for invalid input
class Whatever {
constructor() {}
};
try {
threadId.equals(new Whatever());
assert.fail('ThreadId should not work with wrong types');
} catch (e) {
assert(e.message === 'Invalid argument', 'Thread Id must protect against wrong object types');
}
// Test worker
new Worker(__filename, {
workerData: threadId.serialize(),
});
} else {
const mainThreadId = new platform.ThreadId(workerData);
const threadId = new platform.ThreadId();
// Test for different thread ids
assert(threadId.equals(mainThreadId) === false, 'Thread Id must be different');
}