From c48f9ddf321fbe55cf89c0351509a0191cc5098e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 1 Dec 2026 18:32:10 +0900 Subject: [PATCH] Test the wake-up of the backoff wait without replacing `ConditionVariable.new` ## Motivation and Context `test_input_required_cancellation_wakes_the_backoff_wait`, added in #730, replaced `ConditionVariable.new` for the whole process while it ran. The second call and every later one, from any thread and from `Monitor#new_cond` as well, returned one condition variable whose `wait` ignored its timeout, and the test counted on the client creating exactly one condition variable before the backoff wait. A thread that created one at that moment got the wrong object, so the test could fail, or leave that thread waiting, depending on what else was running. Two tests replace it and leave the class alone. One enters the backoff wait directly, for far longer than the test may take, and cancels once the thread sleeps, so only a wake-up ends it in time. The other cancels as the hook of the backoff wait is registered during a `call_tool`, whatever the timing, and checks that the call raises `CancelledError` with the reason and sends no further request. Follow-up to #730. ## How Has This Been Tested? `test/mcp/client_test.rb`. The first test fails without the signal in the hook of the backoff wait, and the second without the check that precedes each request. ## Breaking Changes None. Only tests change. --- test/mcp/client_test.rb | 70 +++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/test/mcp/client_test.rb b/test/mcp/client_test.rb index d276b64e..1a5725c8 100644 --- a/test/mcp/client_test.rb +++ b/test/mcp/client_test.rb @@ -782,52 +782,48 @@ def test_input_required_cancellation_after_response_takes_precedence_over_round_ end def test_input_required_cancellation_wakes_the_backoff_wait + client = Client.new(transport: mock) + cancellation = MCP::Cancellation.new + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + + # Far longer than the test may take, so only the cancellation can end the wait. + waiting = Thread.new { client.send(:sleep_with_cancellation, 30, cancellation) } + + until waiting.status == "sleep" + flunk("The backoff wait never started") if Process.clock_gettime(Process::CLOCK_MONOTONIC) - started > 5 + sleep(0.005) + end + + cancellation.cancel(reason: "while waiting") + + assert(waiting.join(5), "Cancellation did not wake the backoff wait") + ensure + waiting.kill.join + end + + def test_input_required_cancellation_during_the_backoff_sends_no_further_request transport = mock transport.expects(:send_notification).never state_only = { "result" => { "resultType" => "input_required", "requestState" => "busy-state" } } - final_response = { "result" => { "content" => [] } } - requests = [] - transport.stubs(:send_request).with do |args| - requests << args[:request] - true - end.returns(state_only, final_response) - - wait_state = Queue.new - backoff_condition = ConditionVariable.new - wait = backoff_condition.method(:wait) - backoff_condition.define_singleton_method(:wait) do |mutex, _timeout| - wait_state << :waiting - # The wait can only finish when the cancellation callback signals it. - wait.call(mutex) - end - ConditionVariable.stubs(:new).returns(ConditionVariable.new, backoff_condition) + transport.expects(:send_request).once.returns(state_only) + # The first hook belongs to the request and the second to the backoff wait that follows its answer, + # so cancelling as the second one is registered cancels during the backoff, whatever the timing. cancellation = MCP::Cancellation.new + registrations = 0 + register = cancellation.method(:on_cancel) + + cancellation.define_singleton_method(:on_cancel) do |&block| + register.call(&block).tap { cancel(reason: "during the backoff") if (registrations += 1) == 2 } + end + client = Client.new(transport: transport) client.on_elicitation { flunk("No handler should run") } - worker = Thread.new do - client.call_tool(name: "my_tool", cancellation: cancellation) - rescue MCP::CancelledError => e - e - ensure - wait_state << :finished - end - canceller = Thread.new do - state = wait_state.pop - cancellation.cancel(reason: "while waiting") if state == :waiting - state - end + error = assert_raises(MCP::CancelledError) { client.call_tool(name: "my_tool", cancellation: cancellation) } - assert(canceller.join(5), "The backoff never started") - assert_equal(:waiting, canceller.value) - assert(worker.join(5), "Cancellation did not wake the backoff wait") - assert_kind_of(MCP::CancelledError, worker.value) - assert_equal("while waiting", worker.value.reason) - assert_equal(1, requests.size) - ensure - canceller&.kill&.join - worker&.kill&.join + assert_equal("during the backoff", error.reason) + assert_equal(2, registrations) end def test_input_required_wait_with_cancellation_times_out_and_deregisters_its_hook