-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b6ca55
commit 89df201
Showing
3 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { dirname, resolve } from 'path' | ||
import { Tinypool } from 'tinypool' | ||
import { fileURLToPath } from 'url' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
test('idle workers can be recycled', async () => { | ||
const pool = new Tinypool({ | ||
filename: resolve(__dirname, 'fixtures/sleep.js'), | ||
minThreads: 4, | ||
maxThreads: 4, | ||
isolateWorkers: false, | ||
}) | ||
|
||
function getThreadIds() { | ||
return pool.threads.map((thread) => thread!.threadId).sort() | ||
} | ||
|
||
expect(pool.threads).toHaveLength(4) | ||
const initialThreadIds = getThreadIds() | ||
|
||
await Promise.all(times(4)(() => pool.run({}))) | ||
expect(getThreadIds()).toStrictEqual(initialThreadIds) | ||
|
||
await pool.recycleWorkers() | ||
expect(pool.threads).toHaveLength(4) | ||
|
||
const newThreadIds = getThreadIds() | ||
initialThreadIds.forEach((id) => expect(newThreadIds).not.toContain(id)) | ||
|
||
await Promise.all(times(4)(() => pool.run({}))) | ||
initialThreadIds.forEach((id) => expect(newThreadIds).not.toContain(id)) | ||
}) | ||
|
||
test('running workers can recycle after task execution finishes', async () => { | ||
const pool = new Tinypool({ | ||
filename: resolve(__dirname, 'fixtures/sleep.js'), | ||
minThreads: 4, | ||
maxThreads: 4, | ||
isolateWorkers: false, | ||
}) | ||
|
||
function getThreadIds() { | ||
return pool.threads.map((thread) => thread!.threadId).sort() | ||
} | ||
|
||
expect(pool.threads).toHaveLength(4) | ||
const initialThreadIds = getThreadIds() | ||
expect(getThreadIds()).toStrictEqual(initialThreadIds) | ||
|
||
const tasks = [ | ||
...times(2)(() => pool.run({ time: 500 })), | ||
...times(2)(() => pool.run({ time: 2000 })), | ||
] | ||
|
||
await Promise.all(tasks.slice(0, 2)) | ||
await pool.recycleWorkers() | ||
await Promise.all(tasks) | ||
|
||
const newThreadIds = getThreadIds() | ||
initialThreadIds.forEach((id) => expect(newThreadIds).not.toContain(id)) | ||
}) | ||
|
||
function times(count: number) { | ||
return function run<T>(fn: () => T): T[] { | ||
return Array(count).fill(0).map(fn) | ||
} | ||
} |