|
1
|
|
|
import { equals } from 'pepka' |
|
2
|
|
|
import { createNew, timeout } from '../utils' |
|
3
|
|
|
import mockServer from '../mock/server' |
|
4
|
|
|
import { test } from '../suite' |
|
5
|
|
|
|
|
6
|
|
|
/** Test real streaming functionality with multiple chunks. */ |
|
7
|
|
|
test('stream-real', timeout(1.5e4, () => new Promise<void>(async (ff, rj) => { |
|
8
|
|
|
|
|
9
|
|
|
const {port} = await mockServer() |
|
10
|
|
|
let to = setTimeout(() => { |
|
11
|
|
|
console.log('STREAM-REAL: Timeout - cannot create') |
|
12
|
|
|
rj('cannot create') |
|
13
|
|
|
}, 2e2) |
|
14
|
|
|
const ws = await createNew({}, port) |
|
15
|
|
|
clearTimeout(to) |
|
16
|
|
|
|
|
17
|
|
|
to = setTimeout(() => rj('cannot ready'), 2e2) |
|
18
|
|
|
await ws.ready() |
|
19
|
|
|
clearTimeout(to) |
|
20
|
|
|
|
|
21
|
|
|
// Test real streaming with multiple chunks |
|
22
|
|
|
const streamMsg = {stream: true, multi: true, test: 'real-stream', chunks: [1, 2, 3], delay: 50} |
|
23
|
|
|
to = setTimeout(() => rj('stream timeout'), 5e3) |
|
24
|
|
|
|
|
25
|
|
|
try { |
|
26
|
|
|
const stream = ws.stream<typeof streamMsg, any>(streamMsg) |
|
27
|
|
|
const chunks: any[] = [] |
|
28
|
|
|
|
|
29
|
|
|
for await (const chunk of stream) { |
|
30
|
|
|
chunks.push(chunk) |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
clearTimeout(to) |
|
34
|
|
|
|
|
35
|
|
|
// Verify we got all chunks |
|
36
|
|
|
if (chunks.length !== 3) { |
|
37
|
|
|
return rj(`Expected 3 chunks, got ${chunks.length}`) |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Verify chunks are in order and have correct data |
|
41
|
|
|
for (let i = 0; i < 3; i++) { |
|
42
|
|
|
if (chunks[i].chunk !== i + 1) { |
|
43
|
|
|
return rj(`Chunk ${i} should be ${i + 1}, got ${chunks[i].chunk}`) |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Verify last chunk has done flag |
|
48
|
|
|
if (!chunks[2].done) { |
|
49
|
|
|
return rj('Last chunk should have done flag') |
|
50
|
|
|
} |
|
51
|
|
|
ff() |
|
52
|
|
|
} catch (error) { |
|
53
|
|
|
clearTimeout(to) |
|
54
|
|
|
console.log('STREAM-REAL TEST FAILED:', error) |
|
55
|
|
|
rj('stream real error: ' + error) |
|
56
|
|
|
} |
|
57
|
|
|
}) |
|
58
|
|
|
)) |