RTCDataChannel-send-close-helper.js (2547B)
1 // META: script=RTCPeerConnection-helper.js 2 3 const largeSendDataLength = 64 * 1024; 4 5 function rtc_data_channel_send_close_test(sendData, dataChannelOptions) { 6 const mode = 7 `${dataChannelOptions.negotiated ? 'Negotiated d' : 'D'}atachannel`; 8 9 // Determine the type of the data being sent. 10 let sendDataType = typeof (sendData); 11 if (sendDataType === 'object') { 12 if (ArrayBuffer.isView(sendData)) { 13 sendDataType = 'arraybuffer'; 14 } else if (sendData instanceof Blob) { 15 sendDataType = 'blob'; 16 } 17 } 18 19 // Determine the length of the data being sent. 20 let sendDataLength = 0; 21 switch (sendDataType) { 22 case 'string': 23 sendDataLength = sendData.length; 24 break; 25 case 'arraybuffer': 26 sendDataLength = sendData.byteLength; 27 break; 28 case 'blob': 29 sendDataLength = sendData.size; 30 break; 31 } 32 33 promise_test( 34 async t => { 35 assert_greater_than( 36 sendDataLength, 0, 37 '`sendData` must be a string, Blob or ArrayBuffer view.'); 38 39 let [channel1, channel2] = 40 await createDataChannelPair(t, dataChannelOptions); 41 let receivedSize = 0, sentSize = 0; 42 43 channel2.binaryType = 'arraybuffer'; 44 channel2.onmessage = e => { 45 if (typeof e.data === 'string') 46 receivedSize += e.data.length; 47 else 48 receivedSize += e.data.byteLength; 49 }; 50 51 channel2.onerror = event => { 52 assert_unreached( 53 `channel2 must not dispatch error events: ${event.error}.`); 54 }; 55 56 let closePromiseResolve; 57 let closePromise = new Promise((resolve, reject) => { 58 closePromiseResolve = resolve; 59 }); 60 channel2.onclose = e => { 61 closePromiseResolve(); 62 }; 63 64 try { 65 while (sentSize < 20 * 1024 * 1024) { 66 channel1.send(sendData); 67 sentSize += sendDataLength; 68 } 69 } catch (error) { 70 assert_true(error instanceof DOMException); 71 assert_equals(error.name, 'OperationError'); 72 } 73 channel1.onerror = event => { 74 assert_unreached( 75 `channel1 must not dispatch error events: ${event.error}.`); 76 }; 77 channel1.close(); 78 79 await closePromise; 80 assert_equals( 81 receivedSize, sentSize, 82 'All the pending sent messages are received after calling close()'); 83 }, 84 `${mode} should be able to send and receive all ${ 85 sendDataType} messages on close`); 86 }