forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroTimeoutContractTest.java
More file actions
57 lines (45 loc) · 2.4 KB
/
ZeroTimeoutContractTest.java
File metadata and controls
57 lines (45 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.generated.AssistantMessageEvent;
import com.github.copilot.sdk.json.MessageOptions;
/**
* Verifies the documented contract that {@code timeoutMs <= 0} means "no
* timeout" in {@link CopilotSession#sendAndWait(MessageOptions, long)}.
*/
public class ZeroTimeoutContractTest {
@SuppressWarnings("unchecked")
@Test
void sendAndWaitWithZeroTimeoutShouldNotTimeOut() throws Exception {
// Build a session via reflection (package-private constructor)
var ctor = CopilotSession.class.getDeclaredConstructor(String.class, JsonRpcClient.class, String.class);
ctor.setAccessible(true);
var mockRpc = mock(JsonRpcClient.class);
when(mockRpc.invoke(any(), any(), any())).thenAnswer(invocation -> {
Object method = invocation.getArgument(0);
if ("session.destroy".equals(method)) {
// Make session.close() non-blocking by completing destroy immediately
return CompletableFuture.completedFuture(null);
}
// For other calls (e.g., message send), return an incomplete future so the
// sendAndWait result does not complete due to a mock response.
return new CompletableFuture<>();
});
try (var session = ctor.newInstance("zero-timeout-test", mockRpc, null)) {
// Per the Javadoc: timeoutMs of 0 means "no timeout".
// The future should NOT complete with TimeoutException.
CompletableFuture<AssistantMessageEvent> result = session
.sendAndWait(new MessageOptions().setPrompt("test"), 0);
// Give the scheduler a chance to fire if it was (incorrectly) scheduled
Thread.sleep(200);
// The future should still be pending — not timed out
assertFalse(result.isDone(), "Future should not be done; timeoutMs=0 means no timeout per Javadoc");
}
}
}