Java11Exec: Fix that EOF is replaced by newline.

This commit is contained in:
Maximilian Paß
2022-04-23 21:26:37 +02:00
parent bb2d989bc5
commit a545614040
2 changed files with 44 additions and 11 deletions

View File

@ -11,16 +11,12 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayV2WebSocketEvent;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
// AwsFunctionRequest contains the java files that needs to be executed.
class AwsFunctionRequest {
@ -147,16 +143,29 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
output.join();
error.join();
this.sendMessage(WebSocketMessageType.WebSocketExit, null, p.exitValue());
this.sendMessage(WebSocketMessageType.WebSocketExit, null, p.waitFor());
}
// scanForOutput reads the passed stream and forwards it via the WebSocket connection.
private void scanForOutput(Process p, InputStream stream, WebSocketMessageType type) {
Scanner outputScanner = new Scanner(stream);
while (p.isAlive() || outputScanner.hasNextLine()) {
try {
this.sendMessage(type, outputScanner.nextLine() + "\n", null);
} catch (NoSuchElementException ignored) {}
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuilder s = new StringBuilder();
int nextByte;
try {
while ((nextByte = br.read()) != -1) {
char c = (char) nextByte;
s.append(c);
if (c == '\n') {
this.sendMessage(type, s.toString(), null);
s = new StringBuilder();
}
}
} catch (IOException ignored) {}
if (!s.toString().isEmpty()) {
this.sendMessage(type, s.toString(), null);
}
}

View File

@ -41,6 +41,16 @@ public class AppTest {
" }\n" +
"}").getBytes(StandardCharsets.UTF_8));
static final String MathContentWithoutTrailingNewline = Base64.getEncoder().encodeToString(
("package org.example;\n" +
"\n" +
"public class RecursiveMath {\n" +
"\n" +
" public static void main(String[] args) {\n" +
" System.out.print(\"Mein Text\");\n" +
" }\n" +
"}").getBytes(StandardCharsets.UTF_8));
@Test
public void successfulResponse() {
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(RecursiveMathContent);
@ -62,6 +72,20 @@ public class AppTest {
assertEquals(expectedOutput, out.toString());
}
@Test
public void outputWithoutTrailingNewline() {
ByteArrayOutputStream out = setupStdOutLogs();
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(MathContentWithoutTrailingNewline);
restoreStdOutLogs();
assertEquals(200, result.getStatusCode().intValue());
String expectedOutput =
"{\"type\":\"stdout\",\"data\":\"Mein Text\"}\n" +
"{\"type\":\"exit\",\"data\":0}\n";
assertEquals(expectedOutput, out.toString());
}
private PrintStream originalOut;
private ByteArrayOutputStream setupStdOutLogs() {
originalOut = System.out;