|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.onvoid.webrtc.client; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import dev.onvoid.webrtc.RTCStats; |
| 26 | +import dev.onvoid.webrtc.RTCStatsReport; |
| 27 | +import dev.onvoid.webrtc.RTCStatsType; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +class StatsReaderTests { |
| 32 | + |
| 33 | + private static final long START = 1_700_000_000_000_000L; |
| 34 | + |
| 35 | + private static final long SECOND = 1_000_000; |
| 36 | + |
| 37 | + private final StatsReader reader = new StatsReader(); |
| 38 | + |
| 39 | + |
| 40 | + @Test |
| 41 | + void bitrateFromReportTimes() { |
| 42 | + // Read one right after the other, as a busy control executor hands |
| 43 | + // them on: the reports were measured a second apart. |
| 44 | + PeerStats first = reader.read(report(START, 5_000, 2_000)); |
| 45 | + PeerStats second = reader.read(report(START + SECOND, 6_000, 2_500)); |
| 46 | + |
| 47 | + assertEquals(-1, first.outgoingBitrate()); |
| 48 | + assertEquals(-1, first.incomingBitrate()); |
| 49 | + assertEquals(8_000, second.outgoingBitrate()); |
| 50 | + assertEquals(4_000, second.incomingBitrate()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void timestampOfReport() { |
| 55 | + PeerStats stats = reader.read(report(START + 250, 0, 0)); |
| 56 | + |
| 57 | + assertEquals(Instant.ofEpochSecond(1_700_000_000, 250_000), stats.timestamp()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void repeatedReportKeepsBitrates() { |
| 62 | + // WebRTC hands out the report it cached again. The one after it is |
| 63 | + // measured from the sample before. |
| 64 | + reader.read(report(START, 0, 0)); |
| 65 | + reader.read(report(START + SECOND, 1_000, 1_000)); |
| 66 | + PeerStats repeated = reader.read(report(START + SECOND, 1_000, 1_000)); |
| 67 | + PeerStats next = reader.read(report(START + 2 * SECOND, 3_000, 1_000)); |
| 68 | + |
| 69 | + assertEquals(8_000, repeated.outgoingBitrate()); |
| 70 | + assertEquals(8_000, repeated.incomingBitrate()); |
| 71 | + assertEquals(16_000, next.outgoingBitrate()); |
| 72 | + assertEquals(0, next.incomingBitrate()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void repeatedFirstReport() { |
| 77 | + reader.read(report(START, 1_000, 1_000)); |
| 78 | + PeerStats repeated = reader.read(report(START, 1_000, 1_000)); |
| 79 | + |
| 80 | + assertEquals(-1, repeated.outgoingBitrate()); |
| 81 | + assertEquals(-1, repeated.incomingBitrate()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void clockWentBack() { |
| 86 | + reader.read(report(START, 0, 0)); |
| 87 | + PeerStats back = reader.read(report(START - SECOND, 1_000, 1_000)); |
| 88 | + PeerStats next = reader.read(report(START, 2_000, 3_000)); |
| 89 | + |
| 90 | + assertEquals(-1, back.outgoingBitrate()); |
| 91 | + assertEquals(-1, back.incomingBitrate()); |
| 92 | + assertEquals(8_000, next.outgoingBitrate()); |
| 93 | + assertEquals(16_000, next.incomingBitrate()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void counterStartedAgain() { |
| 98 | + // Fewer bytes sent than before. Received counts on. |
| 99 | + reader.read(report(START, 10_000, 1_000)); |
| 100 | + PeerStats reset = reader.read(report(START + SECOND, 500, 2_000)); |
| 101 | + PeerStats next = reader.read(report(START + 2 * SECOND, 1_500, 2_000)); |
| 102 | + |
| 103 | + assertEquals(-1, reset.outgoingBitrate()); |
| 104 | + assertEquals(8_000, reset.incomingBitrate()); |
| 105 | + assertEquals(8_000, next.outgoingBitrate()); |
| 106 | + assertEquals(0, next.incomingBitrate()); |
| 107 | + } |
| 108 | + |
| 109 | + private static RTCStatsReport report(long micros, long bytesSent, long bytesReceived) { |
| 110 | + Map<String, Object> values = new HashMap<>(); |
| 111 | + values.put("bytesSent", bytesSent); |
| 112 | + values.put("bytesReceived", bytesReceived); |
| 113 | + |
| 114 | + Map<String, RTCStats> stats = new HashMap<>(); |
| 115 | + stats.put("T01", new RTCStats(micros, RTCStatsType.TRANSPORT, "T01", values) { }); |
| 116 | + |
| 117 | + return new RTCStatsReport(stats, micros) { }; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments