From 299e50aae9d8c0b7f0d47aa2ce3d2658a3a80a94 Mon Sep 17 00:00:00 2001 From: Danny Wolf Date: Wed, 24 Nov 2021 02:00:41 -0800 Subject: [PATCH] Format work requests according to ndjson spec (#14314) JSON persistent workers now delimit requests with newlines (`\n` on Unix, `\r\n` on Windows). Fixes #14218. Closes #14219. PiperOrigin-RevId: 411596359 --- .../devtools/build/lib/worker/JsonWorkerProtocol.java | 3 +++ .../google/devtools/build/lib/worker/WorkerTest.java | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/worker/JsonWorkerProtocol.java b/src/main/java/com/google/devtools/build/lib/worker/JsonWorkerProtocol.java index fc1effc6c3d040..2f891071545441 100644 --- a/src/main/java/com/google/devtools/build/lib/worker/JsonWorkerProtocol.java +++ b/src/main/java/com/google/devtools/build/lib/worker/JsonWorkerProtocol.java @@ -48,7 +48,10 @@ final class JsonWorkerProtocol implements WorkerProtocolImpl { @Override public void putRequest(WorkRequest request) throws IOException { + // WorkRequests are serialized according to ndjson spec. + // https://github.com/ndjson/ndjson-spec jsonPrinter.appendTo(request, jsonWriter); + jsonWriter.append(System.lineSeparator()); jsonWriter.flush(); } diff --git a/src/test/java/com/google/devtools/build/lib/worker/WorkerTest.java b/src/test/java/com/google/devtools/build/lib/worker/WorkerTest.java index ae0badbd872fa7..2a4f7d9380f80d 100644 --- a/src/test/java/com/google/devtools/build/lib/worker/WorkerTest.java +++ b/src/test/java/com/google/devtools/build/lib/worker/WorkerTest.java @@ -118,12 +118,12 @@ public void testPutRequest_json_success() throws IOException, InterruptedExcepti testWorker.putRequest(WorkRequest.getDefaultInstance()); OutputStream stdout = testWorker.getFakeSubprocess().getOutputStream(); - assertThat(stdout.toString()).isEqualTo("{}"); + assertThat(stdout.toString()).isEqualTo("{}" + System.lineSeparator()); } @Test public void testGetResponse_json_success() throws IOException, InterruptedException { - TestWorker testWorker = createTestWorker("{}".getBytes(UTF_8), JSON); + TestWorker testWorker = createTestWorker(("{}" + System.lineSeparator()).getBytes(UTF_8), JSON); WorkResponse readResponse = testWorker.getResponse(0); WorkResponse response = WorkResponse.getDefaultInstance(); @@ -151,7 +151,8 @@ public void testPutRequest_json_populatedFields_success() OutputStream stdout = testWorker.getFakeSubprocess().getOutputStream(); String requestJsonString = "{\"arguments\":[\"testRequest\"],\"inputs\":" - + "[{\"path\":\"testPath\",\"digest\":\"dGVzdERpZ2VzdA==\"}],\"requestId\":1,\"verbosity\":11}"; + + "[{\"path\":\"testPath\",\"digest\":\"dGVzdERpZ2VzdA==\"}],\"requestId\":1,\"verbosity\":11}" + + System.lineSeparator(); assertThat(stdout.toString()).isEqualTo(requestJsonString); } @@ -170,7 +171,8 @@ public void testGetResponse_json_populatedFields_success() private void verifyGetResponseFailure(String responseString, String expectedError) throws IOException { - TestWorker testWorker = createTestWorker(responseString.getBytes(UTF_8), JSON); + TestWorker testWorker = + createTestWorker((responseString + System.lineSeparator()).getBytes(UTF_8), JSON); IOException ex = assertThrows(IOException.class, () -> testWorker.getResponse(0)); assertThat(ex).hasMessageThat().contains(expectedError); }