-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improve debugger loader message #9043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ClementPasteau
wants to merge
1
commit into
master
Choose a base branch
from
debugger-loader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
150 changes: 150 additions & 0 deletions
150
...src/ExportAndShare/LocalExporters/LocalPreviewLauncher/LocalPreviewDebuggerServer.spec.js
This file contains hidden or 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,150 @@ | ||
| // @flow | ||
| import { type PreviewDebuggerServerCallbacks } from '../../PreviewLauncher.flow'; | ||
|
|
||
| const mockIpcRendererListeners: Map<string, Array<Function>> = new Map(); | ||
| const mockIpcRenderer = { | ||
| on: jest.fn<[string, Function], void>(), | ||
| removeAllListeners: jest.fn<[string], void>(), | ||
| send: jest.fn<Array<any>, void>(), | ||
| }; | ||
|
|
||
| jest.mock('../../../Utils/OptionalRequire', () => | ||
| jest.fn((moduleName: string) => | ||
| moduleName === 'electron' ? { ipcRenderer: mockIpcRenderer } : null | ||
| ) | ||
| ); | ||
|
|
||
| /** Simulate a message sent by the Electron main process. */ | ||
| const emitFromMainProcess = (channel: string, payload: any) => | ||
| (mockIpcRendererListeners.get(channel) || []).forEach(listener => | ||
| listener({}, payload) | ||
| ); | ||
|
|
||
| /** | ||
| * The debugger server keeps its state at the module level, so each test must | ||
| * start from a freshly loaded module. | ||
| */ | ||
| const loadDebuggerServer = () => { | ||
| jest.resetModules(); | ||
| // $FlowFixMe[unsupported-syntax] - required to get a fresh module state. | ||
| return require('./LocalPreviewDebuggerServer').localPreviewDebuggerServer; | ||
| }; | ||
|
|
||
| const makeCallbacks = (): PreviewDebuggerServerCallbacks => ({ | ||
| onErrorReceived: jest.fn<Array<any>, void>(), | ||
| onServerStateChanged: jest.fn<Array<any>, void>(), | ||
| onConnectionClosed: jest.fn<Array<any>, void>(), | ||
| onConnectionOpened: jest.fn<Array<any>, void>(), | ||
| onConnectionErrored: jest.fn<Array<any>, void>(), | ||
| onHandleParsedMessage: jest.fn<Array<any>, void>(), | ||
| }); | ||
|
|
||
| describe('LocalPreviewDebuggerServer', () => { | ||
| beforeEach(() => { | ||
| // The server registers a listener for the embedded game frames on the window. | ||
| global.window = { addEventListener: jest.fn() }; | ||
| mockIpcRendererListeners.clear(); | ||
| // `resetMocks` is enabled, so the implementations are set for each test. | ||
| mockIpcRenderer.on.mockImplementation( | ||
| (channel: string, listener: Function) => { | ||
| mockIpcRendererListeners.set(channel, [ | ||
| ...(mockIpcRendererListeners.get(channel) || []), | ||
| listener, | ||
| ]); | ||
| } | ||
| ); | ||
| mockIpcRenderer.removeAllListeners.mockImplementation((channel: string) => { | ||
| mockIpcRendererListeners.delete(channel); | ||
| }); | ||
| jest.useFakeTimers(); | ||
| }); | ||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| delete global.window; | ||
| }); | ||
|
|
||
| it('is stopped until the server is started', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
|
|
||
| expect(debuggerServer.getServerState()).toBe('stopped'); | ||
| }); | ||
|
|
||
| it('is starting while waiting for the server to listen', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| const callbacks = makeCallbacks(); | ||
| debuggerServer.registerCallbacks(callbacks); | ||
|
|
||
| const startPromise = debuggerServer.startServer({}); | ||
| startPromise.catch(() => {}); | ||
|
|
||
| expect(debuggerServer.getServerState()).toBe('starting'); | ||
| expect(callbacks.onServerStateChanged).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('is started once the server is listening', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| const callbacks = makeCallbacks(); | ||
| debuggerServer.registerCallbacks(callbacks); | ||
|
|
||
| debuggerServer.startServer({}).catch(() => {}); | ||
| emitFromMainProcess('debugger-start-server-done', { | ||
| address: { address: '127.0.0.1', port: 3030 }, | ||
| }); | ||
|
|
||
| expect(debuggerServer.getServerState()).toBe('started'); | ||
| expect(callbacks.onServerStateChanged).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('goes back to stopped if the server does not start in time', async () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| const callbacks = makeCallbacks(); | ||
| debuggerServer.registerCallbacks(callbacks); | ||
|
|
||
| const startPromise = debuggerServer.startServer({}); | ||
| const startError = startPromise.catch(error => error); | ||
| jest.advanceTimersByTime(5000); | ||
|
|
||
| expect(await startError).toEqual(expect.any(Error)); | ||
| expect(debuggerServer.getServerState()).toBe('stopped'); | ||
| }); | ||
|
|
||
| it('stays started when the start timeout is reached after the server started', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| const callbacks = makeCallbacks(); | ||
| debuggerServer.registerCallbacks(callbacks); | ||
|
|
||
| debuggerServer.startServer({}).catch(() => {}); | ||
| emitFromMainProcess('debugger-start-server-done', { | ||
| address: { address: '127.0.0.1', port: 3030 }, | ||
| }); | ||
| jest.advanceTimersByTime(5000); | ||
|
|
||
| expect(debuggerServer.getServerState()).toBe('started'); | ||
| }); | ||
|
|
||
| it('goes back to stopped when the server errors', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| const callbacks = makeCallbacks(); | ||
| debuggerServer.registerCallbacks(callbacks); | ||
|
|
||
| debuggerServer.startServer({}).catch(() => {}); | ||
| emitFromMainProcess('debugger-error-received', new Error('Some error')); | ||
|
|
||
| expect(debuggerServer.getServerState()).toBe('stopped'); | ||
| expect(callbacks.onErrorReceived).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not send messages to a server that is not started yet', () => { | ||
| const debuggerServer = loadDebuggerServer(); | ||
| jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| const startPromise = debuggerServer.startServer({}); | ||
| startPromise.catch(() => {}); | ||
| debuggerServer.sendMessage('preview-ws-0', { command: 'play' }); | ||
|
|
||
| expect(mockIpcRenderer.send).not.toHaveBeenCalledWith( | ||
| 'debugger-send-message', | ||
| expect.anything() | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tends to mock the whole world so it's ok but to be challenged again in the future if this brings more complexity than actual real checks