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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cmd-control-client-lib",
3
3
  "description": "Cmd-Client-Library",
4
- "version": "3.0.11",
4
+ "version": "3.0.16",
5
5
  "directories": {
6
6
  "lib": "./dist"
7
7
  },
@@ -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.warn = jest.fn();
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
- throw new Error(error);
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(CMDConnection["_socket"]).toBeUndefined();
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
- const secondError = new VControlErrorEvent("error");
53
- secondError.reConnectTimeout = 15000;
54
- expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
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
- const thirdError = new VControlErrorEvent("error");
61
- thirdError.reConnectTimeout = 30000;
62
- expect(connectionConfig.onError).toHaveBeenCalledWith(thirdError);
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
- const fourthError = new VControlErrorEvent("error");
69
- fourthError.reConnectTimeout = 45000;
70
- expect(connectionConfig.onError).toHaveBeenCalledWith(fourthError);
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
- const finalError = new VControlErrorEvent("error");
77
- finalError.reConnectTimeout = 60000;
78
- expect(connectionConfig.onError).toHaveBeenCalledWith(finalError);
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
- const lastTimeout = new VControlErrorEvent("error");
84
- lastTimeout.reConnectTimeout = 75000;
85
- expect(connectionConfig.onError).toHaveBeenCalledWith(lastTimeout);
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.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
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
  });
@@ -1,4 +0,0 @@
1
- export declare class VControlErrorEvent extends Event {
2
- reConnectTimeout?: number;
3
- isFatal?: boolean;
4
- }