Skip to content

Commit 0b52ef0

Browse files
Added tests for reliability modes
1 parent e736c60 commit 0b52ef0

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ set(TESTS_SOURCES
191191
${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp
192192
${CMAKE_CURRENT_SOURCE_DIR}/test/connectivity.cpp
193193
${CMAKE_CURRENT_SOURCE_DIR}/test/negotiated.cpp
194+
${CMAKE_CURRENT_SOURCE_DIR}/test/reliability.cpp
194195
${CMAKE_CURRENT_SOURCE_DIR}/test/turn_connectivity.cpp
195196
${CMAKE_CURRENT_SOURCE_DIR}/test/track.cpp
196197
${CMAKE_CURRENT_SOURCE_DIR}/test/capi_connectivity.cpp

test/main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
using namespace std;
1616
using namespace chrono_literals;
1717

18-
void test_negotiated();
1918
void test_connectivity(bool signal_wrong_fingerprint);
19+
void test_negotiated();
20+
void test_reliability();
2021
void test_turn_connectivity();
2122
void test_track();
2223
void test_capi_connectivity();
@@ -74,6 +75,14 @@ int main(int argc, char **argv) {
7475
cerr << "WebRTC negotiated DataChannel test failed: " << e.what() << endl;
7576
return -1;
7677
}
78+
try {
79+
cout << endl << "*** Running WebRTC reliability mode test..." << endl;
80+
test_reliability();
81+
cout << "*** Finished WebRTC reliaility mode test" << endl;
82+
} catch (const exception &e) {
83+
cerr << "WebRTC reliability test failed: " << e.what() << endl;
84+
return -1;
85+
}
7786
#if RTC_ENABLE_MEDIA
7887
try {
7988
cout << endl << "*** Running WebRTC Track test..." << endl;

test/reliability.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Copyright (c) 2019 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#include "rtc/rtc.hpp"
10+
11+
#include <atomic>
12+
#include <chrono>
13+
#include <iostream>
14+
#include <memory>
15+
#include <thread>
16+
17+
using namespace rtc;
18+
using namespace std;
19+
20+
void test_reliability() {
21+
InitLogger(LogLevel::Debug);
22+
23+
Configuration config1;
24+
// STUN server example (not necessary to connect locally)
25+
config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
26+
27+
PeerConnection pc1(config1);
28+
29+
Configuration config2;
30+
// STUN server example (not necessary to connect locally)
31+
config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
32+
33+
PeerConnection pc2(config2);
34+
35+
pc1.onLocalDescription([&pc2](Description sdp) {
36+
cout << "Description 1: " << sdp << endl;
37+
pc2.setRemoteDescription(string(sdp));
38+
});
39+
40+
pc1.onLocalCandidate([&pc2](Candidate candidate) {
41+
cout << "Candidate 1: " << candidate << endl;
42+
pc2.addRemoteCandidate(string(candidate));
43+
});
44+
45+
pc2.onLocalDescription([&pc1](Description sdp) {
46+
cout << "Description 2: " << sdp << endl;
47+
pc1.setRemoteDescription(string(sdp));
48+
});
49+
50+
pc2.onLocalCandidate([&pc1](Candidate candidate) {
51+
cout << "Candidate 2: " << candidate << endl;
52+
pc1.addRemoteCandidate(string(candidate));
53+
});
54+
55+
Reliability reliableOrdered;
56+
auto dcReliableOrdered = pc1.createDataChannel("reliable_ordered", {reliableOrdered});
57+
58+
Reliability reliableUnordered;
59+
reliableUnordered.unordered = true;
60+
auto dcReliableUnordered = pc1.createDataChannel("reliable_unordered", {reliableUnordered});
61+
62+
Reliability unreliableMaxPacketLifeTime;
63+
unreliableMaxPacketLifeTime.unordered = true;
64+
unreliableMaxPacketLifeTime.maxPacketLifeTime = 222ms;
65+
auto dcUnreliableMaxPacketLifeTime =
66+
pc1.createDataChannel("unreliable_maxpacketlifetime", {unreliableMaxPacketLifeTime});
67+
68+
Reliability unreliableMaxRetransmits;
69+
unreliableMaxRetransmits.unordered = true;
70+
unreliableMaxRetransmits.maxRetransmits = 2;
71+
auto dcUnreliableMaxRetransmits =
72+
pc1.createDataChannel("unreliable_maxretransmits", {unreliableMaxRetransmits});
73+
74+
std::atomic<int> count = 0;
75+
std::atomic<bool> failed = false;
76+
pc2.onDataChannel([&count, &failed](shared_ptr<DataChannel> dc) {
77+
cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
78+
79+
auto label = dc->label();
80+
auto reliability = dc->reliability();
81+
82+
try {
83+
if (label == "reliable_ordered") {
84+
if (reliability.unordered != false || reliability.maxPacketLifeTime ||
85+
reliability.maxRetransmits)
86+
throw std::runtime_error("Expected reliable ordered");
87+
} else if (label == "reliable_unordered") {
88+
if (reliability.unordered != true || reliability.maxPacketLifeTime ||
89+
reliability.maxRetransmits)
90+
throw std::runtime_error("Expected reliable unordered");
91+
} else if (label == "unreliable_maxpacketlifetime") {
92+
if (!reliability.maxPacketLifeTime || *reliability.maxPacketLifeTime != 222ms ||
93+
reliability.maxRetransmits)
94+
throw std::runtime_error("Expected maxPacketLifeTime to be set");
95+
} else if (label == "unreliable_maxretransmits") {
96+
if (reliability.maxPacketLifeTime || !reliability.maxRetransmits ||
97+
*reliability.maxRetransmits != 2)
98+
throw std::runtime_error("Expected maxRetransmits to be set");
99+
} else
100+
throw std::runtime_error("Unexpected label: " + label);
101+
} catch (const std::exception &e) {
102+
cerr << "Error: " << e.what();
103+
failed = true;
104+
return;
105+
}
106+
++count;
107+
});
108+
109+
// Wait a bit
110+
int attempts = 10;
111+
shared_ptr<DataChannel> adc2;
112+
while (count != 4 && !failed && attempts--)
113+
this_thread::sleep_for(1s);
114+
115+
if (pc1.state() != PeerConnection::State::Connected ||
116+
pc2.state() != PeerConnection::State::Connected)
117+
throw runtime_error("PeerConnection is not connected");
118+
119+
if (failed)
120+
throw runtime_error("Incorrect reliability settings");
121+
122+
if (count != 4)
123+
throw runtime_error("Some DataChannels are not open");
124+
125+
pc1.close();
126+
127+
cout << "Success" << endl;
128+
}

0 commit comments

Comments
 (0)