summaryrefslogtreecommitdiff
path: root/test/parallel/test-whatwg-readablestream.mjs
blob: 57ebed604542a3e6ee8c2aa36f3a5ef56a044099 (plain)
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
import { mustCall } from '../common/index.mjs';
import { ReadableStream } from 'stream/web';
import assert from 'assert';

{
  // Test tee() with close in the nextTick after enqueue
  async function read(stream) {
    const chunks = [];
    for await (const chunk of stream)
      chunks.push(chunk);
    return Buffer.concat(chunks).toString();
  }

  const [r1, r2] = new ReadableStream({
    start(controller) {
      process.nextTick(() => {
        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));

        process.nextTick(() => {
          controller.close();
        });
      });
    }
  }).tee();

  (async () => {
    const [dataReader1, dataReader2] = await Promise.all([
      read(r1),
      read(r2),
    ]);

    assert.strictEqual(dataReader1, dataReader2);
    assert.strictEqual(dataReader1, 'foobar');
    assert.strictEqual(dataReader2, 'foobar');
  })().then(mustCall());
}

{
  // Test ReadableByteStream.tee() with close in the nextTick after enqueue
  async function read(stream) {
    const chunks = [];
    for await (const chunk of stream)
      chunks.push(chunk);
    return Buffer.concat(chunks).toString();
  }

  const [r1, r2] = new ReadableStream({
    type: 'bytes',
    start(controller) {
      process.nextTick(() => {
        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));

        process.nextTick(() => {
          controller.close();
        });
      });
    }
  }).tee();

  (async () => {
    const [dataReader1, dataReader2] = await Promise.all([
      read(r1),
      read(r2),
    ]);

    assert.strictEqual(dataReader1, dataReader2);
    assert.strictEqual(dataReader1, 'foobar');
    assert.strictEqual(dataReader2, 'foobar');
  })().then(mustCall());
}