Skip to content

Commit 2987fa6

Browse files
reillyeonchromium-wpt-export-bot
authored andcommitted
serial: Add a test for large unidirectional transfers
This test attempts to receive data from a test serial device as quickly as possible in order to potentially reproduce issues with data loss. Bug: 1070798 Change-Id: I40a35c22fc2e3a31e31f4b1f038a2cc4e33fce37 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3120318 Auto-Submit: Reilly Grant <[email protected]> Commit-Queue: Reilly Grant <[email protected]> Reviewed-by: Chris Mumford <[email protected]> Cr-Commit-Position: refs/heads/main@{#927455}
1 parent 935bbb9 commit 2987fa6

6 files changed

+116
-9
lines changed

serial/resources/manual.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let manualTestPort = null;
22

33
navigator.serial.addEventListener('disconnect', (e) => {
4-
if (e.port === manualTestPort) {
4+
if (e.target === manualTestPort) {
55
manualTestPort = null;
66
}
77
})
@@ -31,7 +31,7 @@ async function getPortForManualTest() {
3131
return manualTestPort;
3232
}
3333

34-
function manual_loopback_serial_test(func, name, properties) {
34+
function manual_serial_test(func, name, properties) {
3535
promise_test(async (test) => {
3636
await func(test, await getPortForManualTest());
3737
}, name, properties);

serial/serialPort_disconnect-manual.https.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
These tests require a serial device to be connected and disconnected.
1414
</p>
1515
<script>
16-
manual_loopback_serial_test(async (t, port) => {
16+
manual_serial_test(async (t, port) => {
1717
const watcher = new EventWatcher(t, navigator.serial, ['disconnect']);
1818

1919
await port.open({baudRate: 115200, bufferSize: 1024});
@@ -47,7 +47,7 @@
4747
await port.close();
4848
}, 'Disconnect during read is detected.');
4949

50-
manual_loopback_serial_test(async (t, port) => {
50+
manual_serial_test(async (t, port) => {
5151
const watcher = new EventWatcher(t, navigator.serial, ['disconnect']);
5252

5353
await port.open({baudRate: 115200, bufferSize: 1024});

serial/serialPort_loopback-manual.https.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"loopback" device, with the transmit and receive pins wired together.
1515
</p>
1616
<script>
17-
manual_loopback_serial_test(async (t, port) => {
17+
manual_serial_test(async (t, port) => {
1818
await port.open({baudRate: 115200, bufferSize: 1024});
1919

2020
// Create something much smaller than bufferSize above.
@@ -39,7 +39,7 @@
3939
await port.close();
4040
}, 'Can perform a series of small writes.');
4141

42-
manual_loopback_serial_test(async (t, port) => {
42+
manual_serial_test(async (t, port) => {
4343
await port.open({baudRate: 115200, bufferSize: 1024});
4444

4545
// Create something much larger than bufferSize above.
@@ -64,7 +64,7 @@
6464
await port.close();
6565
}, 'Can perform a series of large writes.');
6666

67-
manual_loopback_serial_test(async (t, port) => {
67+
manual_serial_test(async (t, port) => {
6868
await port.open({baudRate: 115200, bufferSize: 64});
6969

7070
const writer = port.writable.getWriter();

serial/serialPort_loopback_BreakError-manual.https.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"loopback" device, with the transmit and receive pins wired together.
1515
</p>
1616
<script>
17-
manual_loopback_serial_test(async (t, port) => {
17+
manual_serial_test(async (t, port) => {
1818
await port.open({baudRate: 115200, bufferSize: 1024});
1919

2020
let reader = port.readable.getReader();

serial/serialPort_loopback_BufferOverrunError-manual.https.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"loopback" device, with the transmit and receive pins wired together.
1515
</p>
1616
<script>
17-
manual_loopback_serial_test(async (t, port) => {
17+
manual_serial_test(async (t, port) => {
1818
await port.open({baudRate: 115200, bufferSize: 1024});
1919

2020
// Create something much larger than bufferSize above.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title></title>
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<script src="resources/common.js"></script>
9+
<script src="resources/manual.js"></script>
10+
</head>
11+
<body>
12+
<p>
13+
These tests require a device configured with the following Arduino sketch:
14+
15+
<pre>
16+
uint32_t seed = 0;
17+
uint32_t bytesToSend = 0;
18+
19+
uint8_t nextByte() {
20+
seed = (1103515245 * seed + 12345) % 0x8000000;
21+
return (seed >> 16) & 0xFF;
22+
}
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
}
27+
28+
void loop() {
29+
if (!Serial) {
30+
return;
31+
}
32+
33+
if (bytesToSend == 0) {
34+
// Read the seed and number of bytes to send from the host as 32-bit
35+
// little-endian values.
36+
if (Serial.available() &lt 8) {
37+
return;
38+
}
39+
40+
uint8_t buf[8];
41+
Serial.readBytes((char*)buf, sizeof buf);
42+
seed = (uint32_t)buf[0] |
43+
((uint32_t)buf[1] &lt;&lt; 8) |
44+
((uint32_t)buf[2] &lt;&lt; 16) |
45+
((uint32_t)buf[3] &lt;&lt; 24);
46+
bytesToSend = (uint32_t)buf[4] |
47+
((uint32_t)buf[5] &lt;&lt; 8) |
48+
((uint32_t)buf[6] &lt;&lt; 16) |
49+
((uint32_t)buf[7] &lt;&lt; 24);
50+
} else {
51+
uint8_t buf[64];
52+
uint32_t count = min(sizeof buf, bytesToSend);
53+
for (uint32_t i = 0; i &lt; count; ++i) {
54+
buf[i] = nextByte();
55+
}
56+
bytesToSend -= count;
57+
Serial.write((char*)buf, count);
58+
}
59+
}
60+
</pre>
61+
</p>
62+
<p>
63+
<progress id='progress'></progress>
64+
</p>
65+
<script>
66+
let seed = 10;
67+
const length = 1024 * 1024 * 10;
68+
69+
function next_byte() {
70+
seed = (Math.imul(1103515245, seed) + 12345) % (1 << 31);
71+
return (seed >> 16) & 0xFF;
72+
}
73+
74+
manual_serial_test(async (t, port) => {
75+
await port.open({baudRate: 115200, bufferSize: 1024});
76+
77+
const config = new DataView(new ArrayBuffer(8));
78+
config.setUint32(0, seed, /*littleEndian=*/true);
79+
config.setUint32(4, length, /*littleEndian=*/true);
80+
81+
const writer = port.writable.getWriter();
82+
writer.write(config);
83+
84+
const progress = document.getElementById('progress');
85+
progress.max = length;
86+
progress.value = 0;
87+
88+
const reader = port.readable.getReader();
89+
let bytesRead = 0;
90+
while (bytesRead < length) {
91+
const { value, done } = await reader.read();
92+
assert_false(done);
93+
for (let i = 0; i < value.byteLength; ++i) {
94+
assert_equals(value[i], next_byte(),
95+
`mismatch at byte ${bytesRead + i}`);
96+
}
97+
bytesRead += value.byteLength;
98+
progress.value = bytesRead;
99+
}
100+
101+
writer.releaseLock();
102+
reader.releaseLock();
103+
await port.close();
104+
}, `Reading ${length} bytes from the device succeeds.`);
105+
</script>
106+
</body>
107+
</html>

0 commit comments

Comments
 (0)