Skip to content

Commit 8ab87d2

Browse files
author
Mark Pollack
committed
PromptCancellationSignalTest waits for the agent's cancel handler
The SDK answers a cancelled prompt and runs the agent's session/cancel handler independently; with the in-memory delivery thread the answer can reach the client first. With a 200 ms delay in the handler the test failed every time; it now waits for the count.
1 parent cdbb3fc commit 8ab87d2

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎acp-core/src/test/java/com/agentclientprotocol/sdk/client/PromptCancellationSignalTest.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void asyncPromptStopsWhenTheSignalIsCancelled() throws Exception {
7979

8080
assertThat(answer.get(5, TimeUnit.SECONDS).stopReason()).isEqualTo(AcpSchema.StopReason.CANCELLED);
8181
assertThat(stop.isCancelled()).isTrue();
82-
assertThat(cancels.get()).isEqualTo(1);
82+
awaitCancels(1);
8383
}
8484
finally {
8585
client.close();
@@ -143,4 +143,18 @@ void aSignalNotCancelledSendsNoCancel() {
143143
}
144144
}
145145

146+
147+
/**
148+
* Waits until the agent's cancel handler has counted {@code count} cancels, then checks it. The
149+
* SDK answers the cancelled prompt and runs the agent's {@code session/cancel} handler
150+
* independently, so the answer can reach the client first.
151+
*/
152+
private void awaitCancels(int count) throws InterruptedException {
153+
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
154+
while (cancels.get() < count && System.nanoTime() < deadline) {
155+
Thread.sleep(5);
156+
}
157+
assertThat(cancels.get()).isEqualTo(count);
158+
}
159+
146160
}

0 commit comments

Comments
 (0)