This commit is contained in:
2024-09-17 11:15:50 +03:00
parent 97aad47a21
commit 97f1c25ff8

View File

@@ -52,10 +52,13 @@ TEST(ClientServer, OneClient) {
class ClientSendThread { class ClientSendThread {
using ClientType = TestClient<false, false, 100_KiB>; using ClientType = TestClient<false, false, 10_KiB>;
public: public:
ClientSendThread() { client = createAndConnectClient<ClientType>(); } ClientSendThread() {
client = createAndConnectClient<ClientType>();
sendThread.setName("clSend");
}
~ClientSendThread() { ~ClientSendThread() {
sendThread.stopAndWait(); sendThread.stopAndWait();
@@ -63,8 +66,7 @@ public:
} }
void startSend() { void startSend() {
auto c = client; sendThread.start([this] { client->ping(); }, 100._Hz);
sendThread.start([c] { c->ping(); }, 100._Hz);
} }
void sendOnce() { client->ping(); } void sendOnce() { client->ping(); }
@@ -85,17 +87,13 @@ int getServerPongs(PIClientServer::Server * s) {
int getClientsPongs(const PIVector<ClientSendThread *> & clients) { int getClientsPongs(const PIVector<ClientSendThread *> & clients) {
int pongs = 0; int pongs = 0;
clients.forEach([&pongs](ClientSendThread * c){ clients.forEach([&pongs](ClientSendThread * c) { pongs += c->client->pongCnt(); });
pongs += c->client->pongCnt();
});
return pongs; return pongs;
} }
int getClientsPings(const PIVector<ClientSendThread *> & clients) { int getClientsPings(const PIVector<ClientSendThread *> & clients) {
int pings = 0; int pings = 0;
clients.forEach([&pings](ClientSendThread * c){ clients.forEach([&pings](ClientSendThread * c) { pings += c->client->pingCnt(); });
pings += c->client->pingCnt();
});
return pings; return pings;
} }
@@ -150,7 +148,7 @@ TEST(ClientServer, DynamicClients) {
constexpr int clients_count = 20; constexpr int clients_count = 20;
PIVector<ClientSendThread *> clients; PIVector<ClientSendThread *> clients;
PIMutex clients_mutex; PIMutex clients_mutex;
auto s = createServer<true, true, 100_KiB>(); auto s = createServer<true, true, 10_KiB>();
const auto spawnClient = [&clients, &clients_mutex]() { const auto spawnClient = [&clients, &clients_mutex]() {
auto c = new ClientSendThread(); auto c = new ClientSendThread();
@@ -167,21 +165,26 @@ TEST(ClientServer, DynamicClients) {
PIThread spawnThread; PIThread spawnThread;
PIThread deleteThread; PIThread deleteThread;
spawnThread.setName("spawn");
deleteThread.setName("delete");
spawnThread.start([&spawnClient](){ spawnThread.start(
[&spawnClient]() {
const int new_cnt = randomi() % 10; const int new_cnt = randomi() % 10;
piForTimes(new_cnt) { piForTimes(new_cnt) {
spawnClient(); spawnClient();
} }
}, 12_Hz); },
12_Hz);
deleteThread.start([&clients, &clients_mutex](){ deleteThread.start(
[&clients, &clients_mutex]() {
const int rm_cnt = randomi() % 10; const int rm_cnt = randomi() % 10;
piForTimes(rm_cnt) { piForTimes(rm_cnt) {
ClientSendThread * c = nullptr; ClientSendThread * c = nullptr;
clients_mutex.lock(); clients_mutex.lock();
if (clients.size() > 10) { if (clients.size() > 10) {
c = clients.take_back(); c = clients.take_front();
} }
clients_mutex.unlock(); clients_mutex.unlock();
if (c) { if (c) {
@@ -189,22 +192,25 @@ TEST(ClientServer, DynamicClients) {
piCout << "remove client" << clients.size(); piCout << "remove client" << clients.size();
} }
} }
}, 13_Hz); },
13_Hz);
(1_s).sleep(); (10_s).sleep();
EXPECT_GE(s->clientsCount(), 10); EXPECT_GE(s->clientsCount(), 10);
piCout << "now clients" << clients.size(); piCout << "now clients" << clients.size();
//delete s;
deleteThread.stopAndWait(); deleteThread.stopAndWait();
spawnThread.stopAndWait(); spawnThread.stopAndWait();
piCout << "total clients" << clients.size(); piCout << "total clients" << clients.size();
piDeleteAllAndClear(clients); piDeleteAllAndClear(clients);
waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout); waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout);
EXPECT_EQ(0, s->clientsCount()); EXPECT_EQ(0, s->clientsCount());
delete s;
} }