Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 33 additions & 37 deletions test/mcp/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down