From 4947d33ad83a6f50fddd9c6ef9792453e40a85a9 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sat, 5 Feb 2022 19:28:14 +0100 Subject: [PATCH] Better support / fixes for clients with fd 0 --- FlashMQTests/tst_maintests.cpp | 22 ++++++++++++++++++---- client.cpp | 19 ++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/FlashMQTests/tst_maintests.cpp b/FlashMQTests/tst_maintests.cpp index 437d107..820b5f5 100644 --- a/FlashMQTests/tst_maintests.cpp +++ b/FlashMQTests/tst_maintests.cpp @@ -62,10 +62,10 @@ public: ~MainTests(); private slots: - void init(); - void cleanup(); + void init(); // will be called before each test function is executed + void cleanup(); // will be called after every test function. - void cleanupTestCase(); + void cleanupTestCase(); // will be called after the last test function was executed. void test_circbuf(); void test_circbuf_unwrapped_doubling(); @@ -1091,6 +1091,20 @@ void MainTests::testSavingSessions() } } -QTEST_GUILESS_MAIN(MainTests) +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + app.setAttribute(Qt::AA_Use96Dpi, true); + MainTests tc; + + QTEST_SET_MAIN_SOURCE_PATH; + + // You can more easily debug tests (in case of crashes) by running directly, instead of called as slots by Qt. + //tc.init(); + //tc.testCopyPacket(); + //return 0; + + return QTest::qExec(&tc, argc, argv); +} #include "tst_maintests.moc" diff --git a/client.cpp b/client.cpp index 64b11d1..65f924e 100644 --- a/client.cpp +++ b/client.cpp @@ -66,9 +66,12 @@ Client::~Client() disconnectReason = "not specified"; logger->logf(LOG_NOTICE, "Removing client '%s'. Reason(s): %s", repr().c_str(), disconnectReason.c_str()); - if (epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL) != 0) - logger->logf(LOG_ERR, "Removing fd %d of client '%s' from epoll produced error: %s", fd, repr().c_str(), strerror(errno)); - close(fd); + if (fd > 0) // this check is essentially for testing, when working with a dummy fd. + { + if (epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL) != 0) + logger->logf(LOG_ERR, "Removing fd %d of client '%s' from epoll produced error: %s", fd, repr().c_str(), strerror(errno)); + close(fd); + } // MQTT-3.1.2-6 if (cleanSession) @@ -338,6 +341,11 @@ void Client::setReadyForWriting(bool val) return; #endif +#ifdef TESTING + if (fd == 0) + return; +#endif + if (disconnecting) return; @@ -364,6 +372,11 @@ void Client::setReadyForReading(bool val) return; #endif +#ifdef TESTING + if (fd == 0) + return; +#endif + if (disconnecting) return; -- libgit2 0.21.4