summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/serial/serialPort_readable_chain.https.any.js
blob: 552567cdfe400aa9b00afde30761a7aae911d664 (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
// META: script=/resources/test-only-api.js
// META: script=/serial/resources/common.js
// META: script=resources/automation.js

serial_test(async (t, fake) => {
  const {port, fakePort} = await getFakeSerialPort(fake);
  // Select a buffer size larger than the amount of data transferred.
  await port.open({baudRate: 9600, bufferSize: 64});

  const decoder = new TextDecoderStream();
  const streamClosed = port.readable.pipeTo(decoder.writable);
  const readable = decoder.readable.pipeThrough(new TransformStream())
                       .pipeThrough(new TransformStream())
                       .pipeThrough(new TransformStream())
                       .pipeThrough(new TransformStream());
  const reader = readable.getReader();

  await fakePort.writable();
  fakePort.write(new TextEncoder().encode('Hello world!'));

  const {value, done} = await reader.read();
  assert_false(done);
  assert_equals('Hello world!', value);
  await reader.cancel('arbitrary reason');
  await streamClosed.catch(reason => {
    assert_equals('arbitrary reason', reason);
  });

  await port.close();
}, 'Stream closure is observable through a long chain of transforms');