-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(engine): report worker crashes correctly (#43)
* fix: update typescript for Promise related errors * test: add failing test case for #43 * fix(engine): report worker crashes correctly - Caused by previous result streaming feature - Reported by latest Typescript
- Loading branch information
1 parent
e711cd2
commit 82cb301
Showing
8 changed files
with
151 additions
and
32 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
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,33 @@ | ||
export const isMainThread = true; | ||
|
||
type Listener = (...args: any[]) => void; | ||
|
||
interface Listeners { | ||
message: Listener[]; | ||
error: Listener[]; | ||
exit: Listener[]; | ||
} | ||
|
||
const listeners: Listeners = { | ||
message: [], | ||
error: [], | ||
exit: [], | ||
}; | ||
|
||
export const Emitter = { | ||
emit(event: keyof Listeners, data: unknown): void { | ||
listeners[event].forEach(listener => listener(data)); | ||
}, | ||
|
||
clear(): void { | ||
listeners.message.splice(0); | ||
listeners.error.splice(0); | ||
listeners.exit.splice(0); | ||
}, | ||
}; | ||
|
||
export class Worker { | ||
on(event: keyof Listeners, listener: Listener): void { | ||
listeners[event].push(listener); | ||
} | ||
} |
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,78 @@ | ||
import Engine from '@engine'; | ||
import { Emitter } from '__mocks__/worker_threads'; | ||
|
||
jest.mock('worker_threads', () => require('./__mocks__/worker_threads')); | ||
jest.unmock('@engine'); | ||
|
||
const REPOSITORY = 'test-repository'; | ||
const cleanup = jest.fn(); | ||
const workerCallback = jest.fn().mockReturnValue(cleanup); | ||
|
||
describe('engine', () => { | ||
afterEach(() => { | ||
Emitter.clear(); | ||
}); | ||
|
||
test('crashing worker is reported', async () => { | ||
const onMessage = jest.fn(); | ||
const promise = Engine.scanRepository( | ||
REPOSITORY, | ||
onMessage, | ||
workerCallback | ||
); | ||
|
||
Emitter.emit('error', { code: 'mock-error' }); | ||
|
||
expect(onMessage).toHaveBeenCalledWith({ | ||
type: 'WORKER_ERROR', | ||
payload: 'mock-error', | ||
}); | ||
|
||
expect(onMessage).toHaveBeenLastCalledWith({ | ||
type: 'ON_RESULT', | ||
payload: { | ||
messages: [ | ||
{ | ||
message: 'mock-error', | ||
path: 'test-repository', | ||
ruleId: '', | ||
column: 0, | ||
line: 0, | ||
severity: 0, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
await promise; | ||
}); | ||
|
||
test('unexpected worker exits are reported', async () => { | ||
const onMessage = jest.fn(); | ||
const promise = Engine.scanRepository( | ||
REPOSITORY, | ||
onMessage, | ||
workerCallback | ||
); | ||
|
||
Emitter.emit('exit', 987); | ||
|
||
expect(onMessage).toHaveBeenCalledWith({ | ||
payload: { | ||
messages: [ | ||
{ | ||
message: 'Worker exited with code 987', | ||
path: 'test-repository', | ||
ruleId: '', | ||
column: 0, | ||
line: 0, | ||
severity: 0, | ||
}, | ||
], | ||
}, | ||
type: 'ON_RESULT', | ||
}); | ||
|
||
await promise; | ||
}); | ||
}); |
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