use of com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method missingWriteQueryIsNotFound.
@Test
public void missingWriteQueryIsNotFound() throws IOException {
ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
Digest digest = DIGEST_UTIL.compute(helloWorld);
String uuid = UUID.randomUUID().toString();
String resourceName = createBlobUploadResourceName(uuid, digest);
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
StatusRuntimeException notFoundException = null;
try {
service.queryWriteStatus(QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build());
} catch (StatusRuntimeException e) {
assertThat(Status.fromThrowable(e).getCode()).isEqualTo(Code.NOT_FOUND);
notFoundException = e;
}
assertThat(notFoundException).isNotNull();
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method missingBlobReadIsNotFound.
@Test
public void missingBlobReadIsNotFound() {
ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
Digest digest = DIGEST_UTIL.compute(helloWorld);
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
when(simpleBlobStore.get(eq(digest.getHash()), any(OutputStream.class))).thenReturn(immediateFuture(false));
ReadRequest request = ReadRequest.newBuilder().setResourceName(createBlobDownloadResourceName(digest)).build();
StatusRuntimeException notFoundException = null;
try {
if (service.read(request).hasNext()) {
fail("no responses should be available");
}
} catch (StatusRuntimeException e) {
assertThat(Status.fromThrowable(e).getCode()).isEqualTo(Code.NOT_FOUND);
notFoundException = e;
}
assertThat(notFoundException).isNotNull();
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method completedWriteQueryIsFound.
@Test
public void completedWriteQueryIsFound() throws IOException, InterruptedException {
ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
Digest digest = DIGEST_UTIL.compute(helloWorld);
String uuid = UUID.randomUUID().toString();
String resourceName = createBlobUploadResourceName(uuid, digest);
when(simpleBlobStore.containsKey(digest.getHash())).thenReturn(true);
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
QueryWriteStatusResponse response = service.queryWriteStatus(QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build());
assertThat(response).isEqualTo(QueryWriteStatusResponse.newBuilder().setCommittedSize(digest.getSizeBytes()).setComplete(true).build());
verify(simpleBlobStore, times(1)).containsKey(eq(digest.getHash()));
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method writeCanBeResumed.
@Test
public void writeCanBeResumed() throws IOException, InterruptedException {
ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
Digest digest = DIGEST_UTIL.compute(helloWorld);
String uuid = UUID.randomUUID().toString();
String resourceName = createBlobUploadResourceName(uuid, digest);
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ClientCall<WriteRequest, WriteResponse> initialCall = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
ByteString initialData = helloWorld.substring(0, 6);
ClientCall.Listener<WriteResponse> initialCallListener = new ClientCall.Listener<WriteResponse>() {
boolean complete = false;
boolean callHalfClosed = false;
@Override
public void onReady() {
while (initialCall.isReady()) {
if (complete) {
if (!callHalfClosed) {
initialCall.halfClose();
callHalfClosed = true;
}
return;
}
initialCall.sendMessage(WriteRequest.newBuilder().setResourceName(resourceName).setData(initialData).build());
complete = true;
}
}
};
initialCall.start(initialCallListener, new Metadata());
initialCall.request(1);
ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
QueryWriteStatusResponse response = service.queryWriteStatus(QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build());
assertThat(response.getCommittedSize()).isEqualTo(initialData.size());
assertThat(response.getComplete()).isFalse();
ClientCall<WriteRequest, WriteResponse> finishCall = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
ClientCall.Listener<WriteResponse> finishCallListener = new ClientCall.Listener<WriteResponse>() {
boolean complete = false;
boolean callHalfClosed = false;
@Override
public void onReady() {
while (finishCall.isReady()) {
if (complete) {
if (!callHalfClosed) {
finishCall.halfClose();
callHalfClosed = true;
}
return;
}
finishCall.sendMessage(WriteRequest.newBuilder().setResourceName(resourceName).setWriteOffset(initialData.size()).setData(helloWorld.substring(initialData.size())).setFinishWrite(true).build());
complete = true;
}
}
};
finishCall.start(finishCallListener, new Metadata());
finishCall.request(1);
ArgumentCaptor<InputStream> inputStreamCaptor = ArgumentCaptor.forClass(InputStream.class);
verify(simpleBlobStore, times(1)).put(eq(digest.getHash()), eq(digest.getSizeBytes()), inputStreamCaptor.capture());
InputStream inputStream = inputStreamCaptor.getValue();
assertThat(inputStream.available()).isEqualTo(helloWorld.size());
byte[] data = new byte[helloWorld.size()];
assertThat(inputStream.read(data)).isEqualTo(helloWorld.size());
assertThat(data).isEqualTo(helloWorld.toByteArray());
}
use of com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub in project bazel-buildfarm by bazelbuild.
the class GrpcCAS method newWrite.
@SuppressWarnings("Guava")
public static Write newWrite(Channel channel, String instanceName, Digest digest, UUID uuid, RequestMetadata requestMetadata) {
HashCode hash = HashCode.fromString(digest.getHash());
String resourceName = ByteStreamUploader.uploadResourceName(instanceName, uuid, hash, digest.getSizeBytes());
Supplier<ByteStreamBlockingStub> bsBlockingStub = Suppliers.memoize(() -> ByteStreamGrpc.newBlockingStub(channel).withInterceptors(attachMetadataInterceptor(requestMetadata)));
Supplier<ByteStreamStub> bsStub = Suppliers.memoize(() -> ByteStreamGrpc.newStub(channel).withInterceptors(attachMetadataInterceptor(requestMetadata)));
return new StubWriteOutputStream(bsBlockingStub, bsStub, resourceName, Functions.identity(), digest.getSizeBytes(), /* autoflush=*/
false);
}
Aggregations