From 5519f7f9485c342d7c868846985750d6f152f359 Mon Sep 17 00:00:00 2001 From: Novo Date: Tue, 30 Jun 2026 11:57:35 +0200 Subject: [PATCH] test: recursive async IPC calls Test that async processing threads correctly handle incoming requests while waiting on another. Also use the opportunity to refactor some of the tests to remove the try-catch blocks. --- test/mp/test/test.cpp | 73 +++++++++++++------------------------------ 1 file changed, 21 insertions(+), 52 deletions(-) diff --git a/test/mp/test/test.cpp b/test/mp/test/test.cpp index eb4b5ec7..41124ac2 100644 --- a/test/mp/test/test.cpp +++ b/test/mp/test/test.cpp @@ -38,6 +38,15 @@ #include #include +//! Assert that a call throws std::runtime_error with the given message. +#define EXPECT_EXCEPTION(call, message) \ + try { \ + call; \ + KJ_EXPECT(false); \ + } catch (const std::runtime_error& e) { \ + KJ_EXPECT(std::string_view{e.what()} == message); \ + } + namespace mp { namespace test { @@ -234,6 +243,11 @@ KJ_TEST("Call FooInterface methods") KJ_EXPECT(foo->passFn([]{ return 10; }) == 10); + // Recursive async IPC calls + KJ_EXPECT(foo->passFn([foo]{ + return foo->passFn([]{ return 1; }); + }) == 1); + std::vector data_in; data_in.push_back(std::make_shared(FooData{'H', 'i'})); data_in.push_back(nullptr); @@ -251,14 +265,7 @@ KJ_TEST("Call IPC method after client connection is closed") KJ_EXPECT(foo->add(1, 2) == 3); setup.client_disconnect(); - bool disconnected{false}; - try { - foo->add(1, 2); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method called after disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->add(1, 2), "IPC client method called after disconnect."); } KJ_TEST("Calling IPC method after server connection is closed") @@ -268,14 +275,7 @@ KJ_TEST("Calling IPC method after server connection is closed") KJ_EXPECT(foo->add(1, 2) == 3); setup.server_disconnect(); - bool disconnected{false}; - try { - foo->add(1, 2); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->add(1, 2), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling IPC method and disconnecting during the call") @@ -288,14 +288,7 @@ KJ_TEST("Calling IPC method and disconnecting during the call") // handling the callFn call to make sure this case is handled cleanly. setup.server->m_impl->m_fn = setup.client_disconnect; - bool disconnected{false}; - try { - foo->callFn(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFn(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling IPC method, disconnecting and blocking during the call") @@ -330,14 +323,7 @@ KJ_TEST("Calling IPC method, disconnecting and blocking during the call") signal.get_future().get(); }; - bool disconnected{false}; - try { - foo->callFnAsync(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); // Now that the disconnect has been detected, set signal allowing the // callFnAsync() IPC call to return. Since signalling may not wake up the @@ -374,14 +360,7 @@ KJ_TEST("Worker thread destroyed before it is initialized") std::this_thread::sleep_for(std::chrono::milliseconds(10)); }; - bool disconnected{false}; - try { - foo->callFnAsync(); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - disconnected = true; - } - KJ_EXPECT(disconnected); + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling async IPC method, with server disconnect racing the call") @@ -406,12 +385,7 @@ KJ_TEST("Calling async IPC method, with server disconnect racing the call") std::this_thread::sleep_for(std::chrono::milliseconds(10)); }; - try { - foo->callFnAsync(); - KJ_EXPECT(false); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - } + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Calling async IPC method, with server disconnect after cleanup") @@ -435,12 +409,7 @@ KJ_TEST("Calling async IPC method, with server disconnect after cleanup") setup.server_disconnect(); }; - try { - foo->callFnAsync(); - KJ_EXPECT(false); - } catch (const std::runtime_error& e) { - KJ_EXPECT(std::string_view{e.what()} == "IPC client method call interrupted by disconnect."); - } + EXPECT_EXCEPTION(foo->callFnAsync(), "IPC client method call interrupted by disconnect."); } KJ_TEST("Destroying ProxyClient<> with destroy method after peer disconnect")