From 85b0bc52c8384c739fe38cc028f9c0faaaffeb86 Mon Sep 17 00:00:00 2001 From: Adrien Beraud Date: Wed, 15 Jul 2026 13:11:25 -0400 Subject: [PATCH] test: reproduce completed resolver callback race --- tests/test_http.cpp | 31 +++++++++++++++++++++++++++++++ tests/test_http.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/tests/test_http.cpp b/tests/test_http.cpp index 192fa502..911c1942 100644 --- a/tests/test_http.cpp +++ b/tests/test_http.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -332,6 +333,36 @@ HttpTester::test_parse_url_just_slash() CPPUNIT_ASSERT_EQUAL(std::string("/"), parsed.target); } +void +HttpTester::test_completed_resolver_callback_uses_io_thread() +{ + asio::io_context ioContext; + const asio::ip::tcp::endpoint endpoint(asio::ip::address_v4::loopback(), 80); + dht::http::Resolver resolver(ioContext, {endpoint}); + const auto callerThread = std::this_thread::get_id(); + std::thread::id callbackThread; + asio::error_code callbackError; + std::vector resolvedEndpoints; + std::atomic_bool callbackCalled {false}; + + resolver.add_callback([&](const asio::error_code& ec, const auto& endpoints) { + callbackError = ec; + callbackThread = std::this_thread::get_id(); + resolvedEndpoints = endpoints; + callbackCalled = true; + }); + + CPPUNIT_ASSERT(!callbackCalled); + std::thread ioThread([&] { ioContext.run(); }); + ioThread.join(); + + CPPUNIT_ASSERT(callbackCalled); + CPPUNIT_ASSERT(!callbackError); + CPPUNIT_ASSERT(callbackThread != callerThread); + CPPUNIT_ASSERT_EQUAL(size_t(1), resolvedEndpoints.size()); + CPPUNIT_ASSERT_EQUAL(endpoint, resolvedEndpoints.front()); +} + void HttpTester::test_send_json() { diff --git a/tests/test_http.h b/tests/test_http.h index 78bfd181..d5ee9a12 100644 --- a/tests/test_http.h +++ b/tests/test_http.h @@ -39,6 +39,7 @@ class HttpTester : public CppUnit::TestFixture CPPUNIT_TEST(test_parse_url_complex_path_query_fragment); CPPUNIT_TEST(test_parse_url_empty); CPPUNIT_TEST(test_parse_url_just_slash); + CPPUNIT_TEST(test_completed_resolver_callback_uses_io_thread); // send CPPUNIT_TEST(test_send_json); CPPUNIT_TEST_SUITE_END(); @@ -82,6 +83,7 @@ public: void test_parse_url_complex_path_query_fragment(); void test_parse_url_empty(); void test_parse_url_just_slash(); + void test_completed_resolver_callback_uses_io_thread(); /** * Test send(json) */