]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Gracefully handle the mplexer test pipe having only room for one 8196/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 14 Aug 2019 12:48:29 +0000 (14:48 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 14 Aug 2019 12:48:29 +0000 (14:48 +0200)
pdns/test-mplexer.cc

index 5a3269f5593149a9267c94da0ffc2b008aaa636c..f01500b6d1e4b6ffcd28a5ae438b4ab1454b391e 100644 (file)
@@ -122,35 +122,41 @@ BOOST_AUTO_TEST_CASE(test_MPlexer) {
                       &ttd);
 
   /* both should be available */
+  readCBCalled = false;
+  writeCBCalled = false;
   readyFDs.clear();
+
   mplexer->getAvailableFDs(readyFDs, 0);
-  if (readyFDs.size() == 1) {
-    /* something is wrong, we need some debug infos */
-    cerr<<"FDMultiPlexer implementation is "<<mplexer->getName()<<endl;
-    cerr<<"Watching "<<mplexer->getWatchedFDCount(false)<<" FDs for read and "<<mplexer->getWatchedFDCount(true)<<" for write"<<endl;
-    cerr<<"pipes[0] is "<<pipes[0]<<endl;
-    cerr<<"pipes[1] is "<<pipes[1]<<endl;
-    cerr<<"The file descriptor returned as ready is "<<readyFDs.at(0)<<endl;
-    char buffer[2];
-    ssize_t res = write(pipes[1], "0", 1);
-    int saved = errno;
-    cerr<<"Writing to pipes[1] returns "<<res<<endl;
-    if (res == -1) {
-      cerr<<"errno is "<<saved<<endl;
-    }
-    res = read(pipes[0], &buffer[0], sizeof(buffer));
-    saved = errno;
-    cerr<<"Reading from pipes[0] returns "<<res<<endl;
-    if (res == -1) {
-      cerr<<"errno is "<<saved<<endl;
-    }
+  BOOST_REQUIRE_GT(readyFDs.size(), 0);
+  if (readyFDs.size() == 2) {
+    ready = mplexer->run(&now, 100);
+    BOOST_CHECK_EQUAL(ready, 2);
+  }
+  else if (readyFDs.size() == 1) {
+    /* under high pressure (lots of existing pipes on the system, for example,
+       the pipe might only have room for one 'buffer' and will not be writable
+       after our write of 1 byte, we need to read it so that the pipe becomes
+       writable again */
+    /* make sure the pipe is readable, otherwise something is off */
+    BOOST_REQUIRE_EQUAL(readyFDs.at(0), pipes[0]);
+    ready = mplexer->run(&now, 100);
+    BOOST_CHECK_EQUAL(ready, 1);
+    BOOST_CHECK_EQUAL(readCBCalled, true);
+    BOOST_CHECK_EQUAL(writeCBCalled, false);
+    char buffer[1];
+    ssize_t got = read(pipes[0], &buffer[0], sizeof(buffer));
+    BOOST_CHECK_EQUAL(got, 1);
+
+    /* ok, the pipe should be writable now, but not readable */
+    readyFDs.clear();
+    mplexer->getAvailableFDs(readyFDs, 0);
+    BOOST_CHECK_EQUAL(readyFDs.size(), 1);
+    BOOST_REQUIRE_EQUAL(readyFDs.at(0), pipes[1]);
+
+    ready = mplexer->run(&now, 100);
+    BOOST_CHECK_EQUAL(ready, 1);
   }
-  BOOST_CHECK_EQUAL(readyFDs.size(), 2);
 
-  readCBCalled = false;
-  writeCBCalled = false;
-  ready = mplexer->run(&now, 100);
-  BOOST_CHECK_EQUAL(ready, 2);
   BOOST_CHECK_EQUAL(readCBCalled, true);
   BOOST_CHECK_EQUAL(writeCBCalled, true);