File: RTCDataChannel-stats.html

package info (click to toggle)
firefox 145.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,344 kB
  • sloc: cpp: 7,594,932; javascript: 6,459,612; ansic: 3,752,905; python: 1,403,433; xml: 629,811; asm: 438,677; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (192 lines) | stat: -rw-r--r-- 7,992 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!doctype html>
<html>
  <head>
    <meta charset="utf-8"/>
    <meta name="timeout" content="long"/>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="../webrtc/RTCPeerConnection-helper.js"></script>
    <script src="../webrtc/RTCDataChannel-helper.js"></script>
    <script src="../webrtc/RTCDataChannel-worker-shim.js"></script>
  </head>
  <body>
    <script>

async function getDcStats(pc) {
  const stats = await pc.getStats();
  return [...stats.values()].filter(({type}) => type == "data-channel");
}

// Oh boy here we go!
for (const which of ['neither', 'offerer', 'answerer']) {

  promise_test(async test => {
    const {offerer, answerer, offererChannel, answererChannel} =
      await makeDataChannelTestFixture(which, {},
        which == 'offerer' ? new WorkerBackedDataChannel() : null,
        which == 'answerer' ? new WorkerBackedDataChannel() : null);

    const dcStats1 = await getDcStats(offerer);
    assert_equals(dcStats1.length, 1,
      "One RTCDataChannel results in one data-channel stats object (offerer)");

    const dcStats2 = await getDcStats(answerer);
    assert_equals(dcStats2.length, 1,
      "One RTCDataChannel results in one data-channel stats object (answerer)");
  }, `${which} on worker: Check that RTCDataChannelStats are present`);

  promise_test(async test => {
    const {offerer, answerer, offererChannel, answererChannel} =
      await makeDataChannelTestFixture(which, {},
        which == 'offerer' ? new WorkerBackedDataChannel() : null,
        which == 'answerer' ? new WorkerBackedDataChannel() : null);

    test.add_cleanup(() => offerer.close());
    test.add_cleanup(() => answerer.close());

    for (const [pc, channel, type] of [
      [offerer, offererChannel, "offerer"],
      [answerer, answererChannel, "answerer"],
    ]) {
      const dcStats = await getDcStats(pc);
      assert_equals(dcStats.length, 1,
      `One RTCDataChannel results in one data-channel stats object (${type})`);

      assert_equals(dcStats[0].label, channel.label,
        `Stats label should match (${type})`);
      assert_equals(dcStats[0].protocol, channel.protocol,
        `Stats protocol should match (${type})`);
      assert_equals(dcStats[0].dataChannelIdentifier, channel.id,
        `Stats dataChannelIdentifier/id should match (${type})`);
      assert_equals(dcStats[0].state, channel.readyState,
        `Stats state/readyState should match (${type})`);
      assert_equals(dcStats[0].messagesSent, 0,
        `Initial messagesSent should be 0 (${type})`);
      assert_equals(dcStats[0].bytesSent, 0,
        `Initial bytesSent should be 0 (${type})`);
      assert_equals(dcStats[0].messagesReceived, 0,
        `Initial messagesReceived should be 0 (${type})`);
      assert_equals(dcStats[0].bytesReceived, 0,
        `Initial bytesReceived should be 0 (${type})`);
    };
  }, `${which} on worker: Check that RTCDataChannelStats have a valid initial state`);

  async function getDcCounterStats(pc) {
    const dcStats = await getDcStats(pc);
    return {
      bytesSent: dcStats[0].bytesSent,
      bytesReceived: dcStats[0].bytesReceived,
      messagesSent: dcStats[0].messagesSent,
      messagesReceived: dcStats[0].messagesReceived,
    };
  }

  promise_test(async test => {
    const {offerer, answerer, offererChannel, answererChannel} =
      await makeDataChannelTestFixture(which, {},
        which == 'offerer' ? new WorkerBackedDataChannel() : null,
        which == 'answerer' ? new WorkerBackedDataChannel() : null);

    test.add_cleanup(() => offerer.close());
    test.add_cleanup(() => answerer.close());

    for (const message of ['hello', '', '世界你好']) {
      for (const [sender, senderDc, receiver, receiverDc] of [
        [offerer, offererChannel, answerer, answererChannel],
        [answerer, answererChannel, offerer, offererChannel],
      ]) {
        const senderCountersBefore = await getDcCounterStats(sender);
        const receiverCountersBefore = await getDcCounterStats(receiver);

        senderDc.send(message);
        const recvEvent = await new Promise(r => receiverDc.onmessage = r);
        assert_equals(recvEvent.data, message);
        const encoder = new TextEncoder();
        const byteLength = encoder.encode(message).length;

        const senderCountersAfter = await getDcCounterStats(sender);
        const receiverCountersAfter = await getDcCounterStats(receiver);

        // Don't bother checking the other way around for zeroes
        assert_equals(senderCountersAfter.bytesSent,
          senderCountersBefore.bytesSent + byteLength,
          "Got expected sender bytesSent");
        assert_equals(senderCountersAfter.messagesSent,
          senderCountersBefore.messagesSent + 1,
          "Got expected sender messagesSent");

        assert_equals(receiverCountersAfter.bytesReceived,
          receiverCountersBefore.bytesReceived + byteLength,
          "Got expected receiver bytesReceived");
        assert_equals(receiverCountersAfter.messagesReceived,
          receiverCountersBefore.messagesReceived + 1,
          "Got expected receiver messagesReceived");
      }
    }
  }, `${which} on worker: Check that RTCDataChannelStats messages/bytes Sent/Received are correct for various strings`);

  // ASCII encoded buffer representation of the string
  const helloBuffer = Uint8Array.of(0x68, 0x65, 0x6c, 0x6c, 0x6f);
  const emptyBuffer = new Uint8Array();
  const helloBlob = new Blob([helloBuffer]);

  // UTF-8 encoded buffer representation of the string
  const unicodeBuffer = Uint8Array.of(
    0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c,
    0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd);

  // TODO: How finely-grained should we make these? Each one requires ICE, so we
  // can't do each case separately. What should the top-level loop be?
  for (const binaryType of ["arraybuffer", "blob"]) {
    promise_test(async test => {
    const {offerer, answerer, offererChannel, answererChannel} =
      await makeDataChannelTestFixture(which, {},
        which == 'offerer' ? new WorkerBackedDataChannel() : null,
        which == 'answerer' ? new WorkerBackedDataChannel() : null);

      test.add_cleanup(() => offerer.close());
      test.add_cleanup(() => answerer.close());

      for (const [message, bytes] of [
        [helloBlob, 5],
        [helloBuffer.buffer, 5],
        [helloBuffer, 5],
        [unicodeBuffer, 12]
      ]) {
        for (const [sender, senderDc, receiver, receiverDc] of [
          [offerer, offererChannel, answerer, answererChannel],
          [answerer, answererChannel, offerer, offererChannel],
        ]) {

          receiver.binaryType = binaryType;

          const senderCountersBefore = await getDcCounterStats(sender);
          const receiverCountersBefore = await getDcCounterStats(receiver);

          senderDc.send(message);
          const recvEvent = await new Promise(r => receiverDc.onmessage = r);

          const senderCountersAfter = await getDcCounterStats(sender);
          const receiverCountersAfter = await getDcCounterStats(receiver);

          assert_equals(senderCountersAfter.bytesSent,
            senderCountersBefore.bytesSent + bytes,
            "Got expected sender bytesSent");
          assert_equals(senderCountersAfter.messagesSent,
            senderCountersBefore.messagesSent + 1,
            "Got expected sender messagesSent");

          assert_equals(receiverCountersAfter.bytesReceived,
            receiverCountersBefore.bytesReceived + bytes,
            "Got expected receiver bytesReceived");
          assert_equals(receiverCountersAfter.messagesReceived,
            receiverCountersBefore.messagesReceived + 1,
            "Got expected receiver messagesReceived");
        }
      }
    }, `${which} on worker: Check that RTCDataChannelStats messages/bytes Sent/Received are correct for various binary messages received as ${binaryType}`);
  }
}
    </script>
  </body>
</html>