use of com.google.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method readSlicesLargeChunksFromInstance.
@SuppressWarnings("unchecked")
@Test
public void readSlicesLargeChunksFromInstance() throws Exception {
// pick a large chunk size
long size = CHUNK_SIZE * 10 + CHUNK_SIZE - 47;
ByteString content;
try (ByteString.Output out = ByteString.newOutput(ByteStreamService.CHUNK_SIZE * 10 + ByteStreamService.CHUNK_SIZE - 47)) {
for (long i = 0; i < size; i++) {
out.write((int) (i & 0xff));
}
content = out.toByteString();
}
Digest digest = DIGEST_UTIL.compute(content);
String resourceName = "blobs/" + DigestUtil.toString(digest);
ReadRequest request = ReadRequest.newBuilder().setResourceName(resourceName).build();
doAnswer(answerVoid((blobDigest, offset, limit, chunkObserver, metadata) -> {
})).when(instance).getBlob(eq(digest), eq(request.getReadOffset()), eq((long) content.size()), any(ServerCallStreamObserver.class), eq(RequestMetadata.getDefaultInstance()));
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ByteStreamStub service = ByteStreamGrpc.newStub(channel);
CountingReadObserver readObserver = new CountingReadObserver();
service.read(request, readObserver);
ArgumentCaptor<ServerCallStreamObserver<ByteString>> observerCaptor = ArgumentCaptor.forClass(ServerCallStreamObserver.class);
verify(instance, times(1)).getBlob(eq(digest), eq(request.getReadOffset()), eq((long) content.size()), observerCaptor.capture(), eq(RequestMetadata.getDefaultInstance()));
StreamObserver<ByteString> responseObserver = observerCaptor.getValue();
// supply entire content
responseObserver.onNext(content);
responseObserver.onCompleted();
assertThat(readObserver.isCompleted()).isTrue();
assertThat(readObserver.getData()).isEqualTo(content);
List<Integer> sizes = readObserver.getSizesList();
// 10 + 1 incomplete chunk
assertThat(sizes.size()).isEqualTo(11);
assertThat(sizes.stream().filter((responseSize) -> responseSize > CHUNK_SIZE).collect(Collectors.toList())).isEmpty();
}
use of com.google.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method uploadsCanProgressAfterCancellation.
@Test
public void uploadsCanProgressAfterCancellation() throws Exception {
ByteString content = ByteString.copyFromUtf8("Hello, World!");
Digest digest = DIGEST_UTIL.compute(content);
UUID uuid = UUID.randomUUID();
SettableFuture<Long> writtenFuture = SettableFuture.create();
ByteString.Output output = ByteString.newOutput((int) digest.getSizeBytes());
FeedbackOutputStream out = new FeedbackOutputStream() {
@Override
public void close() {
if (output.size() == digest.getSizeBytes()) {
writtenFuture.set(digest.getSizeBytes());
}
}
@Override
public void write(byte[] b, int off, int len) {
output.write(b, off, len);
}
@Override
public void write(int b) {
output.write(b);
}
@Override
public boolean isReady() {
return true;
}
};
Write write = mock(Write.class);
when(write.getOutput(any(Long.class), any(TimeUnit.class), any(Runnable.class))).thenReturn(out);
doAnswer(invocation -> (long) output.size()).when(write).getCommittedSize();
when(write.getFuture()).thenReturn(writtenFuture);
when(instance.getBlobWrite(digest, uuid, RequestMetadata.getDefaultInstance())).thenReturn(write);
HashCode hash = HashCode.fromString(digest.getHash());
String resourceName = ByteStreamUploader.uploadResourceName(/* instanceName=*/
null, uuid, hash, digest.getSizeBytes());
Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
ByteStreamStub service = ByteStreamGrpc.newStub(channel);
FutureWriteResponseObserver futureResponder = new FutureWriteResponseObserver();
StreamObserver<WriteRequest> requestObserver = service.write(futureResponder);
ByteString shortContent = content.substring(0, 6);
requestObserver.onNext(WriteRequest.newBuilder().setWriteOffset(0).setResourceName(resourceName).setData(shortContent).build());
requestObserver.onError(Status.CANCELLED.asException());
// should be done
assertThat(futureResponder.isDone()).isTrue();
futureResponder = new FutureWriteResponseObserver();
requestObserver = service.write(futureResponder);
requestObserver.onNext(WriteRequest.newBuilder().setWriteOffset(6).setResourceName(resourceName).setData(content.substring(6)).setFinishWrite(true).build());
assertThat(futureResponder.get()).isEqualTo(WriteResponse.newBuilder().setCommittedSize(content.size()).build());
requestObserver.onCompleted();
verify(write, atLeastOnce()).getCommittedSize();
verify(write, atLeastOnce()).getOutput(any(Long.class), any(TimeUnit.class), any(Runnable.class));
verify(write, times(2)).getFuture();
}
use of com.google.cloud.kms.v1.Digest 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.cloud.kms.v1.Digest 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.cloud.kms.v1.Digest in project bazel-buildfarm by bazelbuild.
the class ByteStreamServiceTest method writePutsIntoBlobStore.
@Test
public void writePutsIntoBlobStore() 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> call = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
ClientCall.Listener<WriteResponse> callListener = new ClientCall.Listener<WriteResponse>() {
boolean complete = false;
boolean callHalfClosed = false;
@Override
public void onReady() {
while (call.isReady()) {
if (complete) {
if (!callHalfClosed) {
call.halfClose();
callHalfClosed = true;
}
return;
}
call.sendMessage(WriteRequest.newBuilder().setResourceName(resourceName).setData(helloWorld).setFinishWrite(true).build());
complete = true;
}
}
};
call.start(callListener, new Metadata());
call.request(1);
verify(simpleBlobStore, times(1)).put(eq(digest.getHash()), eq(digest.getSizeBytes()), any(InputStream.class));
}
Aggregations