aboutsummaryrefslogtreecommitdiff
path: root/zenserver-test/zenserver-test.cpp
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2022-02-18 06:56:20 +0100
committerPer Larsson <[email protected]>2022-02-18 06:56:20 +0100
commit4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36 (patch)
tree9d2f5e83679c0eea5de63b129eb1a2779501b28b /zenserver-test/zenserver-test.cpp
parentRenamed file. (diff)
downloadzen-4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36.tar.xz
zen-4b9bac3c5baf7633cd51cffcf8e63cb5527ddb36.zip
Simple websocket client/server test.
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
-rw-r--r--zenserver-test/zenserver-test.cpp100
1 files changed, 96 insertions, 4 deletions
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 <zencore/timer.h>
#include <zenhttp/httpclient.h>
#include <zenhttp/httpshared.h>
+#include <zenhttp/websocket.h>
#include <zenhttp/zenhttp.h>
#include <zenutil/cache/cache.h>
#include <zenutil/zenserverprocess.h>
@@ -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<std::unique_ptr<ZenServerInstance>> m_Instances;
+ std::string m_HelperId;
+ int m_ServerCount = 0;
+ std::vector<std::unique_ptr<ZenServerInstance> > 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")
{