From: Remi Gacogne Date: Thu, 14 Sep 2023 09:12:07 +0000 (+0200) Subject: dnsdist: Ensure our unit tests can be run in random order X-Git-Tag: dnsdist-1.9.0-alpha1~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8298e5d4011949dd5a26937abe7db52f43e2ab84;p=thirdparty%2Fpdns.git dnsdist: Ensure our unit tests can be run in random order --- diff --git a/pdns/dnsdistdist/test-dnsdistnghttp2-in_cc.cc b/pdns/dnsdistdist/test-dnsdistnghttp2-in_cc.cc index 2c98427c06..75b256d7e4 100644 --- a/pdns/dnsdistdist/test-dnsdistnghttp2-in_cc.cc +++ b/pdns/dnsdistdist/test-dnsdistnghttp2-in_cc.cc @@ -448,23 +448,27 @@ struct TestFixture { TestFixture() { - s_steps.clear(); - s_connectionContexts.clear(); - s_connectionBuffers.clear(); - s_connectionID = 0; - s_mplexer = std::make_unique(); + 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}}; diff --git a/pdns/dnsdistdist/test-dnsdistnghttp2_cc.cc b/pdns/dnsdistdist/test-dnsdistnghttp2_cc.cc index a7afea2063..b971e4ac15 100644 --- a/pdns/dnsdistdist/test-dnsdistnghttp2_cc.cc +++ b/pdns/dnsdistdist/test-dnsdistnghttp2_cc.cc @@ -70,6 +70,7 @@ struct ExpectedData static std::deque s_steps; static std::map s_responses; +static std::unique_ptr s_mplexer; std::ostream& operator<<(std::ostream& os, const ExpectedStep::ExpectedRequest d); diff --git a/pdns/dnsdistdist/test-dnsdistnghttp2_common.hh b/pdns/dnsdistdist/test-dnsdistnghttp2_common.hh index 5c79679aba..b6f9bdd10b 100644 --- a/pdns/dnsdistdist/test-dnsdistnghttp2_common.hh +++ b/pdns/dnsdistdist/test-dnsdistnghttp2_common.hh @@ -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 s_mplexer; diff --git a/pdns/dnsdistdist/test-dnsdisttcp_cc.cc b/pdns/dnsdistdist/test-dnsdisttcp_cc.cc index 296cd44cb6..14a13cde8a 100644 --- a/pdns/dnsdistdist/test-dnsdisttcp_cc.cc +++ b/pdns/dnsdistdist/test-dnsdisttcp_cc.cc @@ -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(); } #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, "", {});