use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project tools_remote by bazelbuild.
the class GrpcRemoteCacheTest method testDownloadBlobSingleChunk.
@Test
public void testDownloadBlobSingleChunk() throws Exception {
final GrpcRemoteCache client = newClient();
final Digest digest = DIGEST_UTIL.computeAsUtf8("abcdefg");
serviceRegistry.addService(new ByteStreamImplBase() {
@Override
public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
assertThat(request.getResourceName().contains(digest.getHash())).isTrue();
responseObserver.onNext(ReadResponse.newBuilder().setData(ByteString.copyFromUtf8("abcdefg")).build());
responseObserver.onCompleted();
}
});
assertThat(new String(client.downloadBlob(digest), UTF_8)).isEqualTo("abcdefg");
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.
the class GrpcCASTest method onExpirationCalledWhenNotFound.
@Test
public void onExpirationCalledWhenNotFound() {
Digest digest = DIGEST_UTIL.compute(ByteString.copyFromUtf8("nonexistent"));
String instanceName = "test";
final AtomicReference<Boolean> readCalled = new AtomicReference<>(false);
serviceRegistry.addService(new ByteStreamImplBase() {
@Override
public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
assertThat(request.getResourceName()).isEqualTo(String.format("%s/blobs/%s", instanceName, DigestUtil.toString(digest)));
readCalled.compareAndSet(false, true);
responseObserver.onError(Status.NOT_FOUND.asException());
}
});
Runnable onExpiration = mock(Runnable.class);
onExpirations.put(digest, onExpiration);
GrpcCAS cas = new GrpcCAS(instanceName, InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(), mock(ByteStreamUploader.class), onExpirations);
assertThat(cas.get(digest)).isNull();
assertThat(readCalled.get()).isTrue();
verify(onExpiration, times(1)).run();
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.
the class StubInstanceTest method inputStreamRetriesOnDeadlineExceededWithProgress.
@Test
public void inputStreamRetriesOnDeadlineExceededWithProgress() throws IOException, InterruptedException {
ByteString content = ByteString.copyFromUtf8("1");
serviceRegistry.addService(new ByteStreamImplBase() {
boolean first = true;
@Override
public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
if (first && request.getReadOffset() == 0) {
first = false;
responseObserver.onNext(ReadResponse.newBuilder().setData(content).build());
responseObserver.onError(Status.DEADLINE_EXCEEDED.asException());
} else if (request.getReadOffset() == 1) {
responseObserver.onCompleted();
} else {
// all others fail with unimplemented
responseObserver.onError(Status.UNIMPLEMENTED.asException());
}
}
});
Instance instance = newStubInstance("input-stream-stalled");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Digest delayedDigest = Digest.newBuilder().setHash("delayed-blob-name").setSizeBytes(1).build();
try (InputStream in = instance.newBlobInput(delayedDigest, 0, 1, SECONDS, RequestMetadata.getDefaultInstance())) {
ByteStreams.copy(in, out);
}
assertThat(ByteString.copyFrom(out.toByteArray())).isEqualTo(content);
instance.stop();
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase 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.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.
the class StubInstanceTest method inputStreamThrowsNonDeadlineExceededCausal.
@Test
public void inputStreamThrowsNonDeadlineExceededCausal() throws IOException, InterruptedException {
serviceRegistry.addService(new ByteStreamImplBase() {
@Override
public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
responseObserver.onError(Status.UNAVAILABLE.asException());
}
});
OutputStream out = mock(OutputStream.class);
IOException ioException = null;
Instance instance = newStubInstance("input-stream-non-deadline-exceeded");
Digest unavailableDigest = Digest.newBuilder().setHash("unavailable-blob-name").setSizeBytes(1).build();
try (InputStream in = instance.newBlobInput(unavailableDigest, 0, 1, SECONDS, RequestMetadata.getDefaultInstance())) {
ByteStreams.copy(in, out);
} catch (IOException e) {
ioException = e;
}
assertThat(ioException).isNotNull();
Status status = Status.fromThrowable(ioException);
assertThat(status.getCode()).isEqualTo(Code.UNAVAILABLE);
verifyZeroInteractions(out);
instance.stop();
}
Aggregations