use of com.google.bytestream.ByteStreamProto.QueryWriteStatusRequest in project bazel-buildfarm by bazelbuild.
the class StubInstanceTest method completedWriteBeforeCloseThrowsOnNextInteraction.
@Test
public void completedWriteBeforeCloseThrowsOnNextInteraction() throws IOException, InterruptedException {
String resourceName = "early-completed-output-stream-test";
serviceRegistry.addService(new ByteStreamImplBase() {
boolean completed = false;
int writtenBytes = 0;
@Override
public StreamObserver<WriteRequest> write(StreamObserver<WriteResponse> responseObserver) {
return new StreamObserver<WriteRequest>() {
@Override
public void onNext(WriteRequest request) {
if (!completed) {
writtenBytes = request.getData().size();
responseObserver.onNext(WriteResponse.newBuilder().setCommittedSize(writtenBytes).build());
responseObserver.onCompleted();
completed = true;
}
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onCompleted() {
}
};
}
@Override
public void queryWriteStatus(QueryWriteStatusRequest request, StreamObserver<QueryWriteStatusResponse> responseObserver) {
if (request.getResourceName().equals(resourceName)) {
responseObserver.onNext(QueryWriteStatusResponse.newBuilder().setCommittedSize(writtenBytes).setComplete(completed).build());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asException());
}
}
});
Instance instance = newStubInstance("early-completed-outputStream-test");
ByteString content = ByteString.copyFromUtf8("test-content");
boolean writeThrewException = false;
Write operationStreamWrite = instance.getOperationStreamWrite(resourceName);
try (OutputStream out = operationStreamWrite.getOutput(1, SECONDS, () -> {
})) {
content.writeTo(out);
try {
content.writeTo(out);
} catch (Exception e) {
writeThrewException = true;
}
}
assertThat(writeThrewException).isTrue();
instance.stop();
}
use of com.google.bytestream.ByteStreamProto.QueryWriteStatusRequest in project bazel-buildfarm by bazelbuild.
the class StubInstanceTest method outputStreamWrites.
@Test
public void outputStreamWrites() throws IOException, InterruptedException {
AtomicReference<ByteString> writtenContent = new AtomicReference<>();
serviceRegistry.addService(new ByteStreamImplBase() {
ByteString content = ByteString.EMPTY;
boolean finished = false;
public void queryWriteStatus(QueryWriteStatusRequest request, StreamObserver<QueryWriteStatusResponse> responseObserver) {
responseObserver.onNext(QueryWriteStatusResponse.newBuilder().setCommittedSize(content.size()).setComplete(finished).build());
responseObserver.onCompleted();
}
@Override
public StreamObserver<WriteRequest> write(StreamObserver<WriteResponse> responseObserver) {
return new StreamObserver<WriteRequest>() {
@Override
public void onNext(WriteRequest request) {
checkState(!finished);
if (request.getData().size() != 0) {
checkState(request.getWriteOffset() == content.size());
content = content.concat(request.getData());
}
finished = request.getFinishWrite();
if (finished) {
writtenContent.set(content);
responseObserver.onNext(WriteResponse.newBuilder().setCommittedSize(content.size()).build());
}
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
}
});
Instance instance = newStubInstance("outputStream-test");
String resourceName = "output-stream-test";
ByteString content = ByteString.copyFromUtf8("test-content");
Write operationStreamWrite = instance.getOperationStreamWrite(resourceName);
try (OutputStream out = operationStreamWrite.getOutput(1, SECONDS, () -> {
})) {
content.writeTo(out);
}
assertThat(writtenContent.get()).isEqualTo(content);
instance.stop();
}
use of com.google.bytestream.ByteStreamProto.QueryWriteStatusRequest in project bazel-buildfarm by bazelbuild.
the class StubWriteOutputStreamTest method resetExceptionsAreInterpreted.
@SuppressWarnings("unchecked")
@Test
public void resetExceptionsAreInterpreted() {
String unimplementedResourceName = "unimplemented-resource";
QueryWriteStatusRequest unimplementedRequest = QueryWriteStatusRequest.newBuilder().setResourceName(unimplementedResourceName).build();
doAnswer(invocation -> {
StreamObserver<QueryWriteStatusResponse> observer = invocation.getArgument(1);
observer.onError(Status.UNIMPLEMENTED.asException());
return null;
}).when(serviceImpl).queryWriteStatus(eq(unimplementedRequest), any(StreamObserver.class));
String notFoundResourceName = "not-found-resource";
QueryWriteStatusRequest notFoundRequest = QueryWriteStatusRequest.newBuilder().setResourceName(notFoundResourceName).build();
doAnswer(invocation -> {
StreamObserver<QueryWriteStatusResponse> observer = invocation.getArgument(1);
observer.onError(Status.NOT_FOUND.asException());
return null;
}).when(serviceImpl).queryWriteStatus(eq(notFoundRequest), any(StreamObserver.class));
StubWriteOutputStream write = new StubWriteOutputStream(Suppliers.ofInstance(ByteStreamGrpc.newBlockingStub(channel)), Suppliers.ofInstance(ByteStreamGrpc.newStub(channel)), unimplementedResourceName, Functions.identity(), /* expectedSize=*/
StubWriteOutputStream.UNLIMITED_EXPECTED_SIZE, /* autoflush=*/
true);
assertThat(write.getCommittedSize()).isEqualTo(0);
verify(serviceImpl, times(1)).queryWriteStatus(eq(unimplementedRequest), any(StreamObserver.class));
write = new StubWriteOutputStream(Suppliers.ofInstance(ByteStreamGrpc.newBlockingStub(channel)), Suppliers.ofInstance(ByteStreamGrpc.newStub(channel)), notFoundResourceName, Functions.identity(), /* expectedSize=*/
StubWriteOutputStream.UNLIMITED_EXPECTED_SIZE, /* autoflush=*/
true);
assertThat(write.getCommittedSize()).isEqualTo(0);
verify(serviceImpl, times(1)).queryWriteStatus(eq(notFoundRequest), any(StreamObserver.class));
}
Aggregations