From 97f1c25ff88dd0dfd787736f5630a049ea709c64 Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 17 Sep 2024 11:15:50 +0300 Subject: [PATCH] close --- tests/client_server/client_server_test.cpp | 226 +++++++++++---------- 1 file changed, 116 insertions(+), 110 deletions(-) diff --git a/tests/client_server/client_server_test.cpp b/tests/client_server/client_server_test.cpp index 59839512..b701900e 100644 --- a/tests/client_server/client_server_test.cpp +++ b/tests/client_server/client_server_test.cpp @@ -52,10 +52,13 @@ TEST(ClientServer, OneClient) { class ClientSendThread { - using ClientType = TestClient; + using ClientType = TestClient; public: - ClientSendThread() { client = createAndConnectClient(); } + ClientSendThread() { + client = createAndConnectClient(); + sendThread.setName("clSend"); + } ~ClientSendThread() { sendThread.stopAndWait(); @@ -63,11 +66,10 @@ public: } void startSend() { - auto c = client; - sendThread.start([c] { c->ping(); }, 100._Hz); + sendThread.start([this] { client->ping(); }, 100._Hz); } - - void sendOnce() {client->ping();} + + void sendOnce() { client->ping(); } ClientType * client = nullptr; PIThread sendThread; @@ -75,28 +77,24 @@ public: int getServerPongs(PIClientServer::Server * s) { - int pongs = 0; - s->forEachClient([&pongs](PIClientServer::ServerClient * sc){ - const auto c = static_cast *>(sc); - pongs += c->pongCnt(); - }); - return pongs; + int pongs = 0; + s->forEachClient([&pongs](PIClientServer::ServerClient * sc) { + const auto c = static_cast *>(sc); + pongs += c->pongCnt(); + }); + return pongs; } int getClientsPongs(const PIVector & clients) { - int pongs = 0; - clients.forEach([&pongs](ClientSendThread * c){ - pongs += c->client->pongCnt(); - }); - return pongs; + int pongs = 0; + clients.forEach([&pongs](ClientSendThread * c) { pongs += c->client->pongCnt(); }); + return pongs; } int getClientsPings(const PIVector & clients) { - int pings = 0; - clients.forEach([&pings](ClientSendThread * c){ - pings += c->client->pingCnt(); - }); - return pings; + int pings = 0; + clients.forEach([&pings](ClientSendThread * c) { pings += c->client->pingCnt(); }); + return pings; } @@ -113,98 +111,106 @@ TEST(ClientServer, ManyClients) { waitLoop([s]() { return s->clientsCount() == clients_count; }, loop_timeout); EXPECT_EQ(clients_count, s->clientsCount()); - - EXPECT_EQ(0, getServerPongs(s)); - - EXPECT_EQ(getClientsPings(clients), 0); - - for (const auto c : clients) { - c->sendOnce(); - } - - EXPECT_EQ(getClientsPings(clients), clients_count); - EXPECT_TRUE(clients.every([](ClientSendThread * c){return c->client->pingCnt() == 1;})); - EXPECT_TRUE(clients.every([](ClientSendThread * c){return c->client->pongCnt() == 0;})); - waitLoop([s]() { return getServerPongs(s) >= clients_count; }, loop_timeout); - EXPECT_EQ(clients_count, getServerPongs(s)); - - s->forEachClient([](PIClientServer::ServerClient * sc) { static_cast *>(sc)->ping(); }); - const auto clientCheckPong = [&clients](){return clients.every([](ClientSendThread * c){return c->client->pongCnt() == 1;});}; - waitLoop([&clientCheckPong]() { return clientCheckPong(); }, loop_timeout); - EXPECT_TRUE(clientCheckPong()); - - for (const auto c : clients) { - c->startSend(); - } - (100_ms).sleep(); - EXPECT_TRUE(getClientsPings(clients) > clients_count*2); - EXPECT_TRUE(getServerPongs(s) > clients_count*2); - piDeleteAllAndClear(clients); - waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout); - EXPECT_EQ(0, s->clientsCount()); - delete s; + + EXPECT_EQ(0, getServerPongs(s)); + + EXPECT_EQ(getClientsPings(clients), 0); + + for (const auto c: clients) { + c->sendOnce(); + } + + EXPECT_EQ(getClientsPings(clients), clients_count); + EXPECT_TRUE(clients.every([](ClientSendThread * c) { return c->client->pingCnt() == 1; })); + EXPECT_TRUE(clients.every([](ClientSendThread * c) { return c->client->pongCnt() == 0; })); + waitLoop([s]() { return getServerPongs(s) >= clients_count; }, loop_timeout); + EXPECT_EQ(clients_count, getServerPongs(s)); + + s->forEachClient([](PIClientServer::ServerClient * sc) { static_cast *>(sc)->ping(); }); + const auto clientCheckPong = [&clients]() { return clients.every([](ClientSendThread * c) { return c->client->pongCnt() == 1; }); }; + waitLoop([&clientCheckPong]() { return clientCheckPong(); }, loop_timeout); + EXPECT_TRUE(clientCheckPong()); + + for (const auto c: clients) { + c->startSend(); + } + (100_ms).sleep(); + EXPECT_TRUE(getClientsPings(clients) > clients_count * 2); + EXPECT_TRUE(getServerPongs(s) > clients_count * 2); + piDeleteAllAndClear(clients); + waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout); + EXPECT_EQ(0, s->clientsCount()); + delete s; } TEST(ClientServer, DynamicClients) { - auto const loop_timeout = 1000_ms; + auto const loop_timeout = 1000_ms; constexpr int clients_count = 20; - PIVector clients; - PIMutex clients_mutex; - auto s = createServer(); - - const auto spawnClient = [&clients, &clients_mutex]() { - auto c = new ClientSendThread(); - c->startSend(); - clients_mutex.lock(); - clients << c; - clients_mutex.unlock(); - piCout << "new client" << clients.size(); - }; - - piForTimes(clients_count) { + PIVector clients; + PIMutex clients_mutex; + auto s = createServer(); + + const auto spawnClient = [&clients, &clients_mutex]() { + auto c = new ClientSendThread(); + c->startSend(); + clients_mutex.lock(); + clients << c; + clients_mutex.unlock(); + piCout << "new client" << clients.size(); + }; + + piForTimes(clients_count) { spawnClient(); } - - PIThread spawnThread; - PIThread deleteThread; - - spawnThread.start([&spawnClient](){ - const int new_cnt = randomi() % 10; - piForTimes(new_cnt) { - spawnClient(); - } - }, 12_Hz); - - deleteThread.start([&clients, &clients_mutex](){ - const int rm_cnt = randomi() % 10; - piForTimes(rm_cnt) { - ClientSendThread * c = nullptr; - clients_mutex.lock(); - if (clients.size() > 10) { - c = clients.take_back(); - } - clients_mutex.unlock(); - if (c) { - delete c; - piCout << "remove client" << clients.size(); - } - } - }, 13_Hz); - - (1_s).sleep(); - - EXPECT_GE(s->clientsCount(), 10); - - piCout << "now clients" << clients.size(); - - //delete s; - - deleteThread.stopAndWait(); - spawnThread.stopAndWait(); - - piCout << "total clients" << clients.size(); - - piDeleteAllAndClear(clients); - waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout); - EXPECT_EQ(0, s->clientsCount()); + + PIThread spawnThread; + PIThread deleteThread; + spawnThread.setName("spawn"); + deleteThread.setName("delete"); + + spawnThread.start( + [&spawnClient]() { + const int new_cnt = randomi() % 10; + piForTimes(new_cnt) { + spawnClient(); + } + }, + 12_Hz); + + deleteThread.start( + [&clients, &clients_mutex]() { + const int rm_cnt = randomi() % 10; + piForTimes(rm_cnt) { + ClientSendThread * c = nullptr; + clients_mutex.lock(); + if (clients.size() > 10) { + c = clients.take_front(); + } + clients_mutex.unlock(); + if (c) { + delete c; + piCout << "remove client" << clients.size(); + } + } + }, + 13_Hz); + + (10_s).sleep(); + + EXPECT_GE(s->clientsCount(), 10); + + piCout << "now clients" << clients.size(); + + + deleteThread.stopAndWait(); + spawnThread.stopAndWait(); + + + piCout << "total clients" << clients.size(); + + piDeleteAllAndClear(clients); + waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout); + EXPECT_EQ(0, s->clientsCount()); + + delete s; }