Переглянути джерело

Added test for WebSocket server

Paul-Louis Ageneau 4 роки тому
батько
коміт
abbff6eb8d
3 змінених файлів з 121 додано та 0 видалено
  1. 1 0
      CMakeLists.txt
  2. 9 0
      test/main.cpp
  3. 111 0
      test/websocketserver.cpp

+ 1 - 0
CMakeLists.txt

@@ -166,6 +166,7 @@ set(TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_connectivity.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_track.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/websocket.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/websocketserver.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark.cpp
 )
 

+ 9 - 0
test/main.cpp

@@ -29,6 +29,7 @@ void test_track();
 void test_capi_connectivity();
 void test_capi_track();
 void test_websocket();
+void test_websocketserver();
 size_t benchmark(chrono::milliseconds duration);
 
 void test_benchmark() {
@@ -101,6 +102,14 @@ int main(int argc, char **argv) {
 		return -1;
 	}
 */
+	try {
+		cout << endl << "*** Running WebSocketServer test..." << endl;
+		test_websocketserver();
+		cout << "*** Finished WebSocketServer test" << endl;
+	} catch (const exception &e) {
+		cerr << "WebSocketServer test failed: " << e.what() << endl;
+		return -1;
+	}
 #endif
 	this_thread::sleep_for(1s);
 	try {

+ 111 - 0
test/websocketserver.cpp

@@ -0,0 +1,111 @@
+/**
+ * Copyright (c) 2021 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "rtc/rtc.hpp"
+
+#if RTC_ENABLE_WEBSOCKET
+
+#include <atomic>
+#include <chrono>
+#include <iostream>
+#include <memory>
+#include <thread>
+
+using namespace rtc;
+using namespace std;
+
+template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
+
+void test_websocketserver() {
+	InitLogger(LogLevel::Debug);
+
+	const string myMessage = "Hello world from client";
+
+	WebSocketServer::Configuration serverConfig;
+	serverConfig.port = 48080;
+	WebSocketServer server(std::move(serverConfig));
+
+	shared_ptr<WebSocket> client;
+	server.onClient([&client](shared_ptr<WebSocket> incoming) {
+		cout << "WebSocketServer: Client connection received" << endl;
+		client = incoming;
+
+		if(auto addr = client->remoteAddress())
+			cout << "WebSocketServer: Client remote address is " << *addr << endl;
+
+		client->onOpen([wclient = make_weak_ptr(client)]() {
+			cout << "WebSocketServer: Client connection open" << endl;
+			if(auto client = wclient.lock())
+				if(auto path = client->path())
+					cout << "WebSocketServer: Requested path is " << *path << endl;
+		});
+
+		client->onClosed([]() {
+			cout << "WebSocketServer: Client connection closed" << endl;
+		});
+
+		client->onMessage([wclient = make_weak_ptr(client)](variant<binary, string> message) {
+			if(auto client = wclient.lock())
+				client->send(std::move(message));
+		});
+	});
+
+	WebSocket::Configuration config;
+	config.disableTlsVerification = true;
+	WebSocket ws(std::move(config));
+
+	ws.onOpen([&ws, &myMessage]() {
+		cout << "WebSocket: Open" << endl;
+		ws.send(myMessage);
+	});
+
+	ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
+
+	std::atomic<bool> received = false;
+	ws.onMessage([&received, &myMessage](variant<binary, string> message) {
+		if (holds_alternative<string>(message)) {
+			string str = std::move(get<string>(message));
+			if ((received = (str == myMessage)))
+				cout << "WebSocket: Received expected message" << endl;
+			else
+				cout << "WebSocket: Received UNEXPECTED message" << endl;
+		}
+	});
+
+	ws.open("ws://localhost:48080/");
+
+	int attempts = 10;
+	while ((!ws.isOpen() || !received) && attempts--)
+		this_thread::sleep_for(1s);
+
+	if (!ws.isOpen())
+		throw runtime_error("WebSocket is not open");
+
+	if (!received)
+		throw runtime_error("Expected message not received");
+
+	ws.close();
+	this_thread::sleep_for(1s);
+
+	server.stop();
+	this_thread::sleep_for(1s);
+
+	cout << "Success" << endl;
+}
+
+#endif