]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Ensure our unit tests can be run in random order
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 14 Sep 2023 09:12:07 +0000 (11:12 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 14 Sep 2023 09:12:07 +0000 (11:12 +0200)
pdns/dnsdistdist/test-dnsdistnghttp2-in_cc.cc
pdns/dnsdistdist/test-dnsdistnghttp2_cc.cc
pdns/dnsdistdist/test-dnsdistnghttp2_common.hh
pdns/dnsdistdist/test-dnsdisttcp_cc.cc

index 2c98427c06932f32f2fa42e45e779fae687d25bf..75b256d7e4579a936e1a5a97f8883a24db417517 100644 (file)
@@ -448,23 +448,27 @@ struct TestFixture
 {
   TestFixture()
   {
-    s_steps.clear();
-    s_connectionContexts.clear();
-    s_connectionBuffers.clear();
-    s_connectionID = 0;
-    s_mplexer = std::make_unique<MockupFDMultiplexer>();
+    reset();
   }
   TestFixture(const TestFixture&) = delete;
   TestFixture(TestFixture&&) = delete;
   TestFixture& operator=(const TestFixture&) = delete;
   TestFixture& operator=(TestFixture&&) = delete;
   ~TestFixture()
+  {
+    reset();
+  }
+private:
+  void reset()
   {
     s_steps.clear();
     s_connectionContexts.clear();
     s_connectionBuffers.clear();
     s_connectionID = 0;
-    s_mplexer.reset();
+    /* we _NEED_ to set this function to empty otherwise we might get what was set
+       by the last test, and we might not like it at all */
+    s_processQuery = nullptr;
+    g_proxyProtocolACL.clear();
   }
 };
 
@@ -500,10 +504,6 @@ BOOST_FIXTURE_TEST_CASE(test_IncomingConnection_SelfAnswered, TestFixture)
   pwR.xfr32BitInt(0x01020304);
   pwR.commit();
 
-  /* we _NEED_ to set this function to empty otherwise we might get what was set
-     by the last test, and we might not like it at all */
-  s_processQuery = nullptr;
-
   {
     /* dnsdist drops the query right away after receiving it, client closes the connection */
     s_connectionContexts[counter++] = ExpectedData{{}, {query}, {response}, {403U}};
index a7afea2063e11c698cf2c470c342342569edf483..b971e4ac15173018fe92fd70808cf5c24fe4ea76 100644 (file)
@@ -70,6 +70,7 @@ struct ExpectedData
 
 static std::deque<ExpectedStep> s_steps;
 static std::map<uint16_t, ExpectedData> s_responses;
+static std::unique_ptr<FDMultiplexer> s_mplexer;
 
 std::ostream& operator<<(std::ostream& os, const ExpectedStep::ExpectedRequest d);
 
index 5c79679aba0fe2863193b0ed44d482c0bc3f6c2b..b6f9bdd10bb21482d9eaf7741410796298674ae6 100644 (file)
@@ -153,5 +153,3 @@ static ComboAddress getBackendAddress(const std::string& lastDigit, uint16_t por
 
   return ComboAddress("192.0.2." + lastDigit, port);
 }
-
-static std::unique_ptr<FDMultiplexer> s_mplexer;
index 296cd44cb62e55f4e4afb7c5be4cd5a4aba2fda7..14a13cde8a2ef9d51de63c2750e3de834743e07b 100644 (file)
@@ -435,6 +435,39 @@ static void prependPayloadEditingID(PacketBuffer& buffer, const PacketBuffer& pa
   buffer.insert(buffer.begin(), newPayload.begin(), newPayload.end());
 }
 
+struct TestFixture
+{
+  TestFixture()
+  {
+    reset();
+  }
+  TestFixture(const TestFixture&) = delete;
+  TestFixture(TestFixture&&) = delete;
+  TestFixture& operator=(const TestFixture&) = delete;
+  TestFixture& operator=(TestFixture&&) = delete;
+  ~TestFixture()
+  {
+    reset();
+  }
+
+  static void reset()
+  {
+    s_steps.clear();
+    s_readBuffer.clear();
+    s_writeBuffer.clear();
+    s_backendReadBuffer.clear();
+    s_backendWriteBuffer.clear();
+
+    g_proxyProtocolACL.clear();
+    g_verbose = false;
+    IncomingTCPConnectionState::clearAllDownstreamConnections();
+
+    /* we _NEED_ to set this function to empty otherwise we might get what was set
+       by the last test, and we might not like it at all */
+    s_processQuery = nullptr;
+  }
+};
+
 static void testInit(const std::string& name, TCPClientThreadData& threadData)
 {
 #ifdef DEBUGLOG_ENABLED
@@ -443,22 +476,13 @@ static void testInit(const std::string& name, TCPClientThreadData& threadData)
   (void) name;
 #endif
 
-  s_steps.clear();
-  s_readBuffer.clear();
-  s_writeBuffer.clear();
-  s_backendReadBuffer.clear();
-  s_backendWriteBuffer.clear();
-
-  g_proxyProtocolACL.clear();
-  g_verbose = false;
-  IncomingTCPConnectionState::clearAllDownstreamConnections();
-
+  TestFixture::reset();
   threadData.mplexer = std::make_unique<MockupFDMultiplexer>();
 }
 
 #define TEST_INIT(str) testInit(str, threadData)
 
-BOOST_AUTO_TEST_CASE(test_IncomingConnection_SelfAnswered)
+BOOST_FIXTURE_TEST_CASE(test_IncomingConnection_SelfAnswered, TestFixture)
 {
   auto local = getBackendAddress("1", 80);
   ClientState localCS(local, true, false, false, "", {});
@@ -705,7 +729,7 @@ BOOST_AUTO_TEST_CASE(test_IncomingConnection_SelfAnswered)
   }
 }
 
-BOOST_AUTO_TEST_CASE(test_IncomingConnectionWithProxyProtocol_SelfAnswered)
+BOOST_FIXTURE_TEST_CASE(test_IncomingConnectionWithProxyProtocol_SelfAnswered, TestFixture)
 {
   auto local = getBackendAddress("1", 80);
   ClientState localCS(local, true, false, false, "", {});
@@ -835,7 +859,7 @@ BOOST_AUTO_TEST_CASE(test_IncomingConnectionWithProxyProtocol_SelfAnswered)
   }
 }
 
-BOOST_AUTO_TEST_CASE(test_IncomingConnection_BackendNoOOOR)
+BOOST_FIXTURE_TEST_CASE(test_IncomingConnection_BackendNoOOOR, TestFixture)
 {
   auto local = getBackendAddress("1", 80);
   ClientState localCS(local, true, false, false, "", {});
@@ -1735,7 +1759,7 @@ BOOST_AUTO_TEST_CASE(test_IncomingConnection_BackendNoOOOR)
   }
 }
 
-BOOST_AUTO_TEST_CASE(test_IncomingConnectionOOOR_BackendOOOR)
+BOOST_FIXTURE_TEST_CASE(test_IncomingConnectionOOOR_BackendOOOR, TestFixture)
 {
   auto local = getBackendAddress("1", 80);
   ClientState localCS(local, true, false, false, "", {});
@@ -3875,7 +3899,7 @@ BOOST_AUTO_TEST_CASE(test_IncomingConnectionOOOR_BackendOOOR)
   }
 }
 
-BOOST_AUTO_TEST_CASE(test_IncomingConnectionOOOR_BackendNotOOOR)
+BOOST_FIXTURE_TEST_CASE(test_IncomingConnectionOOOR_BackendNotOOOR, TestFixture)
 {
   auto local = getBackendAddress("1", 80);
   ClientState localCS(local, true, false, false, "", {});