diff --git a/libs/client_server/piclientserver_server.cpp b/libs/client_server/piclientserver_server.cpp index f1700b01..edfd2083 100644 --- a/libs/client_server/piclientserver_server.cpp +++ b/libs/client_server/piclientserver_server.cpp @@ -72,11 +72,7 @@ PIClientServer::Server::~Server() { clean_thread->waitForFinish(); piDeleteSafety(clean_thread); stopServer(); - for (auto c: clients) { - c->aboutDelete(); - c->destroy(); - delete c; - } + closeAll(); piDeleteSafety(tcp_server); } diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index 19eddf28..7cb7a1dc 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -527,6 +527,7 @@ bool PIEthernet::listen(bool threaded) { listen_threaded = true; server_bounded = false; server_thread_.start(server_func); + server_thread_.waitForStart(); return true; } listen_threaded = server_bounded = false; diff --git a/libs/main/types/pisystemtime.cpp b/libs/main/types/pisystemtime.cpp index 4d218968..f26b7bd6 100644 --- a/libs/main/types/pisystemtime.cpp +++ b/libs/main/types/pisystemtime.cpp @@ -105,7 +105,7 @@ extern clock_serv_t __pi_mac_clock; //! Используйте этот метод для ожидания разниц системных времен или своего времени. //! Если метод будет вызван для системного времени \a PISystemTime::current(), то //! ожидание будет почти бесконечным -void PISystemTime::sleep() { +void PISystemTime::sleep() const { piUSleep(piFloord(toMicroseconds())); } @@ -118,7 +118,7 @@ void PISystemTime::toTimespec(void * ts) { } -PISystemTime::Frequency PISystemTime::toFrequency() { +PISystemTime::Frequency PISystemTime::toFrequency() const { return PISystemTime::Frequency::fromSystemTime(*this); } diff --git a/libs/main/types/pisystemtime.h b/libs/main/types/pisystemtime.h index bb197377..c7438c7e 100644 --- a/libs/main/types/pisystemtime.h +++ b/libs/main/types/pisystemtime.h @@ -253,7 +253,7 @@ public: //! \~english Sleep for this time //! \~russian Ожидать это время - void sleep(); + void sleep() const; //! \~english On *nix system assign current value to timespec struct //! \~russian На *nix системах присваивает время к timespec структуре @@ -261,7 +261,7 @@ public: //! \~english Returns \a Frequency that corresponds this time interval //! \~russian Возвращает \a Frequency соответствующую этому временному интервалу - PISystemTime::Frequency toFrequency(); + PISystemTime::Frequency toFrequency() const; //! \~english Returns "yyyy-MM-dd hh:mm:ss.zzz" for absolute time and " ..." for relative //! \~russian Возвращает "yyyy-MM-dd hh:mm:ss.zzz" для абсолютного времени и " ..." для относительного diff --git a/tests/client_server/client_server_test.cpp b/tests/client_server/client_server_test.cpp index bbe639c5..e0980649 100644 --- a/tests/client_server/client_server_test.cpp +++ b/tests/client_server/client_server_test.cpp @@ -32,6 +32,7 @@ Client * createAndConnectClient() { TEST(ClientServer, OneClient) { auto const loop_timeout = 1000_ms; auto s = createServer(); + piMinSleep(); auto c = createAndConnectClient>(); waitLoop([s]() { return s->clientsCount() > 0; }, loop_timeout); @@ -106,6 +107,7 @@ TEST(ClientServer, ManyClients) { constexpr int clients_count = 20; PIVector clients; auto s = createServer(); + piMinSleep(); piForTimes(clients_count) { clients.append(new ClientSendThread()); @@ -137,7 +139,7 @@ TEST(ClientServer, ManyClients) { for (const auto c: clients) { c->startSend(); } - (100_ms).sleep(); + loop_timeout.sleep(); EXPECT_TRUE(getClientsPings(clients) > clients_count * 2); EXPECT_TRUE(getServerPongs(s) > clients_count * 2); piDeleteAllAndClear(clients); @@ -147,7 +149,7 @@ TEST(ClientServer, ManyClients) { } TEST(ClientServer, DynamicClients) { - auto const loop_timeout = 100_ms; + auto const loop_timeout = 10_ms; constexpr int clients_count = 20; PIVector clients; PIMutex clients_mutex; @@ -160,7 +162,6 @@ TEST(ClientServer, DynamicClients) { clients_mutex.lock(); clients << c; clients_mutex.unlock(); - piCout << "new client" << clients.size(); }; piForTimes(clients_count) { @@ -178,9 +179,8 @@ TEST(ClientServer, DynamicClients) { piForTimes(new_cnt) { spawnClient(); } - piCout << "+++++++"; }, - 12_Hz); + 120_Hz); deleteThread.start( [&clients, &clients_mutex]() { @@ -194,28 +194,20 @@ TEST(ClientServer, DynamicClients) { clients_mutex.unlock(); if (c) { delete c; - piCout << "remove client" << clients.size(); } } - piCout << "----------"; }, - 13_Hz); + 130_Hz); - (2_s).sleep(); + (loop_timeout * clients_count).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); + waitLoop([s]() { return s->clientsCount() == 0; }, loop_timeout * clients_count); EXPECT_EQ(0, s->clientsCount()); delete s;