Commit 4947d33ad83a6f50fddd9c6ef9792453e40a85a9

Authored by Wiebe Cazemier
1 parent 5614ee5c

Better support / fixes for clients with fd 0

It caused really funky behavior. When a destructed client closed fd 0,
eventfd() would give 0 back as fd. This would then later give errors.
FlashMQTests/tst_maintests.cpp
@@ -62,10 +62,10 @@ public: @@ -62,10 +62,10 @@ public:
62 ~MainTests(); 62 ~MainTests();
63 63
64 private slots: 64 private slots:
65 - void init();  
66 - void cleanup(); 65 + void init(); // will be called before each test function is executed
  66 + void cleanup(); // will be called after every test function.
67 67
68 - void cleanupTestCase(); 68 + void cleanupTestCase(); // will be called after the last test function was executed.
69 69
70 void test_circbuf(); 70 void test_circbuf();
71 void test_circbuf_unwrapped_doubling(); 71 void test_circbuf_unwrapped_doubling();
@@ -1091,6 +1091,20 @@ void MainTests::testSavingSessions() @@ -1091,6 +1091,20 @@ void MainTests::testSavingSessions()
1091 } 1091 }
1092 } 1092 }
1093 1093
1094 -QTEST_GUILESS_MAIN(MainTests) 1094 +int main(int argc, char *argv[])
  1095 +{
  1096 + QCoreApplication app(argc, argv);
  1097 + app.setAttribute(Qt::AA_Use96Dpi, true);
  1098 + MainTests tc;
  1099 +
  1100 + QTEST_SET_MAIN_SOURCE_PATH;
  1101 +
  1102 + // You can more easily debug tests (in case of crashes) by running directly, instead of called as slots by Qt.
  1103 + //tc.init();
  1104 + //tc.testCopyPacket();
  1105 + //return 0;
  1106 +
  1107 + return QTest::qExec(&tc, argc, argv);
  1108 +}
1095 1109
1096 #include "tst_maintests.moc" 1110 #include "tst_maintests.moc"
client.cpp
@@ -66,9 +66,12 @@ Client::~Client() @@ -66,9 +66,12 @@ Client::~Client()
66 disconnectReason = "not specified"; 66 disconnectReason = "not specified";
67 67
68 logger->logf(LOG_NOTICE, "Removing client '%s'. Reason(s): %s", repr().c_str(), disconnectReason.c_str()); 68 logger->logf(LOG_NOTICE, "Removing client '%s'. Reason(s): %s", repr().c_str(), disconnectReason.c_str());
69 - if (epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL) != 0)  
70 - logger->logf(LOG_ERR, "Removing fd %d of client '%s' from epoll produced error: %s", fd, repr().c_str(), strerror(errno));  
71 - close(fd); 69 + if (fd > 0) // this check is essentially for testing, when working with a dummy fd.
  70 + {
  71 + if (epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL) != 0)
  72 + logger->logf(LOG_ERR, "Removing fd %d of client '%s' from epoll produced error: %s", fd, repr().c_str(), strerror(errno));
  73 + close(fd);
  74 + }
72 75
73 // MQTT-3.1.2-6 76 // MQTT-3.1.2-6
74 if (cleanSession) 77 if (cleanSession)
@@ -338,6 +341,11 @@ void Client::setReadyForWriting(bool val) @@ -338,6 +341,11 @@ void Client::setReadyForWriting(bool val)
338 return; 341 return;
339 #endif 342 #endif
340 343
  344 +#ifdef TESTING
  345 + if (fd == 0)
  346 + return;
  347 +#endif
  348 +
341 if (disconnecting) 349 if (disconnecting)
342 return; 350 return;
343 351
@@ -364,6 +372,11 @@ void Client::setReadyForReading(bool val) @@ -364,6 +372,11 @@ void Client::setReadyForReading(bool val)
364 return; 372 return;
365 #endif 373 #endif
366 374
  375 +#ifdef TESTING
  376 + if (fd == 0)
  377 + return;
  378 +#endif
  379 +
367 if (disconnecting) 380 if (disconnecting)
368 return; 381 return;
369 382