Skip to content

Commit b8d3f9e

Browse files
committed
test: socket destroyed during connect
1 parent 34c5b4e commit b8d3f9e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

electron/main/game/__tests__/game-socket.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ describe('game-socket', () => {
244244
expect(mockSocket.writeSpy).toHaveBeenNthCalledWith(3, `\n\n`);
245245
});
246246

247-
it('throws error if socket is destroyed during connect', async () => {
247+
it('throws error if socket times out during connect', async () => {
248248
const socket = new GameSocketImpl({ credentials });
249249

250250
// ---
@@ -267,6 +267,38 @@ describe('game-socket', () => {
267267
expect(error).toEqual(new Error('[GAME:SOCKET:CONNECT:TIMEOUT] 5000'));
268268
}
269269
});
270+
271+
it('throws error if socket is destroyed during connect', async () => {
272+
const socket = new GameSocketImpl({ credentials });
273+
274+
// ---
275+
276+
try {
277+
// Connect to socket and begin listening for data.
278+
const socketDataPromise = socket.connect();
279+
280+
// Before the connect logic has a chance to know if the
281+
// socket has connected, issue a disconnect to flag it as destroyed.
282+
await socket.disconnect();
283+
284+
// Run timer so that the "wait until" logic runs.
285+
// Note, we don't run them async because otherwise vitest
286+
// treats the error as an unhandled promise rejection
287+
// when instead we want to actually catch it in this test.
288+
vi.runAllTimers();
289+
290+
// Now await the connect promise, which will reject due to destroyed.
291+
await socketDataPromise;
292+
293+
expect.unreachable('it should throw an error');
294+
} catch (error) {
295+
expect(error).toEqual(
296+
new Error(
297+
'[GAME:SOCKET:STATUS:DESTROYED] failed to connect to game server'
298+
)
299+
);
300+
}
301+
});
270302
});
271303

272304
describe('#disconnect', () => {

0 commit comments

Comments
 (0)