Skip to content

Commit

Permalink
test: add case for task level isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 30, 2023
1 parent 1b6ca55 commit 15f9c65
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/isolation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { dirname, resolve } from 'path'
import { Tinypool } from 'tinypool'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url))

test('workers can be isolated between tasks', async () => {
const pool = new Tinypool({
filename: resolve(__dirname, 'fixtures/sleep.js'),
minThreads: 1,
maxThreads: 1,
isolateWorkers: false,
})

function getThreadId() {
expect(pool.threads).toHaveLength(1)
return pool.threads[0]!.threadId
}

const initialThreadId = getThreadId()

await pool.run({})
expect(getThreadId()).toBe(initialThreadId)

await pool.run({})
await pool.run({})
await pool.run({})
expect(getThreadId()).toBe(initialThreadId)

// TODO: pool.flushWorkers()
const newThreadId = getThreadId()
expect(newThreadId).not.toBe(initialThreadId)

await pool.run({})
await pool.run({})
await pool.run({})
expect(getThreadId()).toBe(newThreadId)
})

0 comments on commit 15f9c65

Please sign in to comment.