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