summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/serial/serialPort_loopback_BufferOverrunError-manual.https.html
blob: 06f8271ac9af71655cb0bc89a8c63e9b7c34ef45 (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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="resources/common.js"></script>
    <script src="resources/manual.js"></script>
  </head>
  <body>
    <p>
      These tests require a connected serial device configured to act as a
      "loopback" device, with the transmit and receive pins wired together.
    </p>
    <script>
      manual_serial_test(async (t, port) => {
        await port.open({baudRate: 115200, bufferSize: 1024});

        // Create something much larger than bufferSize above.
        const data = new Uint8Array(16 * 1024);
        for (let i = 0; i < data.byteLength; ++i)
          data[i] = (i / 1024) & 0xff;

        // Completely write |data| to the port without waiting for it to be
        // received.
        const writer = port.writable.getWriter();
        writer.write(data);
        await writer.close();

        const reader = port.readable.getReader();
        const chunks = [];
        let actualLength = 0;
        while (true) {
          try {
            const {value, done} = await reader.read();
            if (value) {
              actualLength += value.byteLength;
              chunks.push(value);
            }
            if (done) {
              assert_unreached("Unexpected end of stream.");
              break;
            }
          } catch (e) {
            assert_equals(e.name, 'BufferOverrunError');
            break;
          }
        }
        reader.releaseLock();

        const buffer = new Uint8Array(actualLength);
        chunks.reduce((offset, chunk) => {
          buffer.set(chunk, offset);
          return offset + chunk.byteLength;
        }, 0);

        assert_greater_than(actualLength, 0);
        compareArrays(buffer, data.slice(0, actualLength));

        await port.close();
      }, 'Overflowing the receive buffer triggers an error.');
    </script>
  </body>
</html>