From 4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36 Mon Sep 17 00:00:00 2001 From: Per Larsson Date: Fri, 18 Feb 2022 06:56:20 +0100 Subject: Simple websocket client/server test. --- zenserver-test/zenserver-test.cpp | 100 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 4 deletions(-) (limited to 'zenserver-test') diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index b46106287..b7d157d5a 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -324,7 +325,7 @@ main(int argc, char** argv) zen::logging::InitializeLogging(); spdlog::set_level(spdlog::level::debug); - spdlog::set_formatter(std::make_unique<::logging::full_test_formatter>("test", std::chrono::system_clock::now())); + spdlog::set_formatter(std::make_unique< ::logging::full_test_formatter>("test", std::chrono::system_clock::now())); std::filesystem::path ProgramBaseDir = std::filesystem::path(argv[0]).parent_path(); std::filesystem::path TestBaseDir = ProgramBaseDir.parent_path().parent_path() / ".test"; @@ -1958,9 +1959,59 @@ public: ZenServerInstance& GetInstance(int Index) { return *m_Instances[Index]; } private: - std::string m_HelperId; - int m_ServerCount = 0; - std::vector> m_Instances; + std::string m_HelperId; + int m_ServerCount = 0; + std::vector > m_Instances; +}; + +class IoDispatcher +{ +public: + IoDispatcher(asio::io_context& IoCtx) : m_IoCtx(IoCtx) {} + ~IoDispatcher() { Stop(); } + + void Run() + { + Stop(); + + m_Running = true; + + m_IoThread = std::thread([this]() { + try + { + m_IoCtx.run(); + } + catch (std::exception& Error) + { + m_Error = Error; + } + + m_Running = false; + }); + } + + void Stop() + { + if (m_Running) + { + m_Running = false; + + if (m_IoThread.joinable()) + { + m_IoThread.join(); + } + } + } + + bool IsRunning() const { return m_Running; } + + const std::exception& Error() { return m_Error; } + +private: + asio::io_context& m_IoCtx; + std::thread m_IoThread; + std::exception m_Error; + std::atomic_bool m_Running{false}; }; TEST_CASE("http.basics") @@ -2030,6 +2081,47 @@ TEST_CASE("http.package") CHECK_EQ(ResponsePackage, TestPackage); } +TEST_CASE("websocket.basic") +{ + std::filesystem::path TestDir = TestEnv.CreateNewTestDir(); + const uint16_t PortNumber = 13337; + + ZenServerInstance Inst(TestEnv); + Inst.SetTestDir(TestDir); + Inst.SpawnServer(PortNumber); + Inst.WaitUntilReady(); + + asio::io_context IoCtx; + IoDispatcher IoDispatcher(IoCtx); + auto WebSocket = WebSocketClient::Create(IoCtx); + + std::atomic_bool Signaled{false}; + WebSocketEvent Event; + + WebSocket->On(WebSocketEvent::kConnected, [&]() { + Event = WebSocketEvent::kConnected; + Signaled = true; + }); + + WebSocket->On(WebSocketEvent::kDisconnected, [&]() { + Event = WebSocketEvent::kDisconnected; + Signaled = true; + }); + + WebSocket->Connect({.Host = "127.0.0.1", .Port = 8848, .Endpoint = "/zen"}); + + IoDispatcher.Run(); + + while (Signaled == false && IoDispatcher.IsRunning()) + { + std::this_thread::sleep_for(std::chrono::seconds(5)); + }; + + CHECK(Event == WebSocketEvent::kConnected); + + WebSocket->Disconnect(); +} + # if 0 TEST_CASE("lifetime.owner") { -- cgit v1.2.3