close
This commit is contained in:
@@ -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,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;
|
||||
@@ -76,7 +78,7 @@ public:
|
||||
|
||||
int getServerPongs(PIClientServer::Server * s) {
|
||||
int pongs = 0;
|
||||
s->forEachClient([&pongs](PIClientServer::ServerClient * sc){
|
||||
s->forEachClient([&pongs](PIClientServer::ServerClient * sc) {
|
||||
const auto c = static_cast<TestServerClient<> *>(sc);
|
||||
pongs += c->pongCnt();
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -118,27 +116,27 @@ TEST(ClientServer, ManyClients) {
|
||||
|
||||
EXPECT_EQ(getClientsPings(clients), 0);
|
||||
|
||||
for (const auto c : clients) {
|
||||
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;}));
|
||||
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<TestServerClient<> *>(sc)->ping(); });
|
||||
const auto clientCheckPong = [&clients](){return clients.every([](ClientSendThread * c){return c->client->pongCnt() == 1;});};
|
||||
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) {
|
||||
for (const auto c: clients) {
|
||||
c->startSend();
|
||||
}
|
||||
(100_ms).sleep();
|
||||
EXPECT_TRUE(getClientsPings(clients) > clients_count*2);
|
||||
EXPECT_TRUE(getServerPongs(s) > clients_count*2);
|
||||
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());
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user