deno.land / std@0.177.1 / streams / iterate_reader_test.ts

iterate_reader_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { assertEquals } from "../testing/asserts.ts";// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { iterateReader, iterateReaderSync } from "./iterate_reader.ts";import { readerFromIterable } from "./reader_from_iterable.ts";import { delay } from "../async/delay.ts";import type { Reader, ReaderSync } from "../types.d.ts";
Deno.test("iterateReader", async () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder();
class TestReader implements Reader { #offset = 0; #buf: Uint8Array;
constructor(s: string) { this.#buf = new Uint8Array(encoder.encode(s)); }
read(p: Uint8Array): Promise<number | null> { const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset); p.set(this.#buf.slice(this.#offset, this.#offset + n)); this.#offset += n;
if (n === 0) { return Promise.resolve(null); }
return Promise.resolve(n); } }
const reader = new TestReader("hello world!");
let totalSize = 0; for await (const buf of iterateReader(reader)) { totalSize += buf.byteLength; }
assertEquals(totalSize, 12);});
Deno.test("iterateReader works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); const iter = iterateReader(readerFromIterable([a, b])); const promises = []; for await (const bytes of iter) { promises.push(delay(10).then(() => bytes)); } assertEquals([a, b], await Promise.all(promises));});
Deno.test("iterateReaderSync", () => { // ref: https://github.com/denoland/deno/issues/2330 const encoder = new TextEncoder();
class TestReader implements ReaderSync { #offset = 0; #buf: Uint8Array;
constructor(s: string) { this.#buf = new Uint8Array(encoder.encode(s)); }
readSync(p: Uint8Array): number | null { const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset); p.set(this.#buf.slice(this.#offset, this.#offset + n)); this.#offset += n;
if (n === 0) { return null; }
return n; } }
const reader = new TestReader("hello world!");
let totalSize = 0; for (const buf of iterateReaderSync(reader)) { totalSize += buf.byteLength; }
assertEquals(totalSize, 12);});
Deno.test("iterateReaderSync works with slow consumer", async () => { const a = new Uint8Array([97]); const b = new Uint8Array([98]); const data = [a, b]; const readerSync = { readSync(u8: Uint8Array) { const bytes = data.shift(); if (bytes) { u8.set(bytes); return bytes.length; } return null; }, }; const iter = iterateReaderSync(readerSync); const promises = []; for (const bytes of iter) { promises.push(delay(10).then(() => bytes)); } assertEquals([a, b], await Promise.all(promises));});
std

Version Info

Tagged at
10 months ago