test/specs/existing-socket.ts   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 0

Size

Lines of Code 46
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 41
mnd 5
bc 5
fnc 0
dl 0
loc 46
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { createNew } from '../utils'
2
import mockServer from '../mock/server'
3
4
import WS from 'ws'
5
import { test } from '../suite'
6
import { equals } from 'pepka'
7
8
9
const addr = (port: number) => 'ws://localhost:' + port
10
11
/** If an existing socket connection is provided via config. */
12
test('existing_socket', () => new Promise(async (ff, rj) => {
13
  const {port} = await mockServer()
14
  const to = setTimeout(() => rj(), 4e4)
15
  const existing_addr = addr(port)
16
17
  const ws1 = await createNew({socket: new WS(existing_addr)}, port)
18
19
  const msg1 = {echo: true, msg: 'existing_socket!'}
20
  const response1 = await ws1.send(msg1)
21
22
  if(!equals(response1, msg1)) return rj('Bad echo.')
23
  await ws1.close()
24
25
  const ws2_0 = new WS(existing_addr)
26
27
  ws2_0.addEventListener('open', async () => {
28
    console.log('START')
29
    const ws2 = await createNew({socket: ws2_0}, port)
30
    console.log({ws1})
31
32
    if(ws2.socket?.readyState !== 1) return rj('Bad echo.')
33
  
34
    const msg2 = {echo: true, msg: 'existing_socket!'}
35
    const response2 = await ws2.send(msg2)
36
37
    if(
38
      ws2.socket?.readyState as number !== 1
39
      || !equals(response2, msg2)
40
    ) return rj('not ready.')
41
    await ws2.close()
42
43
    clearTimeout(to)
44
    ff()
45
  })
46
}))