Skip to content

Commit e2569eb

Browse files
fix: improve cancellation test to wait for tool execution before cancelling
Co-authored-by: SteveSandersonMS <1101362+SteveSandersonMS@users.noreply.github.com>
1 parent 7d71db9 commit e2569eb

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

dotnet/test/SessionTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,23 @@ public async Task SendAndWait_Throws_OperationCanceledException_When_Token_Cance
408408
{
409409
var session = await Client.CreateSessionAsync();
410410

411+
// Set up wait for tool execution to start BEFORE sending
412+
var toolStartTask = TestHelper.GetNextEventOfTypeAsync<ToolExecutionStartEvent>(session);
413+
411414
using var cts = new CancellationTokenSource();
412415

413-
// Cancel before any response can arrive
416+
// Start SendAndWaitAsync - don't await it yet
414417
var sendTask = session.SendAndWaitAsync(
415-
new MessageOptions { Prompt = "Run 'sleep 2 && echo done'" },
418+
new MessageOptions { Prompt = "run the shell command 'sleep 10' (note this works on both bash and PowerShell)" },
416419
cancellationToken: cts.Token);
417420

421+
// Wait for the tool to begin executing before cancelling
422+
await toolStartTask;
423+
424+
// Cancel the token
418425
cts.Cancel();
419426

420-
await Assert.ThrowsAsync<OperationCanceledException>(() => sendTask);
427+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sendTask);
421428
}
422429

423430
[Fact]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
models:
2+
- claude-sonnet-4.5
3+
conversations:
4+
- messages:
5+
- role: system
6+
content: ${system}
7+
- role: user
8+
content: run the shell command 'sleep 10' (note this works on both bash and PowerShell)
9+
- role: assistant
10+
content: I'll run the sleep command for 10 seconds.
11+
- role: assistant
12+
tool_calls:
13+
- id: toolcall_0
14+
type: function
15+
function:
16+
name: report_intent
17+
arguments: '{"intent":"Running sleep command"}'
18+
- role: assistant
19+
tool_calls:
20+
- id: toolcall_1
21+
type: function
22+
function:
23+
name: ${shell}
24+
arguments: '{"command":"sleep 10","description":"Run sleep 10 command","mode":"sync","initial_wait":15}'

0 commit comments

Comments
 (0)