Browse Source

Added tests for DataChannel stream id assignment

Paul-Louis Ageneau 3 years ago
parent
commit
74151f04af
1 changed files with 27 additions and 17 deletions
  1. 27 17
      test/connectivity.cpp

+ 27 - 17
test/connectivity.cpp

@@ -102,10 +102,6 @@ void test_connectivity() {
 	shared_ptr<DataChannel> dc2;
 	shared_ptr<DataChannel> dc2;
 	pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
 	pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
 		cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
 		cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
-		if (dc->label() != "test") {
-			cerr << "Wrong DataChannel label" << endl;
-			return;
-		}
 
 
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 			if (auto dc = wdc.lock()) {
 			if (auto dc = wdc.lock()) {
@@ -114,9 +110,7 @@ void test_connectivity() {
 			}
 			}
 		});
 		});
 
 
-		dc->onClosed([]() {
-			cout << "DataChannel 2: Closed" << endl;
-		});
+		dc->onClosed([]() { cout << "DataChannel 2: Closed" << endl; });
 
 
 		dc->onMessage([](variant<binary, string> message) {
 		dc->onMessage([](variant<binary, string> message) {
 			if (holds_alternative<string>(message)) {
 			if (holds_alternative<string>(message)) {
@@ -129,6 +123,9 @@ void test_connectivity() {
 
 
 	auto dc1 = pc1.createDataChannel("test");
 	auto dc1 = pc1.createDataChannel("test");
 
 
+	if (dc1->id().has_value())
+		throw std::runtime_error("DataChannel stream id assigned before connection");
+
 	dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
 	dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
 		if (auto dc1 = wdc1.lock()) {
 		if (auto dc1 = wdc1.lock()) {
 			cout << "DataChannel 1: Open" << endl;
 			cout << "DataChannel 1: Open" << endl;
@@ -136,9 +133,7 @@ void test_connectivity() {
 		}
 		}
 	});
 	});
 
 
-	dc1->onClosed([]() {
-		cout << "DataChannel 1: Closed" << endl;
-	});
+	dc1->onClosed([]() { cout << "DataChannel 1: Closed" << endl; });
 
 
 	dc1->onMessage([](const variant<binary, string> &message) {
 	dc1->onMessage([](const variant<binary, string> &message) {
 		if (holds_alternative<string>(message)) {
 		if (holds_alternative<string>(message)) {
@@ -159,10 +154,19 @@ void test_connectivity() {
 	if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
 	if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
 		throw runtime_error("DataChannel is not open");
 		throw runtime_error("DataChannel is not open");
 
 
+	if (adc2->label() != "test")
+		throw runtime_error("Wrong DataChannel label");
+
 	if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
 	if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
 	    dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
 	    dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
 		throw runtime_error("DataChannel max message size is incorrect");
 		throw runtime_error("DataChannel max message size is incorrect");
 
 
+	if (!dc1->id().has_value())
+		throw runtime_error("DataChannel stream id is not assigned");
+
+	if (dc1->id().value() != adc2->id().value())
+		throw runtime_error("DataChannel stream ids do not match");
+
 	if (auto addr = pc1.localAddress())
 	if (auto addr = pc1.localAddress())
 		cout << "Local address 1:  " << *addr << endl;
 		cout << "Local address 1:  " << *addr << endl;
 	if (auto addr = pc1.remoteAddress())
 	if (auto addr = pc1.remoteAddress())
@@ -186,10 +190,6 @@ void test_connectivity() {
 	shared_ptr<DataChannel> second2;
 	shared_ptr<DataChannel> second2;
 	pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
 	pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
 		cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
 		cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
-		if (dc->label() != "second") {
-			cerr << "Wrong second DataChannel label" << endl;
-			return;
-		}
 
 
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 			if (auto dc = wdc.lock())
 			if (auto dc = wdc.lock())
@@ -207,6 +207,9 @@ void test_connectivity() {
 
 
 	auto second1 = pc1.createDataChannel("second");
 	auto second1 = pc1.createDataChannel("second");
 
 
+	if (!second1->id().has_value())
+		throw std::runtime_error("Second DataChannel stream id is not assigned");
+
 	second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
 	second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
 		if (auto second1 = wsecond1.lock()) {
 		if (auto second1 = wsecond1.lock()) {
 			cout << "Second DataChannel 1: Open" << endl;
 			cout << "Second DataChannel 1: Open" << endl;
@@ -214,9 +217,7 @@ void test_connectivity() {
 		}
 		}
 	});
 	});
 
 
-	second1->onClosed([]() {
-		cout << "Second DataChannel 1: Closed" << endl;
-	});
+	second1->onClosed([]() { cout << "Second DataChannel 1: Closed" << endl; });
 
 
 	second1->onMessage([](const variant<binary, string> &message) {
 	second1->onMessage([](const variant<binary, string> &message) {
 		if (holds_alternative<string>(message)) {
 		if (holds_alternative<string>(message)) {
@@ -235,6 +236,15 @@ void test_connectivity() {
 	if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
 	if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
 		throw runtime_error("Second DataChannel is not open");
 		throw runtime_error("Second DataChannel is not open");
 
 
+	if (asecond2->label() != "second")
+		throw runtime_error("Wrong second DataChannel label");
+
+	if (!second2->id().has_value() || !asecond2->id().has_value())
+		throw runtime_error("Second DataChannel stream id is not assigned");
+
+	if (second2->id().value() != asecond2->id().value())
+		throw runtime_error("Second DataChannel stream ids do not match");
+
 	// Delay close of peer 2 to check closing works properly
 	// Delay close of peer 2 to check closing works properly
 	pc1.close();
 	pc1.close();
 	this_thread::sleep_for(1s);
 	this_thread::sleep_for(1s);