Skip to content

Commit

Permalink
Merge pull request #80 from mkouba/stdio-test-err-redirected
Browse files Browse the repository at this point in the history
stdio IT: assert that quarkus log is redirected to stderr by default
  • Loading branch information
mkouba authored Jan 21, 2025
2 parents 74bafba + 4fa3914 commit 7bfc57f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.quarkiverse.mcp.server.Resource;
import io.quarkiverse.mcp.server.TextContent;
import io.quarkiverse.mcp.server.Tool;
import io.quarkus.logging.Log;

public class ServerFeatures {

Expand All @@ -22,6 +23,7 @@ TextContent toLowerCase(String value) {

@Prompt(name = "code_assist")
PromptMessage codeAssist(@PromptArg(name = "lang") String language) {
Log.info("Log from code assist...");
return PromptMessage.withUserRole(new TextContent(codeService.assist(language)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.quarkiverse.mcp.server.stdio.it;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -10,7 +12,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand All @@ -34,7 +38,9 @@ public class ServerFeaturesIT {

static Process process;
static PrintStream out;
static BlockingQueue<JsonObject> synchronizer = new LinkedBlockingQueue<>();
static List<String> stderrLines;

static final BlockingQueue<JsonObject> synchronizer = new LinkedBlockingQueue<>();

@BeforeAll
static void startServer() throws IOException {
Expand All @@ -45,14 +51,16 @@ static void startServer() throws IOException {
ProcessBuilder builder = new ProcessBuilder("java", "-jar", quarkusRunJar.toString());
process = builder.start();
out = new PrintStream(process.getOutputStream(), true);
stderrLines = new CopyOnWriteArrayList<>();

Executors.newSingleThreadExecutor().submit(new Runnable() {

@Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
String line;
while ((line = reader.readLine()) != null) {
while ((line = stdout.readLine()) != null) {
System.out.println(String.format("JSON message received:\n%s", line));
synchronizer.put(new JsonObject(line));
}
Expand All @@ -61,13 +69,34 @@ public void run() {
}
}
});

Executors.newSingleThreadExecutor().submit(new Runnable() {

@Override
public void run() {
BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
try {
String line;
while ((line = stderr.readLine()) != null) {
System.out.println(line);
stderrLines.add(line);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});

}

@AfterAll
static void stopServer() {
if (process != null) {
process.destroy();
process = null;
}
out = null;
stderrLines = null;
}

@Order(1)
Expand All @@ -93,6 +122,10 @@ public void testPrompt() {
});
assertPromptMessage("System.out.println(\"Hello world!\");", "code_assist", new JsonObject()
.put("lang", "java"));

// Assert that quarkus log is redirected to stderr by default
assertFalse(stderrLines.isEmpty());
assertTrue(stderrLines.stream().anyMatch(l -> l.contains("Log from code assist...")));
}

@Order(2)
Expand Down

0 comments on commit 7bfc57f

Please sign in to comment.