serialPort_loopback_BufferOverrunError-manual.https.html (2024B)
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 connected serial device configured to act as a 14 "loopback" device, with the transmit and receive pins wired together. 15 </p> 16 <script> 17 manual_serial_test(async (t, port) => { 18 await port.open({baudRate: 115200, bufferSize: 1024}); 19 20 // Create something much larger than bufferSize above. 21 const data = new Uint8Array(16 * 1024); 22 for (let i = 0; i < data.byteLength; ++i) 23 data[i] = (i / 1024) & 0xff; 24 25 // Completely write |data| to the port without waiting for it to be 26 // received. 27 const writer = port.writable.getWriter(); 28 writer.write(data); 29 await writer.close(); 30 31 const reader = port.readable.getReader(); 32 const chunks = []; 33 let actualLength = 0; 34 while (true) { 35 try { 36 const {value, done} = await reader.read(); 37 if (value) { 38 actualLength += value.byteLength; 39 chunks.push(value); 40 } 41 if (done) { 42 assert_unreached("Unexpected end of stream."); 43 break; 44 } 45 } catch (e) { 46 assert_equals(e.name, 'BufferOverrunError'); 47 break; 48 } 49 } 50 reader.releaseLock(); 51 52 const buffer = new Uint8Array(actualLength); 53 chunks.reduce((offset, chunk) => { 54 buffer.set(chunk, offset); 55 return offset + chunk.byteLength; 56 }, 0); 57 58 assert_greater_than(actualLength, 0); 59 compareArrays(buffer, data.slice(0, actualLength)); 60 61 await port.close(); 62 }, 'Overflowing the receive buffer triggers an error.'); 63 </script> 64 </body> 65 </html>