@tchain/tcabci-read-js-client 2.5.8 → 2.5.9

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/websocket_test.js DELETED
@@ -1,556 +0,0 @@
1
- import { describe, it, before } from 'mocha'
2
- import { expect } from 'chai'
3
- import { Options } from './websocketOptions.js'
4
- import WebSocket from 'ws'
5
- import { TWebSocket } from './websocket.js'
6
- import { createServer } from 'node:http'
7
- import { WebSocketServer } from 'ws'
8
-
9
- const waiter = (timeout = 100) => {
10
- return new Promise((resolve) => setTimeout(resolve, timeout))
11
- }
12
-
13
- let server,
14
- serverTwo,
15
- ws,
16
- wsTwo,
17
- sendMessage = (msg) => {
18
- ws.send(msg)
19
- },
20
- cameMessage = null
21
-
22
- describe('WebSocket', () => {
23
- before(() => {
24
- server = createServer({})
25
- serverTwo = createServer({})
26
- const wss = new WebSocketServer({ server }),
27
- wssTwo = new WebSocketServer({ server: serverTwo })
28
-
29
- wss.on('connection', function connection(w) {
30
- ws = w
31
- ws.on('error', console.error)
32
-
33
- ws.on('message', function message(data) {
34
- cameMessage = data
35
- })
36
- })
37
-
38
- wssTwo.on('connection', function connection(w) {
39
- wsTwo = w
40
- wsTwo.on('error', console.error)
41
-
42
- wsTwo.on('message', function message(data) {})
43
- })
44
-
45
- setTimeout(() => {
46
- server.listen(4442)
47
- serverTwo.listen(4444)
48
- })
49
- })
50
- after(() => {
51
- server.closeAllConnections()
52
- server.close()
53
-
54
- serverTwo.closeAllConnections()
55
- serverTwo.close()
56
- })
57
-
58
- it('constructor with options', async () => {
59
- const options = new Options('localhost', 100, false, 4442)
60
-
61
- options.setMaxRetries(1)
62
- options.setMaxConnectionDelay(100)
63
- options.setMinReconnectionDelay(100)
64
- options.setCustomWS(WebSocket)
65
- options.setDebug(true)
66
-
67
- const twSocket = new TWebSocket(options)
68
-
69
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
70
- })
71
-
72
- it('should error constructor with invalid argument', async () => {
73
- expect(() => {
74
- new TWebSocket('invalid')
75
- }).to.throw(Error)
76
- })
77
-
78
- it('connect/0', async () => {
79
- const options = new Options('localhost', 100, false, 4442)
80
-
81
- options.setMaxRetries(1)
82
- options.setMaxConnectionDelay(100)
83
- options.setMinReconnectionDelay(100)
84
- options.setCustomWS(WebSocket)
85
- options.setDebug(true)
86
-
87
- const twSocket = new TWebSocket(options)
88
-
89
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
90
-
91
- await twSocket.connect()
92
- await waiter(50)
93
- expect(twSocket.ready).to.be.eq(true)
94
- await twSocket.disconnect()
95
- })
96
-
97
- it('should error connect/0 already connected', async () => {
98
- const options = new Options('localhost', 100, false, 4442)
99
-
100
- options.setMaxRetries(1)
101
- options.setMaxConnectionDelay(100)
102
- options.setMinReconnectionDelay(100)
103
- options.setCustomWS(WebSocket)
104
- options.setDebug(true)
105
-
106
- const twSocket = new TWebSocket(options)
107
-
108
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
109
-
110
- await twSocket.connect()
111
- await waiter(50)
112
- expect(twSocket.ready).to.be.eq(true)
113
- let thw = null
114
-
115
- try {
116
- await twSocket.connect()
117
- } catch (e) {
118
- thw = e
119
- }
120
-
121
- expect(thw).to.be.an.instanceOf(Error)
122
-
123
- await twSocket.disconnect()
124
- })
125
-
126
- it('connect/1', async () => {
127
- const options = new Options('localhost', 100, false, 4442)
128
-
129
- options.setMaxRetries(1)
130
- options.setMaxConnectionDelay(100)
131
- options.setMinReconnectionDelay(100)
132
- options.setCustomWS(WebSocket)
133
- options.setDebug(true)
134
-
135
- const twSocket = new TWebSocket(options)
136
-
137
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
138
-
139
- await twSocket.connect()
140
- await waiter(50)
141
- expect(twSocket.ready).to.be.eq(true)
142
- await twSocket.connect(true)
143
- await waiter(50)
144
- await twSocket.disconnect()
145
- })
146
-
147
- it('reconnect/0', async () => {
148
- const options = new Options('localhost', 100, false, 4442)
149
-
150
- options.setMaxRetries(1)
151
- options.setMaxConnectionDelay(100)
152
- options.setMinReconnectionDelay(100)
153
- options.setCustomWS(WebSocket)
154
- options.setDebug(true)
155
-
156
- const twSocket = new TWebSocket(options)
157
-
158
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
159
-
160
- await twSocket.connect()
161
- await waiter(50)
162
- await twSocket.reconnect()
163
- await waiter(50)
164
- expect(twSocket.ready).to.be.eq(true)
165
- await twSocket.disconnect()
166
- })
167
-
168
- it('disconnect/1', async () => {
169
- const options = new Options('localhost', 100, false, 4442)
170
-
171
- options.setMaxRetries(1)
172
- options.setMaxConnectionDelay(100)
173
- options.setMinReconnectionDelay(100)
174
- options.setCustomWS(WebSocket)
175
- options.setDebug(true)
176
-
177
- const twSocket = new TWebSocket(options)
178
-
179
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
180
-
181
- await twSocket.connect()
182
- await waiter(50)
183
- expect(twSocket.ready).to.be.eq(true)
184
- await twSocket.disconnect()
185
- await waiter(50)
186
- expect(twSocket.ready).to.be.eq(false)
187
- })
188
-
189
- it('disconnect/1', async () => {
190
- const options = new Options('localhost', 100, false, 4442)
191
-
192
- options.setMaxRetries(1)
193
- options.setMaxConnectionDelay(100)
194
- options.setMinReconnectionDelay(100)
195
- options.setCustomWS(WebSocket)
196
- options.setDebug(true)
197
-
198
- const twSocket = new TWebSocket(options)
199
-
200
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
201
-
202
- await twSocket.connect()
203
- await waiter(50)
204
- expect(twSocket.ready).to.be.eq(true)
205
- await twSocket.disconnect()
206
- await waiter(50)
207
- expect(twSocket.ready).to.be.eq(false)
208
- })
209
-
210
- it('send/1', async () => {
211
- const options = new Options('localhost', 100, false, 4442)
212
-
213
- options.setMaxRetries(1)
214
- options.setMaxConnectionDelay(100)
215
- options.setMinReconnectionDelay(100)
216
- options.setCustomWS(WebSocket)
217
- options.setDebug(true)
218
-
219
- const twSocket = new TWebSocket(options)
220
-
221
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
222
-
223
- await twSocket.connect()
224
- await waiter(50)
225
- expect(twSocket.ready).to.be.eq(true)
226
-
227
- twSocket.send('test1')
228
- await waiter(50)
229
- expect(new TextDecoder().decode(cameMessage)).to.be.eq('test1')
230
-
231
- await twSocket.disconnect()
232
- })
233
-
234
- it('addOpenListener/1', async () => {
235
- const options = new Options('localhost', 100, false, 4442)
236
-
237
- options.setMaxRetries(1)
238
- options.setMaxConnectionDelay(100)
239
- options.setMinReconnectionDelay(100)
240
- options.setCustomWS(WebSocket)
241
- options.setDebug(true)
242
-
243
- let opened = false,
244
- openCb = () => {
245
- opened = true
246
- }
247
-
248
- const twSocket = new TWebSocket(options)
249
-
250
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
251
-
252
- twSocket.addOpenListener(openCb)
253
-
254
- await twSocket.connect()
255
- await waiter(50)
256
- expect(twSocket.ready).to.be.eq(true)
257
- expect(opened).to.be.eq(true)
258
- await twSocket.disconnect()
259
- })
260
-
261
- it('addOpenListener/1 and opened listener getter', async () => {
262
- const options = new Options('localhost', 100, false, 4442)
263
-
264
- options.setMaxRetries(1)
265
- options.setMaxConnectionDelay(100)
266
- options.setMinReconnectionDelay(100)
267
- options.setCustomWS(WebSocket)
268
- options.setDebug(true)
269
-
270
- let opened = false,
271
- openCb = () => {
272
- opened = true
273
- }
274
-
275
- const twSocket = new TWebSocket(options)
276
-
277
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
278
-
279
- twSocket.addOpenListener(openCb)
280
- expect(twSocket.openListeners.length).to.be.eq(1)
281
- })
282
-
283
- it('removeOpenListener/1', async () => {
284
- const options = new Options('localhost', 100, false, 4442)
285
-
286
- options.setMaxRetries(1)
287
- options.setMaxConnectionDelay(100)
288
- options.setMinReconnectionDelay(100)
289
- options.setCustomWS(WebSocket)
290
- options.setDebug(true)
291
-
292
- let opened = false,
293
- openCb = () => {
294
- opened = true
295
- }
296
-
297
- const twSocket = new TWebSocket(options)
298
-
299
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
300
-
301
- twSocket.addOpenListener(openCb)
302
- expect(twSocket.openListeners.length).to.be.eq(1)
303
- twSocket.removeOpenListener(openCb)
304
- expect(twSocket.openListeners.length).to.be.eq(0)
305
- })
306
-
307
- it('addMessageListener/1', async () => {
308
- const options = new Options('localhost', 100, false, 4442)
309
-
310
- options.setMaxRetries(1)
311
- options.setMaxConnectionDelay(100)
312
- options.setMinReconnectionDelay(100)
313
- options.setCustomWS(WebSocket)
314
- options.setDebug(true)
315
-
316
- let came = null,
317
- messageCb = (msg) => {
318
- came = msg.data
319
- }
320
-
321
- const twSocket = new TWebSocket(options)
322
-
323
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
324
-
325
- twSocket.addMessageListener(messageCb)
326
-
327
- await twSocket.connect()
328
- await waiter(50)
329
- expect(twSocket.ready).to.be.eq(true)
330
-
331
- sendMessage(JSON.stringify({ key: 'value' }))
332
- await waiter(100)
333
- expect(JSON.parse(came)).to.deep.eq({ key: 'value' })
334
- await twSocket.disconnect()
335
- })
336
-
337
- it('addMessageListener/1 and message listener getter', async () => {
338
- const options = new Options('localhost', 100, false, 4442)
339
-
340
- options.setMaxRetries(1)
341
- options.setMaxConnectionDelay(100)
342
- options.setMinReconnectionDelay(100)
343
- options.setCustomWS(WebSocket)
344
- options.setDebug(true)
345
-
346
- let came = false,
347
- messageCb = () => {
348
- came = true
349
- }
350
-
351
- const twSocket = new TWebSocket(options)
352
-
353
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
354
-
355
- twSocket.addMessageListener(messageCb)
356
- expect(twSocket.messageListeners.length).to.be.eq(1)
357
- })
358
-
359
- it('removeMessageListener/1', async () => {
360
- const options = new Options('localhost', 100, false, 4442)
361
-
362
- options.setMaxRetries(1)
363
- options.setMaxConnectionDelay(100)
364
- options.setMinReconnectionDelay(100)
365
- options.setCustomWS(WebSocket)
366
- options.setDebug(true)
367
-
368
- let came = false,
369
- messageCb = () => {
370
- came = true
371
- }
372
-
373
- const twSocket = new TWebSocket(options)
374
-
375
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
376
-
377
- twSocket.addMessageListener(messageCb)
378
- expect(twSocket.messageListeners.length).to.be.eq(1)
379
- twSocket.removeMessageListener(messageCb)
380
- expect(twSocket.messageListeners.length).to.be.eq(0)
381
- })
382
-
383
- it('addErrorListener/1', async () => {
384
- const options = new Options('localhost', 100, false, 4444)
385
-
386
- options.setMaxRetries(1)
387
- options.setMaxConnectionDelay(100)
388
- options.setMinReconnectionDelay(100)
389
- options.setCustomWS(WebSocket)
390
- options.setDebug(true)
391
-
392
- let came = null,
393
- errorCb = (e) => {
394
- came = e.error.code
395
- }
396
-
397
- const twSocket = new TWebSocket(options)
398
-
399
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
400
-
401
- twSocket.addErrorListener(errorCb)
402
-
403
- serverTwo.closeAllConnections()
404
- serverTwo.close()
405
-
406
- await waiter(50)
407
-
408
- await twSocket.connect()
409
- await waiter(100)
410
-
411
- expect(twSocket.ready).to.be.eq(false)
412
- expect(came).to.be.eq('ECONNREFUSED')
413
- await twSocket.disconnect()
414
- })
415
-
416
- it('addErrorListener/1 and message listener getter', async () => {
417
- const options = new Options('localhost', 100, false, 4442)
418
-
419
- options.setMaxRetries(1)
420
- options.setMaxConnectionDelay(100)
421
- options.setMinReconnectionDelay(100)
422
- options.setCustomWS(WebSocket)
423
- options.setDebug(true)
424
-
425
- let came = false,
426
- errorCb = () => {
427
- came = true
428
- }
429
-
430
- const twSocket = new TWebSocket(options)
431
-
432
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
433
-
434
- twSocket.addErrorListener(errorCb)
435
- expect(twSocket.errorListeners.length).to.be.eq(1)
436
- })
437
-
438
- it('removeErrorListener/1', async () => {
439
- const options = new Options('localhost', 100, false, 4442)
440
-
441
- options.setMaxRetries(1)
442
- options.setMaxConnectionDelay(100)
443
- options.setMinReconnectionDelay(100)
444
- options.setCustomWS(WebSocket)
445
- options.setDebug(true)
446
-
447
- let came = false,
448
- errorCb = () => {
449
- came = true
450
- }
451
-
452
- const twSocket = new TWebSocket(options)
453
-
454
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
455
-
456
- twSocket.addErrorListener(errorCb)
457
- expect(twSocket.errorListeners.length).to.be.eq(1)
458
- twSocket.removeErrorListener(errorCb)
459
- expect(twSocket.errorListeners.length).to.be.eq(0)
460
- })
461
-
462
- it('addCloseListener/1', async () => {
463
- const options = new Options('localhost', 100, false, 4442)
464
-
465
- options.setMaxRetries(5)
466
- options.setMaxConnectionDelay(100)
467
- options.setMinReconnectionDelay(100)
468
- options.setCustomWS(WebSocket)
469
- options.setDebug(true)
470
-
471
- let came = null,
472
- closeCb = (e) => {
473
- came = e
474
- }
475
-
476
- const twSocket = new TWebSocket(options)
477
-
478
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
479
-
480
- twSocket.addCloseListener(closeCb)
481
-
482
- await twSocket.connect()
483
- await waiter(50)
484
- expect(twSocket.ready).to.be.eq(true)
485
- await twSocket.disconnect(1007)
486
- await twSocket.disconnect()
487
- })
488
-
489
- it('addCloseListener/1 and close listener getter', async () => {
490
- const options = new Options('localhost', 100, false, 4442)
491
-
492
- options.setMaxRetries(1)
493
- options.setMaxConnectionDelay(100)
494
- options.setMinReconnectionDelay(100)
495
- options.setCustomWS(WebSocket)
496
- options.setDebug(true)
497
-
498
- let came = false,
499
- closeCb = () => {
500
- came = true
501
- }
502
-
503
- const twSocket = new TWebSocket(options)
504
-
505
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
506
-
507
- twSocket.addCloseListener(closeCb)
508
- expect(twSocket.closeListeners.length).to.be.eq(1)
509
- })
510
-
511
- it('removeCloseListener/1', async () => {
512
- const options = new Options('localhost', 100, false, 4442)
513
-
514
- options.setMaxRetries(1)
515
- options.setMaxConnectionDelay(100)
516
- options.setMinReconnectionDelay(100)
517
- options.setCustomWS(WebSocket)
518
- options.setDebug(true)
519
-
520
- let came = false,
521
- closeCb = (e) => {
522
- came = true
523
- }
524
-
525
- const twSocket = new TWebSocket(options)
526
-
527
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
528
-
529
- twSocket.addCloseListener(closeCb)
530
- expect(twSocket.closeListeners.length).to.be.eq(1)
531
- twSocket.removeCloseListener(closeCb)
532
- expect(twSocket.closeListeners.length).to.be.eq(0)
533
- })
534
-
535
- it('should error addOpenListener/1 if exceeded maximum listener size', async () => {
536
- const options = new Options('localhost', 100, false, 4442)
537
-
538
- options.setMaxRetries(1)
539
- options.setMaxConnectionDelay(100)
540
- options.setMinReconnectionDelay(100)
541
- options.setCustomWS(WebSocket)
542
- options.setDebug(true)
543
-
544
- const twSocket = new TWebSocket(options)
545
-
546
- expect(twSocket).to.be.an.instanceOf(TWebSocket)
547
-
548
- expect(() => {
549
- for (let i = 0; i < 101; i++) {
550
- twSocket.addOpenListener(() => {
551
- console.log('cc1')
552
- })
553
- }
554
- }).to.throw(Error)
555
- })
556
- })