|
1
|
|
|
import { equals } from 'pepka' |
|
2
|
|
|
import { createNew, timeout } from '../utils' |
|
3
|
|
|
import mockServer from '../mock/server' |
|
4
|
|
|
import { test } from '../suite' |
|
5
|
|
|
|
|
6
|
|
|
/** Simple test for stream method basic functionality. */ |
|
7
|
|
|
test('stream-basic', timeout(5e3, () => new Promise<void>(async (ff, rj) => { |
|
8
|
|
|
const {port} = await mockServer() |
|
9
|
|
|
let to = setTimeout(() => rj('cannot create'), 2e2) |
|
10
|
|
|
const ws = await createNew({}, port) |
|
11
|
|
|
clearTimeout(to) |
|
12
|
|
|
|
|
13
|
|
|
to = setTimeout(() => rj('cannot ready'), 2e2) |
|
14
|
|
|
await ws.ready() |
|
15
|
|
|
clearTimeout(to) |
|
16
|
|
|
|
|
17
|
|
|
const msg = {stream: true, test: 'stream'} |
|
18
|
|
|
to = setTimeout(() => rj('stream timeout'), 2e2) |
|
19
|
|
|
|
|
20
|
|
|
try { |
|
21
|
|
|
const stream = ws.stream(msg) |
|
22
|
|
|
|
|
23
|
|
|
// Test that stream is an AsyncGenerator |
|
24
|
|
|
if (typeof stream[Symbol.asyncIterator] !== 'function') { |
|
25
|
|
|
clearTimeout(to) |
|
26
|
|
|
return rj('stream is not an AsyncGenerator') |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Test that we can iterate over it |
|
30
|
|
|
const iterator = stream[Symbol.asyncIterator]() |
|
31
|
|
|
const firstResult = await iterator.next() |
|
32
|
|
|
|
|
33
|
|
|
clearTimeout(to) |
|
34
|
|
|
|
|
35
|
|
|
// For streaming messages, check that we got a valid chunk with the expected properties |
|
36
|
|
|
if (!firstResult.done && firstResult.value && firstResult.value.test === msg.test) { |
|
37
|
|
|
ff() |
|
38
|
|
|
} else { |
|
39
|
|
|
rj(`stream did not return expected value. Got: ${JSON.stringify(firstResult.value)}, Expected test: ${msg.test}`) |
|
40
|
|
|
} |
|
41
|
|
|
} catch (error) { |
|
42
|
|
|
clearTimeout(to) |
|
43
|
|
|
rj('stream error: ' + error) |
|
44
|
|
|
} |
|
45
|
|
|
}) |
|
46
|
|
|
)) |