Added WebSocket test

This commit is contained in:
Paul-Louis Ageneau
2020-06-01 00:44:12 +02:00
parent 755b3e9dac
commit 9bb9725f4d
3 changed files with 101 additions and 6 deletions

View File

@ -71,6 +71,7 @@ set(TESTS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/connectivity.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/capi.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/websocket.cpp
)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)

View File

@ -22,23 +22,34 @@ using namespace std;
void test_connectivity();
void test_capi();
void test_websocket();
int main(int argc, char **argv) {
try {
cout << endl << "*** Running connectivity test..." << endl;
cout << endl << "*** Running WebRTC connectivity test..." << endl;
test_connectivity();
cout << "*** Finished connectivity test" << endl;
cout << "*** Finished WebRTC connectivity test" << endl;
} catch (const exception &e) {
cerr << "Connectivity test failed: " << e.what() << endl;
cerr << "WebRTC connectivity test failed: " << e.what() << endl;
return -1;
}
try {
cout << endl << "*** Running C API test..." << endl;
cout << endl << "*** Running WebRTC C API test..." << endl;
test_capi();
cout << "*** Finished C API test" << endl;
cout << "*** Finished WebRTC C API test" << endl;
} catch (const exception &e) {
cerr << "C API test failed: " << e.what() << endl;
cerr << "WebRTC C API test failed: " << e.what() << endl;
return -1;
}
#if RTC_ENABLE_WEBSOCKET
try {
cout << endl << "*** Running WebSocket test..." << endl;
test_websocket();
cout << "*** Finished WebSocket test" << endl;
} catch (const exception &e) {
cerr << "WebSocket test failed: " << e.what() << endl;
return -1;
}
#endif
return 0;
}

83
test/websocket.cpp Normal file
View File

@ -0,0 +1,83 @@
/**
* Copyright (c) 2019 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 <chrono>
#include <iostream>
#include <memory>
#include <thread>
#include <atomic>
using namespace rtc;
using namespace std;
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
void test_websocket() {
InitLogger(LogLevel::Verbose);
const string myMessage = "Hello world";
auto ws = std::make_shared<WebSocket>();
ws->onOpen([wws = make_weak_ptr(ws), &myMessage]() {
auto ws = wws.lock();
if (!ws)
return;
cout << "WebSocket: Open" << endl;
ws->send(myMessage);
});
std::atomic<bool> received = false;
ws->onMessage([&received, &myMessage](const variant<binary, string> &message) {
if (holds_alternative<string>(message)) {
string str = get<string>(message);
if((received = (str == myMessage)))
cout << "WebSocket: Received expected message" << endl;
else
cout << "WebSocket: Received UNEXPECTED message" << endl;
}
});
ws->open("wss://echo.websocket.org/");
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);
// You may call rtc::Cleanup() when finished to free static resources
rtc::Cleanup();
this_thread::sleep_for(1s);
cout << "Success" << endl;
}
#endif