cmd-control-client-lib 3.0.11 → 3.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@types/index.d.ts +1 -0
- package/dist/@types/reconnection-error-event.d.ts +5 -0
- package/dist/cmd-connection.d.ts +3 -2
- package/dist/cmd-control-client-lib.js +1 -1
- package/dist/cmd-control-client-lib.js.map +1 -1
- package/package.json +1 -1
- package/test/open-web-socket.spec.ts +50 -96
- package/dist/v-control-error-event.d.ts +0 -4
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CmdConnection, ConnectionConfig } from "../src/cmd-connection";
|
|
2
2
|
import { logger } from "../src/logger";
|
|
3
|
-
import { VControlErrorEvent } from "../src/v-control-error-event";
|
|
4
3
|
|
|
5
4
|
describe("WebSocket connection", () => {
|
|
6
5
|
it("Should open webSocket successfully", () => {
|
|
@@ -20,14 +19,11 @@ describe("WebSocket connection", () => {
|
|
|
20
19
|
});
|
|
21
20
|
|
|
22
21
|
it("Should make 5 attempts if webSocket open failed", () => {
|
|
23
|
-
logger.
|
|
22
|
+
logger.log = jest.fn();
|
|
24
23
|
jest.useFakeTimers();
|
|
25
|
-
const error = "Connection failed Mock";
|
|
26
24
|
// @ts-ignore
|
|
27
25
|
global.WebSocket = jest.fn().mockImplementation(() => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return {};
|
|
26
|
+
return { readyState: 3 };
|
|
31
27
|
});
|
|
32
28
|
|
|
33
29
|
const connectionConfig = new ConnectionConfig();
|
|
@@ -42,122 +38,80 @@ describe("WebSocket connection", () => {
|
|
|
42
38
|
const CMDConnection = new CmdConnection(connectionConfig);
|
|
43
39
|
CMDConnection.connect();
|
|
44
40
|
expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
|
|
45
|
-
expect(
|
|
41
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(0);
|
|
43
|
+
|
|
44
|
+
CMDConnection._socket.onerror();
|
|
45
|
+
CMDConnection._socket.onclose();
|
|
46
46
|
//First attempt
|
|
47
47
|
expect(setTimeout).toHaveBeenCalledTimes(1);
|
|
48
48
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 15000);
|
|
49
49
|
|
|
50
|
+
const firstError = new CustomEvent("connection-error", { detail: { reconnectionTimeout: 15000 } });
|
|
51
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(firstError);
|
|
50
52
|
jest.runOnlyPendingTimers();
|
|
53
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 15000 ms");
|
|
54
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(1);
|
|
55
|
+
|
|
51
56
|
//Second attempt
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(2);
|
|
58
|
+
CMDConnection._socket.onerror();
|
|
59
|
+
CMDConnection._socket.onclose();
|
|
60
|
+
|
|
61
|
+
const secondError = new CustomEvent("connection-error", { detail: { reconnectionTimeout: 30000 } });
|
|
55
62
|
expect(setTimeout).toHaveBeenCalledTimes(2);
|
|
56
63
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000);
|
|
64
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
|
|
65
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 30000 ms");
|
|
66
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(2);
|
|
57
67
|
|
|
58
68
|
jest.runOnlyPendingTimers();
|
|
59
69
|
//Third attempt
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(3);
|
|
71
|
+
CMDConnection._socket.onerror();
|
|
72
|
+
CMDConnection._socket.onclose();
|
|
73
|
+
|
|
74
|
+
const thirdError = new CustomEvent("connection-error", { detail: { reconnectionTimeout: 45000 } });
|
|
63
75
|
expect(setTimeout).toHaveBeenCalledTimes(3);
|
|
64
76
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000);
|
|
77
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(thirdError);
|
|
78
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 45000 ms");
|
|
79
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(3);
|
|
65
80
|
|
|
66
81
|
jest.runOnlyPendingTimers();
|
|
67
82
|
//Fourth attempt
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(4);
|
|
84
|
+
CMDConnection._socket.onerror();
|
|
85
|
+
CMDConnection._socket.onclose();
|
|
86
|
+
|
|
87
|
+
const fourthError = new CustomEvent("connection-error", { detail: { reconnectionTimeout: 60000 } });
|
|
71
88
|
expect(setTimeout).toHaveBeenCalledTimes(4);
|
|
72
89
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000);
|
|
90
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(fourthError);
|
|
91
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 60000 ms");
|
|
92
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(4);
|
|
73
93
|
|
|
74
94
|
jest.runOnlyPendingTimers();
|
|
75
95
|
//Final attempt
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
96
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(5);
|
|
97
|
+
CMDConnection._socket.onerror();
|
|
98
|
+
CMDConnection._socket.onclose();
|
|
99
|
+
|
|
100
|
+
const fifthError = new CustomEvent("connection-error", { detail: { reconnectionTimeout: 75000 } });
|
|
79
101
|
expect(setTimeout).toHaveBeenCalledTimes(5);
|
|
80
102
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 75000);
|
|
103
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(fifthError);
|
|
104
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(5);
|
|
81
105
|
|
|
82
106
|
jest.runOnlyPendingTimers();
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
107
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(6);
|
|
108
|
+
CMDConnection._socket.onerror();
|
|
109
|
+
CMDConnection._socket.onclose();
|
|
110
|
+
|
|
111
|
+
const fatalError = new CustomEvent("connection-error", { detail: { isFatal: true } });
|
|
112
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(fatalError);
|
|
86
113
|
//No more setTimeout was called
|
|
87
114
|
expect(setTimeout).toHaveBeenCalledTimes(5);
|
|
88
|
-
expect(logger.
|
|
89
|
-
"CMDP._openSocket SECURITY_ERR, maximum reconnect attempts count reached",
|
|
90
|
-
new Error(error),
|
|
91
|
-
);
|
|
92
|
-
//Calling error function that was provided through settings with fatal:true flag
|
|
93
|
-
const fatalError = new VControlErrorEvent("error");
|
|
94
|
-
fatalError.isFatal = true;
|
|
95
|
-
expect(connectionConfig.onError).toHaveBeenCalledWith(fatalError);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("Should make up to 5 attempts if opened websocket failed", () => {
|
|
99
|
-
logger.log = jest.fn();
|
|
100
|
-
logger.warn = jest.fn();
|
|
101
|
-
jest.useFakeTimers();
|
|
102
|
-
// @ts-ignore
|
|
103
|
-
global.WebSocket = jest.fn().mockImplementationOnce(() => ({ send: jest.fn() }));
|
|
104
|
-
const connectionConfig = new ConnectionConfig();
|
|
105
|
-
connectionConfig.https = true;
|
|
106
|
-
connectionConfig.host = "hostMock";
|
|
107
|
-
connectionConfig.wssport = "wssPortMock";
|
|
108
|
-
connectionConfig.useWS = true;
|
|
109
|
-
connectionConfig.connectionRetryInterval = 15000;
|
|
110
|
-
connectionConfig.onError = jest.fn();
|
|
111
|
-
|
|
112
|
-
const CMDConnection = new CmdConnection(connectionConfig);
|
|
113
|
-
CMDConnection.connect();
|
|
114
|
-
expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
|
|
115
|
-
//Socket is opened
|
|
116
|
-
expect(CMDConnection["_socket"]).not.toBeUndefined();
|
|
117
|
-
|
|
118
|
-
const closeEvent = { reason: "Mocked WebSocket close imitation" } as CloseEvent;
|
|
119
|
-
CMDConnection["_closing"] = false;
|
|
120
|
-
// @ts-ignore
|
|
121
|
-
CMDConnection._onclose(closeEvent);
|
|
122
|
-
expect(logger.log).toHaveBeenCalledWith("CMDP.socket.onclose", closeEvent);
|
|
123
|
-
expect(logger.log).toHaveBeenCalledWith("CMDP.retry connect in 15000 ms");
|
|
124
|
-
|
|
125
|
-
const error = "Connection failed Mock";
|
|
126
|
-
// @ts-ignore
|
|
127
|
-
global.WebSocket = jest.fn().mockImplementation(() => {
|
|
128
|
-
throw new Error(error);
|
|
129
|
-
|
|
130
|
-
return {};
|
|
131
|
-
});
|
|
132
|
-
jest.runOnlyPendingTimers();
|
|
133
|
-
//Socket was killed
|
|
134
|
-
expect(CMDConnection["_socket"]).toBeUndefined();
|
|
135
|
-
expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
|
|
136
|
-
expect(setTimeout).toHaveBeenCalledTimes(2);
|
|
137
|
-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000);
|
|
138
|
-
jest.runOnlyPendingTimers();
|
|
139
|
-
|
|
140
|
-
const firstError = new VControlErrorEvent("error");
|
|
141
|
-
firstError.reConnectTimeout = 30000;
|
|
142
|
-
expect(connectionConfig.onError).toHaveBeenCalledWith(firstError);
|
|
143
|
-
expect(setTimeout).toHaveBeenCalledTimes(3);
|
|
144
|
-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000);
|
|
145
|
-
jest.runOnlyPendingTimers();
|
|
146
|
-
|
|
147
|
-
const secondError = new VControlErrorEvent("error");
|
|
148
|
-
secondError.reConnectTimeout = 45000;
|
|
149
|
-
expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
|
|
150
|
-
expect(setTimeout).toHaveBeenCalledTimes(4);
|
|
151
|
-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000);
|
|
152
|
-
|
|
153
|
-
//Imitating reconnect success on fourth attempt
|
|
154
|
-
//Socket can be opened successfully
|
|
155
|
-
// @ts-ignore
|
|
156
|
-
global.WebSocket = jest.fn().mockImplementationOnce(() => ({ send: jest.fn() }));
|
|
157
|
-
jest.runOnlyPendingTimers();
|
|
158
|
-
expect(setTimeout).toHaveBeenCalledTimes(4);
|
|
159
|
-
//Socket was successfully reconnected
|
|
160
|
-
expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
|
|
161
|
-
expect(CMDConnection["_socket"]).not.toBeUndefined();
|
|
115
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. maximum reconnect attempts count reached");
|
|
162
116
|
});
|
|
163
117
|
});
|