Skip to content

Commit

Permalink
Format work requests according to ndjson spec
Browse files Browse the repository at this point in the history
JSON persistent workers now delimit requests with newlines (`\n` on Unix, `\r\n` on Windows).

Fixes bazelbuild#14218.

Closes bazelbuild#14219.

PiperOrigin-RevId: 411596359
  • Loading branch information
wolfd authored and Copybara-Service committed Nov 22, 2021
1 parent ab632b4 commit 227e49e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Expand Up @@ -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();
}

Expand Down
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit 227e49e

Please sign in to comment.