Skip to content

Commit

Permalink
chore: enable @typescript-eslint/no-floating-promises rule
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 14, 2024
1 parent c4b21e6 commit 5dccf89
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 47 deletions.
1 change: 0 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default defineConfig([
{
// TODO: Must-have rules. Enable one-by-one
rules: {
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class WorkerInfo extends AsynchronouslyCreatedResource {
)
: null

this.worker.terminate().then(() => {
void this.worker.terminate().then(() => {
if (timer !== null) {
clearTimeout(timer)
}
Expand Down Expand Up @@ -791,7 +791,7 @@ class ThreadPool {

// Remove the worker from the list and potentially start a new Worker to
// replace the current one.
this._removeWorker(workerInfo)
void this._removeWorker(workerInfo)

if (workerInfo.isReady() && !this.workerFailsDuringBootstrap) {
this._ensureMinimumWorkers()
Expand Down Expand Up @@ -874,7 +874,7 @@ class ThreadPool {
workerInfo.idleTimeout = setTimeout(() => {
assert.strictEqual(workerInfo.taskInfos.size, 0)
if (this.workers.size > this.options.minThreads) {
this._removeWorker(workerInfo)
void this._removeWorker(workerInfo)
}
}, this.options.idleTimeout).unref()
}
Expand Down Expand Up @@ -942,7 +942,7 @@ class ThreadPool {

if (taskInfo.workerInfo !== null) {
// Already running: We cancel the Worker this is running on.
this._removeWorker(taskInfo.workerInfo)
void this._removeWorker(taskInfo.workerInfo)
this._ensureMinimumWorkers()
} else {
// Not yet running: Remove it from the queue.
Expand Down Expand Up @@ -1063,7 +1063,7 @@ class ThreadPool {
// @ts-ignore
exitEvents.push(once(workerInfo.worker, 'exit'))
// @ts-ignore
this._removeWorker(workerInfo)
void this._removeWorker(workerInfo)
}

await Promise.all(exitEvents)
Expand All @@ -1090,7 +1090,7 @@ class ThreadPool {
if (workerInfo.currentUsage() === 0) {
// @ts-ignore
exitEvents.push(once(workerInfo.worker, 'exit'))
this._removeWorker(workerInfo!)
void this._removeWorker(workerInfo!)
}
// Mark on-going workers for recycling.
// Note that we don't need to wait for these ones to finish
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/nested-pool.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function nestedPool() {
})

await Promise.resolve()
pool.recycleWorkers()
void pool.recycleWorkers()
}

export function entrypoint() {}
10 changes: 8 additions & 2 deletions test/pool-destroy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test('can destroy pool while tasks are running', async () => {
filename: resolve(__dirname, 'fixtures/eval.js'),
})
setImmediate(() => pool.destroy())
expect(pool.run('while(1){}')).rejects.toThrow(/Terminating worker thread/)
await expect(pool.run('while(1){}')).rejects.toThrow(
/Terminating worker thread/
)
})

test('destroy after initializing should work (#43)', async () => {
Expand All @@ -19,8 +21,12 @@ test('destroy after initializing should work (#43)', async () => {
isolateWorkers: true,
})

expect(pool.run({})).rejects.toThrow(/Terminating worker thread/)
const promise = expect(pool.run({})).rejects.toThrow(
/Terminating worker thread/
)

setImmediate(() => pool.destroy())
await promise
})

test('cleans up async resources', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/resource-limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('resourceLimits causes task to reject', async () => {
expect(limits.maxOldGenerationSizeMb).toBe(4)
expect(limits.maxYoungGenerationSizeMb).toBe(2)
expect(limits.codeRangeSizeMb).toBe(4)
expect(worker.run(null)).rejects.toThrow(
await expect(worker.run(null)).rejects.toThrow(
/Worker terminated due to reaching memory limit: JS heap out of memory/
)
})
Expand Down
28 changes: 14 additions & 14 deletions test/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test('filename can be null when initially provided', async () => {

test('filename must be provided while posting', async () => {
const worker = new Tinypool()
expect(worker.run('doesn’t matter')).rejects.toThrow(
await expect(worker.run('doesn’t matter')).rejects.toThrow(
/filename must be provided to run\(\) or in options object/
)
})
Expand Down Expand Up @@ -194,11 +194,11 @@ test('workerId should never be more than maxThreads=1', async () => {
maxThreads: maxThreads,
})
await pool.destroy()
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)

await sleep(300)
})
Expand All @@ -211,14 +211,14 @@ test('workerId should never be more than maxThreads', async () => {
maxThreads: maxThreads,
})
await pool.destroy()
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)
await expect(pool.run({})).resolves.toBeLessThanOrEqual(maxThreads)

await sleep(300)
})
Expand Down
76 changes: 55 additions & 21 deletions test/task-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,44 @@ test('will reject items over task queue limit', async () => {
maxThreads: 1,
maxQueue: 2,
})
const promises: Promise<void>[] = []

expect(pool.threads.length).toBe(0)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
)
)

expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
)
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(1)

expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
)
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(2)

expect(pool.run('while (true) {}')).rejects.toThrow(/Task queue is at limit/)
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Task queue is at limit/
)
)

await pool.destroy()
await Promise.all(promises)
})

test('will reject items when task queue is unavailable', async () => {
Expand All @@ -89,20 +103,27 @@ test('will reject items when task queue is unavailable', async () => {
maxThreads: 1,
maxQueue: 0,
})
const promises: Promise<void>[] = []

expect(pool.threads.length).toBe(0)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
)
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/No task queue available and all Workers are busy/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/No task queue available and all Workers are busy/
)
)

await pool.destroy()
await Promise.all(promises)
})

test('will reject items when task queue is unavailable (fixed thread count)', async () => {
Expand All @@ -112,20 +133,27 @@ test('will reject items when task queue is unavailable (fixed thread count)', as
maxThreads: 1,
maxQueue: 0,
})
const promises: Promise<void>[] = []

expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/Terminating worker thread/
)
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

expect(pool.run('while (true) {}')).rejects.toThrow(
/No task queue available and all Workers are busy/
promises.push(
expect(pool.run('while (true) {}')).rejects.toThrow(
/No task queue available and all Workers are busy/
)
)

await pool.destroy()
await Promise.all(promises)
})

test('tasks can share a Worker if requested (both tests blocking)', async () => {
Expand All @@ -136,23 +164,29 @@ test('tasks can share a Worker if requested (both tests blocking)', async () =>
maxQueue: 0,
concurrentTasksPerWorker: 2,
})
const promises: Promise<void>[] = []

expect(pool.threads.length).toBe(0)
expect(pool.queueSize).toBe(0)

expect(
pool.run(new Int32Array(new SharedArrayBuffer(4)))
).rejects.toBeTruthy()
promises.push(
expect(
pool.run(new Int32Array(new SharedArrayBuffer(4)))
).rejects.toBeTruthy()
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

expect(
pool.run(new Int32Array(new SharedArrayBuffer(4)))
).rejects.toBeTruthy()
promises.push(
expect(
pool.run(new Int32Array(new SharedArrayBuffer(4)))
).rejects.toBeTruthy()
)
expect(pool.threads.length).toBe(1)
expect(pool.queueSize).toBe(0)

await pool.destroy()
await Promise.all(promises)
})

test('tasks can share a Worker if requested (both tests finish)', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/uncaught-exception-from-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('uncaught exception resets Worker', async () => {
filename: resolve(__dirname, 'fixtures/eval.js'),
})

expect(pool.run('throw new Error("not_caught")')).rejects.toThrow(
await expect(pool.run('throw new Error("not_caught")')).rejects.toThrow(
/not_caught/
)
})
Expand Down

0 comments on commit 5dccf89

Please sign in to comment.