|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +package com.github.copilot.sdk; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.net.Socket; |
| 14 | +import java.util.concurrent.CompletableFuture; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import com.github.copilot.sdk.events.AssistantMessageEvent; |
| 19 | +import com.github.copilot.sdk.json.MessageOptions; |
| 20 | + |
| 21 | +/** |
| 22 | + * Regression tests for timeout edge cases in |
| 23 | + * {@link CopilotSession#sendAndWait}. |
| 24 | + * <p> |
| 25 | + * These tests assert two behavioral contracts of the shared |
| 26 | + * {@code ScheduledExecutorService} approach: |
| 27 | + * <ol> |
| 28 | + * <li>A pending timeout must NOT fire after {@code close()} and must NOT |
| 29 | + * complete the returned future with a {@code TimeoutException}.</li> |
| 30 | + * <li>Multiple {@code sendAndWait} calls must reuse a single shared scheduler |
| 31 | + * thread rather than spawning a new OS thread per call.</li> |
| 32 | + * </ol> |
| 33 | + */ |
| 34 | +public class TimeoutEdgeCaseTest { |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a {@link JsonRpcClient} whose {@code invoke()} returns futures that |
| 38 | + * never complete. The reader thread blocks forever on the input stream, and |
| 39 | + * writes go to a no-op output stream. |
| 40 | + */ |
| 41 | + private JsonRpcClient createHangingRpcClient() throws Exception { |
| 42 | + InputStream blockingInput = new InputStream() { |
| 43 | + @Override |
| 44 | + public int read() throws IOException { |
| 45 | + try { |
| 46 | + Thread.sleep(Long.MAX_VALUE); |
| 47 | + } catch (InterruptedException e) { |
| 48 | + Thread.currentThread().interrupt(); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + return -1; |
| 52 | + } |
| 53 | + }; |
| 54 | + ByteArrayOutputStream sinkOutput = new ByteArrayOutputStream(); |
| 55 | + |
| 56 | + var ctor = JsonRpcClient.class.getDeclaredConstructor(InputStream.class, java.io.OutputStream.class, |
| 57 | + Socket.class, Process.class); |
| 58 | + ctor.setAccessible(true); |
| 59 | + return (JsonRpcClient) ctor.newInstance(blockingInput, sinkOutput, null, null); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * After {@code close()}, the future returned by {@code sendAndWait} must NOT be |
| 64 | + * completed by a stale timeout. |
| 65 | + * <p> |
| 66 | + * Contract: {@code close()} shuts down the timeout scheduler before the |
| 67 | + * blocking {@code session.destroy} RPC call, so any pending timeout task is |
| 68 | + * cancelled and the future remains incomplete (not exceptionally completed with |
| 69 | + * {@code TimeoutException}). |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testTimeoutDoesNotFireAfterSessionClose() throws Exception { |
| 73 | + JsonRpcClient rpc = createHangingRpcClient(); |
| 74 | + try { |
| 75 | + try (CopilotSession session = new CopilotSession("test-timeout-id", rpc)) { |
| 76 | + |
| 77 | + CompletableFuture<AssistantMessageEvent> result = session |
| 78 | + .sendAndWait(new MessageOptions().setPrompt("hello"), 2000); |
| 79 | + |
| 80 | + assertFalse(result.isDone(), "Future should be pending before timeout fires"); |
| 81 | + |
| 82 | + // close() blocks up to 5s on session.destroy RPC. The 2s timeout |
| 83 | + // fires during that window with the current per-call scheduler. |
| 84 | + session.close(); |
| 85 | + |
| 86 | + assertFalse(result.isDone(), "Future should not be completed by a timeout after session is closed. " |
| 87 | + + "The per-call ScheduledExecutorService leaked a TimeoutException."); |
| 88 | + } |
| 89 | + } finally { |
| 90 | + rpc.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * A shared scheduler must reuse a single thread across multiple |
| 96 | + * {@code sendAndWait} calls, rather than spawning a new OS thread per call. |
| 97 | + * <p> |
| 98 | + * Contract: after two consecutive {@code sendAndWait} calls the number of live |
| 99 | + * {@code sendAndWait-timeout} threads must not increase after the second call. |
| 100 | + */ |
| 101 | + @Test |
| 102 | + void testSendAndWaitReusesTimeoutThread() throws Exception { |
| 103 | + JsonRpcClient rpc = createHangingRpcClient(); |
| 104 | + try { |
| 105 | + try (CopilotSession session = new CopilotSession("test-thread-count-id", rpc)) { |
| 106 | + |
| 107 | + long baselineCount = countTimeoutThreads(); |
| 108 | + |
| 109 | + CompletableFuture<AssistantMessageEvent> result1 = session |
| 110 | + .sendAndWait(new MessageOptions().setPrompt("hello1"), 30000); |
| 111 | + |
| 112 | + Thread.sleep(100); |
| 113 | + long afterFirst = countTimeoutThreads(); |
| 114 | + assertTrue(afterFirst >= baselineCount + 1, |
| 115 | + "Expected at least one new sendAndWait-timeout thread after first call. " + "Baseline: " |
| 116 | + + baselineCount + ", after: " + afterFirst); |
| 117 | + |
| 118 | + CompletableFuture<AssistantMessageEvent> result2 = session |
| 119 | + .sendAndWait(new MessageOptions().setPrompt("hello2"), 30000); |
| 120 | + |
| 121 | + Thread.sleep(100); |
| 122 | + long afterSecond = countTimeoutThreads(); |
| 123 | + assertTrue(afterSecond == afterFirst, |
| 124 | + "Shared scheduler should reuse the same thread — no new threads after second call. " |
| 125 | + + "After first: " + afterFirst + ", after second: " + afterSecond); |
| 126 | + |
| 127 | + result1.cancel(true); |
| 128 | + result2.cancel(true); |
| 129 | + } |
| 130 | + } finally { |
| 131 | + rpc.close(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Counts the number of live threads whose name contains "sendAndWait-timeout". |
| 137 | + */ |
| 138 | + private long countTimeoutThreads() { |
| 139 | + return Thread.getAllStackTraces().keySet().stream().filter(t -> t.getName().contains("sendAndWait-timeout")) |
| 140 | + .filter(Thread::isAlive).count(); |
| 141 | + } |
| 142 | +} |
0 commit comments