cmd-control-client-lib 3.0.11 → 3.0.12
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/package.json
CHANGED
|
@@ -20,14 +20,12 @@ describe("WebSocket connection", () => {
|
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
it("Should make 5 attempts if webSocket open failed", () => {
|
|
23
|
-
logger.
|
|
23
|
+
logger.log = jest.fn();
|
|
24
24
|
jest.useFakeTimers();
|
|
25
25
|
const error = "Connection failed Mock";
|
|
26
26
|
// @ts-ignore
|
|
27
27
|
global.WebSocket = jest.fn().mockImplementation(() => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return {};
|
|
28
|
+
return { readyState: 3 };
|
|
31
29
|
});
|
|
32
30
|
|
|
33
31
|
const connectionConfig = new ConnectionConfig();
|
|
@@ -42,122 +40,93 @@ describe("WebSocket connection", () => {
|
|
|
42
40
|
const CMDConnection = new CmdConnection(connectionConfig);
|
|
43
41
|
CMDConnection.connect();
|
|
44
42
|
expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
|
|
45
|
-
expect(
|
|
43
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(1);
|
|
44
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(0);
|
|
45
|
+
|
|
46
|
+
CMDConnection._socket.onerror();
|
|
47
|
+
CMDConnection._socket.onclose();
|
|
46
48
|
//First attempt
|
|
47
49
|
expect(setTimeout).toHaveBeenCalledTimes(1);
|
|
48
50
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 15000);
|
|
49
51
|
|
|
52
|
+
const firstError = new VControlErrorEvent("error");
|
|
53
|
+
firstError.reConnectTimeout = 15000;
|
|
54
|
+
firstError.isFatal = false;
|
|
55
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(firstError);
|
|
50
56
|
jest.runOnlyPendingTimers();
|
|
57
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 15000 ms");
|
|
58
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(1);
|
|
59
|
+
|
|
51
60
|
//Second attempt
|
|
61
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(2);
|
|
62
|
+
CMDConnection._socket.onerror();
|
|
63
|
+
CMDConnection._socket.onclose();
|
|
64
|
+
|
|
52
65
|
const secondError = new VControlErrorEvent("error");
|
|
53
|
-
secondError.reConnectTimeout =
|
|
54
|
-
|
|
66
|
+
secondError.reConnectTimeout = 30000;
|
|
67
|
+
secondError.isFatal = false;
|
|
55
68
|
expect(setTimeout).toHaveBeenCalledTimes(2);
|
|
56
69
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000);
|
|
70
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
|
|
71
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 30000 ms");
|
|
72
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(2);
|
|
57
73
|
|
|
58
74
|
jest.runOnlyPendingTimers();
|
|
59
75
|
//Third attempt
|
|
76
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(3);
|
|
77
|
+
CMDConnection._socket.onerror();
|
|
78
|
+
CMDConnection._socket.onclose();
|
|
79
|
+
|
|
60
80
|
const thirdError = new VControlErrorEvent("error");
|
|
61
|
-
thirdError.reConnectTimeout =
|
|
62
|
-
|
|
81
|
+
thirdError.reConnectTimeout = 45000;
|
|
82
|
+
thirdError.isFatal = false;
|
|
63
83
|
expect(setTimeout).toHaveBeenCalledTimes(3);
|
|
64
84
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000);
|
|
85
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(thirdError);
|
|
86
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 45000 ms");
|
|
87
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(3);
|
|
65
88
|
|
|
66
89
|
jest.runOnlyPendingTimers();
|
|
67
90
|
//Fourth attempt
|
|
91
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(4);
|
|
92
|
+
CMDConnection._socket.onerror();
|
|
93
|
+
CMDConnection._socket.onclose();
|
|
94
|
+
|
|
68
95
|
const fourthError = new VControlErrorEvent("error");
|
|
69
|
-
fourthError.reConnectTimeout =
|
|
70
|
-
|
|
96
|
+
fourthError.reConnectTimeout = 60000;
|
|
97
|
+
fourthError.isFatal = false;
|
|
71
98
|
expect(setTimeout).toHaveBeenCalledTimes(4);
|
|
72
99
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000);
|
|
100
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(fourthError);
|
|
101
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. retry connect in 60000 ms");
|
|
102
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(4);
|
|
73
103
|
|
|
74
104
|
jest.runOnlyPendingTimers();
|
|
75
105
|
//Final attempt
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
106
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(5);
|
|
107
|
+
CMDConnection._socket.onerror();
|
|
108
|
+
CMDConnection._socket.onclose();
|
|
109
|
+
|
|
110
|
+
const fifthError = new VControlErrorEvent("error");
|
|
111
|
+
fifthError.reConnectTimeout = 75000;
|
|
112
|
+
fifthError.isFatal = false;
|
|
79
113
|
expect(setTimeout).toHaveBeenCalledTimes(5);
|
|
80
114
|
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 75000);
|
|
115
|
+
expect(connectionConfig.onError).toHaveBeenCalledWith(fifthError);
|
|
116
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(5);
|
|
81
117
|
|
|
82
118
|
jest.runOnlyPendingTimers();
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
//No more setTimeout was called
|
|
87
|
-
expect(setTimeout).toHaveBeenCalledTimes(5);
|
|
88
|
-
expect(logger.warn).toHaveBeenCalledWith(
|
|
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
|
|
119
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(6);
|
|
120
|
+
CMDConnection._socket.onerror();
|
|
121
|
+
CMDConnection._socket.onclose();
|
|
93
122
|
const fatalError = new VControlErrorEvent("error");
|
|
123
|
+
fatalError.reConnectTimeout = 90000;
|
|
94
124
|
fatalError.isFatal = true;
|
|
125
|
+
//No more setTimeout was called
|
|
126
|
+
expect(setTimeout).toHaveBeenCalledTimes(5);
|
|
127
|
+
expect(global.WebSocket).toHaveBeenCalledTimes(6);
|
|
95
128
|
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();
|
|
129
|
+
expect(logger.log).toHaveBeenCalledWith("CMDP. maximum reconnect attempts count reached");
|
|
130
|
+
expect(CMDConnection._reConnectAttemptsCount).toEqual(0);
|
|
162
131
|
});
|
|
163
132
|
});
|