import { CmdConnection, ConnectionConfig } from "../src/cmd-connection"; import { logger } from "../src/logger"; describe("WebSocket connection", () => { it("Should open webSocket successfully", () => { // @ts-ignore global.WebSocket = jest.fn().mockImplementation(() => ({ send: jest.fn() })); const connectionConfig = new ConnectionConfig(); connectionConfig.https = true; connectionConfig.host = "hostMock"; connectionConfig.wssport = "wssPortMock"; connectionConfig.useWS = true; const CMDConnection = new CmdConnection(connectionConfig); CMDConnection.connect(); expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock"); //Socket was successfully opened and is able from the service expect(CMDConnection["_socket"]).not.toBeUndefined(); }); it("Should make 5 attempts if webSocket open failed", () => { logger.log = jest.fn(); jest.useFakeTimers(); // @ts-ignore global.WebSocket = jest.fn().mockImplementation(() => { return { readyState: 3 }; }); const connectionConfig = new ConnectionConfig(); connectionConfig.https = true; connectionConfig.host = "hostMock"; connectionConfig.wssport = "wssPortMock"; connectionConfig.useWS = true; connectionConfig.jsonp = false; connectionConfig.connectionRetryInterval = 15000; connectionConfig.onError = jest.fn(); const CMDConnection = new CmdConnection(connectionConfig); CMDConnection.connect(); expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock"); expect(global.WebSocket).toHaveBeenCalledTimes(1); expect(CMDConnection._reConnectAttemptsCount).toEqual(0); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); //Zero attempt (immediate reconnect) expect(setTimeout).toHaveBeenCalledTimes(1); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 0); const firstError = { reConnectionTimeout: 0 }; expect(connectionConfig.onError).toHaveBeenCalledWith(firstError); jest.runOnlyPendingTimers(); expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 0 ms"); expect(CMDConnection._reConnectAttemptsCount).toEqual(1); //First attempt expect(global.WebSocket).toHaveBeenCalledTimes(2); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const secondError = { reConnectionTimeout: 15000 }; expect(setTimeout).toHaveBeenCalledTimes(2); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 15000); expect(connectionConfig.onError).toHaveBeenCalledWith(secondError); expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 15000 ms"); expect(CMDConnection._reConnectAttemptsCount).toEqual(2); jest.runOnlyPendingTimers(); //Second attempt expect(global.WebSocket).toHaveBeenCalledTimes(3); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const thirdError = { reConnectionTimeout: 30000 }; expect(setTimeout).toHaveBeenCalledTimes(3); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000); expect(connectionConfig.onError).toHaveBeenCalledWith(thirdError); expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 30000 ms"); expect(CMDConnection._reConnectAttemptsCount).toEqual(3); jest.runOnlyPendingTimers(); //Third attempt expect(global.WebSocket).toHaveBeenCalledTimes(4); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const fourthError = { reConnectionTimeout: 45000 }; expect(setTimeout).toHaveBeenCalledTimes(4); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000); expect(connectionConfig.onError).toHaveBeenCalledWith(fourthError); expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 45000 ms"); expect(CMDConnection._reConnectAttemptsCount).toEqual(4); jest.runOnlyPendingTimers(); //Fourth attempt expect(global.WebSocket).toHaveBeenCalledTimes(5); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const fifthError = { reConnectionTimeout: 60000 }; expect(setTimeout).toHaveBeenCalledTimes(5); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000); expect(connectionConfig.onError).toHaveBeenCalledWith(fifthError); expect(CMDConnection._reConnectAttemptsCount).toEqual(5); jest.runOnlyPendingTimers(); //Final attempt expect(global.WebSocket).toHaveBeenCalledTimes(6); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const finalError = { reConnectionTimeout: 75000 }; expect(setTimeout).toHaveBeenCalledTimes(6); expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 75000); expect(connectionConfig.onError).toHaveBeenCalledWith(finalError); expect(CMDConnection._reConnectAttemptsCount).toEqual(6); jest.runOnlyPendingTimers(); expect(global.WebSocket).toHaveBeenCalledTimes(7); CMDConnection._socket.onerror(); CMDConnection._socket.onclose(); const fatalError = { isFatal: true }; expect(connectionConfig.onError).toHaveBeenCalledWith(fatalError); //No more setTimeout was called expect(setTimeout).toHaveBeenCalledTimes(6); expect(logger.log).toHaveBeenCalledWith("CMDP. maximum reconnect attempts count reached"); }); });