Java11Exec: Fix that EOF is replaced by newline.
This commit is contained in:
@@ -11,16 +11,12 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayV2WebSocketEvent;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
// AwsFunctionRequest contains the java files that needs to be executed.
|
// AwsFunctionRequest contains the java files that needs to be executed.
|
||||||
class AwsFunctionRequest {
|
class AwsFunctionRequest {
|
||||||
@@ -147,16 +143,29 @@ public class App implements RequestHandler<APIGatewayV2WebSocketEvent, APIGatewa
|
|||||||
|
|
||||||
output.join();
|
output.join();
|
||||||
error.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.
|
// scanForOutput reads the passed stream and forwards it via the WebSocket connection.
|
||||||
private void scanForOutput(Process p, InputStream stream, WebSocketMessageType type) {
|
private void scanForOutput(Process p, InputStream stream, WebSocketMessageType type) {
|
||||||
Scanner outputScanner = new Scanner(stream);
|
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
|
||||||
while (p.isAlive() || outputScanner.hasNextLine()) {
|
StringBuilder s = new StringBuilder();
|
||||||
try {
|
int nextByte;
|
||||||
this.sendMessage(type, outputScanner.nextLine() + "\n", null);
|
|
||||||
} catch (NoSuchElementException ignored) {}
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,6 +41,16 @@ public class AppTest {
|
|||||||
" }\n" +
|
" }\n" +
|
||||||
"}").getBytes(StandardCharsets.UTF_8));
|
"}").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
|
@Test
|
||||||
public void successfulResponse() {
|
public void successfulResponse() {
|
||||||
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(RecursiveMathContent);
|
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse(RecursiveMathContent);
|
||||||
@@ -62,6 +72,20 @@ public class AppTest {
|
|||||||
assertEquals(expectedOutput, out.toString());
|
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 PrintStream originalOut;
|
||||||
private ByteArrayOutputStream setupStdOutLogs() {
|
private ByteArrayOutputStream setupStdOutLogs() {
|
||||||
originalOut = System.out;
|
originalOut = System.out;
|
||||||
|
Reference in New Issue
Block a user