Skip to content

Commit

Permalink
feat: add event to allow listening to side-effect errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Jul 22, 2022
1 parent 75685ae commit 4ce09c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const getTests = (disableMultithreading: boolean) => {

let threaded: ThreadedClass<TestClassErrors>
let onClosed = jest.fn()
let onError = jest.fn()
let onClosedListener: any
let onErrorListener: any

beforeAll(async () => {

Expand All @@ -22,6 +24,7 @@ const getTests = (disableMultithreading: boolean) => {

threaded = await threadedClass<TestClassErrors, typeof TestClassErrors>(TESTCLASS_PATH, 'TestClassErrors', [], { disableMultithreading })
onClosedListener = ThreadedClassManager.onEvent(threaded, 'thread_closed', onClosed)
onErrorListener = ThreadedClassManager.onEvent(threaded, 'error', onError)

})
beforeEach(() => {
Expand All @@ -34,6 +37,7 @@ const getTests = (disableMultithreading: boolean) => {
await ThreadedClassManager.destroy(threaded)
expect(ThreadedClassManager.getThreadCount()).toEqual(0)
onClosedListener.stop()
onErrorListener.stop();
expect(onClosed).toHaveBeenCalledTimes(1)
})

Expand All @@ -44,6 +48,13 @@ const getTests = (disableMultithreading: boolean) => {
await expect(threaded.doError()).rejects.toMatch(/testClassErrors.js/)
await expect(threaded.doError()).rejects.toMatch(/errors.spec/)
})
test('SyntaxError in called method', async () => {

await expect(threaded.doSyntaxError()).rejects.toMatch(/SyntaxError/)
// ensure that the original path is included in the stack-trace:
await expect(threaded.doSyntaxError()).rejects.toMatch(/testClassErrors.js/)
await expect(threaded.doSyntaxError()).rejects.toMatch(/errors.spec/)
})
test('Error in callback', async () => {
// Pre-test: check that cbError throws an error:
expect(returnError(cbError)).toMatch(/TestError in callback 123/)
Expand Down Expand Up @@ -154,6 +165,13 @@ const getTests = (disableMultithreading: boolean) => {
expect(err).toMatch(/errors.spec/)
})
}

test('Error thrown in a setTimeout', async () => {
await expect(threaded.doAsyncError()).resolves.toBeTruthy();
await sleep(10);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0].message).toMatch(/setTimeout/)
})
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/parent-process/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export class ThreadedClassManagerClass {
return this._internal.getMemoryUsage()
}
public onEvent (proxy: ThreadedClass<any>, event: string, cb: Function) {
const onEvent = (child: Child) => {
const onEvent = (child: Child, ...args: any[]) => {
let foundChild = Object.keys(child.instances).find((instanceId) => {
const instance = child.instances[instanceId]
return instance.proxy === proxy
})
if (foundChild) {
cb()
cb(...args)
}
}
this._internal.on(event, onEvent)
Expand Down
9 changes: 9 additions & 0 deletions test-lib/testClassErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class TestClassErrors extends events_1.EventEmitter {
doError() {
throw new Error('TestError in doError');
}
doSyntaxError() {
DaleATuCuerpoAlegría(Macarena)
}
doAsyncError() {
setTimeout(() => {
throw new Error('Error in setTimeout');
}, 1);
return true;
}
emitEvent(eventName) {
this.emit(eventName, 'testData');
// await Promise.resolve(this.emit(eventName, 'testData'))
Expand Down
11 changes: 11 additions & 0 deletions test-lib/testClassErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export class TestClassErrors extends EventEmitter {
public doError (): void {
throw new Error('TestError in doError')
}
public doSyntaxError (): void {
// @ts-ignore
DaleATuCuerpoAlegría(Macarena)
}
public doAsyncError (): boolean {
setTimeout(() => {
throw new Error('Error in setTimeout');
}, 1);

return true;
}
public emitEvent (eventName: string): void {
this.emit(eventName, 'testData')
// await Promise.resolve(this.emit(eventName, 'testData'))
Expand Down

0 comments on commit 4ce09c9

Please sign in to comment.