Skip to content

Commit d453dce

Browse files
edburnsCopilot
andcommitted
Test: replace external blocking processes
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d86a54a commit d453dce

3 files changed

Lines changed: 69 additions & 16 deletions

File tree

java/sdk/src/test/java/com/github/copilot/CliServerManagerTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,13 @@ void connectToServerTcpMode() throws Exception {
7070
}
7171
}
7272

73-
private static Process startBlockingProcess() throws IOException {
74-
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
75-
return (isWindows ? new ProcessBuilder(System.getenv("COMSPEC"), "/c", "more") : new ProcessBuilder("cat"))
76-
.start();
77-
}
78-
7973
@Test
8074
void connectToServerStdioMode() throws Exception {
8175
var options = new CopilotClientOptions();
8276
var manager = new CliServerManager(options);
8377

8478
// Create a dummy process for stdio mode
85-
Process process = startBlockingProcess();
79+
Process process = new TestProcess();
8680
try {
8781
JsonRpcClient client = manager.connectToServer(process, null, null);
8882
assertNotNull(client);

java/sdk/src/test/java/com/github/copilot/JsonRpcClientTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,17 @@ void testIsConnectedWithSocketClosed() throws Exception {
133133
pair.serverSocket.close();
134134
}
135135

136-
private static Process startBlockingProcess() throws IOException {
137-
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
138-
return (isWindows ? new ProcessBuilder(System.getenv("COMSPEC"), "/c", "more") : new ProcessBuilder("cat"))
139-
.start();
140-
}
141-
142136
@Test
143137
void testIsConnectedWithProcess() throws Exception {
144-
Process proc = startBlockingProcess();
138+
Process proc = new TestProcess();
145139
try (var client = JsonRpcClient.fromProcess(proc)) {
146140
assertTrue(client.isConnected());
147141
}
148142
}
149143

150144
@Test
151145
void testIsConnectedWithProcessDead() throws Exception {
152-
Process proc = startBlockingProcess();
146+
Process proc = new TestProcess();
153147
var client = JsonRpcClient.fromProcess(proc);
154148
proc.destroy();
155149
proc.waitFor(5, TimeUnit.SECONDS);
@@ -161,7 +155,7 @@ void testIsConnectedWithProcessDead() throws Exception {
161155

162156
@Test
163157
void testGetProcessReturnsProcess() throws Exception {
164-
Process proc = startBlockingProcess();
158+
Process proc = new TestProcess();
165159
try (var client = JsonRpcClient.fromProcess(proc)) {
166160
assertSame(proc, client.getProcess());
167161
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
package com.github.copilot;
6+
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.util.concurrent.CountDownLatch;
10+
import java.util.concurrent.TimeUnit;
11+
12+
final class TestProcess extends Process {
13+
14+
private final CountDownLatch terminated = new CountDownLatch(1);
15+
16+
@Override
17+
public OutputStream getOutputStream() {
18+
return OutputStream.nullOutputStream();
19+
}
20+
21+
@Override
22+
public InputStream getInputStream() {
23+
return InputStream.nullInputStream();
24+
}
25+
26+
@Override
27+
public InputStream getErrorStream() {
28+
return InputStream.nullInputStream();
29+
}
30+
31+
@Override
32+
public int waitFor() throws InterruptedException {
33+
terminated.await();
34+
return 0;
35+
}
36+
37+
@Override
38+
public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException {
39+
return terminated.await(timeout, unit);
40+
}
41+
42+
@Override
43+
public int exitValue() {
44+
if (isAlive()) {
45+
throw new IllegalThreadStateException("Process has not exited");
46+
}
47+
return 0;
48+
}
49+
50+
@Override
51+
public void destroy() {
52+
terminated.countDown();
53+
}
54+
55+
@Override
56+
public Process destroyForcibly() {
57+
destroy();
58+
return this;
59+
}
60+
61+
@Override
62+
public boolean isAlive() {
63+
return terminated.getCount() > 0;
64+
}
65+
}

0 commit comments

Comments
 (0)