-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
91 lines (72 loc) · 2.83 KB
/
index.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
87
88
89
90
91
import test from 'parallel-test'
import * as assert from 'assert'
import SharedArrayBufferReadonlyStringArray from '../index.js'
import {
Worker,
} from 'worker_threads'
import * as path from 'path'
test(`Contains the same data that was passed in`, () => {
const inputData: ConstructorParameters<typeof SharedArrayBufferReadonlyStringArray>[0] = [
`abc`,
`a longer string`,
`striñg with speciàl characters: Ꭳ‗‰⁔₴ℒↈ⇘⊞⏚⛄🭨ЌΫDŽƆ\u0016⻆👨👩👧👦`.normalize(`NFC`),
`striñg with speciàl characters: Ꭳ‗‰⁔₴ℒↈ⇘⊞⏚⛄🭨ЌΫDŽƆ\u0016⻆👨👩👧👦`.normalize(`NFD`),
``,
``,
`\n\n`,
]
const sharedStrings = new SharedArrayBufferReadonlyStringArray(inputData)
assert.deepStrictEqual(
inputData,
Array.from(sharedStrings),
)
})
test(`Works for empty arrays`, () => {
assert.doesNotThrow(() => new SharedArrayBufferReadonlyStringArray([]))
})
test(`Works for single-element arrays`, () => {
const inputData: ConstructorParameters<typeof SharedArrayBufferReadonlyStringArray>[0] = [
`abc`,
]
const sharedStrings = new SharedArrayBufferReadonlyStringArray(inputData)
assert.deepStrictEqual(
inputData,
Array.from(sharedStrings),
)
})
test(`Can transfer from thread to thread`, async () => {
const inputData: ConstructorParameters<typeof SharedArrayBufferReadonlyStringArray>[0] = Array(10)
.fill(null)
.map(() => Math.random().toString())
const sharedStrings = new SharedArrayBufferReadonlyStringArray(inputData)
const workerThread = new Worker(path.join(path.dirname(new URL(import.meta.url).pathname), `identityWorker.js`))
workerThread.unref() // alternative to explicit `terminate` later, to allow the process to exit
workerThread.postMessage(sharedStrings)
const response = await new Promise((resolve, reject) => {
workerThread.once(`error`, reject)
workerThread.once(`messageerror`, reject)
workerThread.once(`message`, resolve)
})
assert.deepStrictEqual(
Array.from(SharedArrayBufferReadonlyStringArray.fromWorkerThreadMessageData(response)),
inputData,
)
})
test(`Worker threads have the same data`, async () => {
const inputData: ConstructorParameters<typeof SharedArrayBufferReadonlyStringArray>[0] = Array(10)
.fill(null)
.map(() => Math.random().toString())
const sharedStrings = new SharedArrayBufferReadonlyStringArray(inputData)
const workerThread = new Worker(path.join(path.dirname(new URL(import.meta.url).pathname), `concatenateWorker.js`))
workerThread.unref() // alternative to explicit `terminate` later, to allow the process to exit
workerThread.postMessage(sharedStrings)
const response = await new Promise((resolve, reject) => {
workerThread.once(`error`, reject)
workerThread.once(`messageerror`, reject)
workerThread.once(`message`, resolve)
})
assert.deepStrictEqual(
response,
inputData.join(``),
)
})