Search in sources :

Example 1 with CHUNK_SIZE

use of build.buildfarm.server.ByteStreamService.CHUNK_SIZE 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();
}
Also used : SHA256(build.buildfarm.common.DigestUtil.HashFunction.SHA256) RequestMetadata(build.bazel.remote.execution.v2.RequestMetadata) SettableFuture(com.google.common.util.concurrent.SettableFuture) Channel(io.grpc.Channel) ByteStreamGrpc(com.google.bytestream.ByteStreamGrpc) MockitoAnnotations(org.mockito.MockitoAnnotations) StreamObserver(io.grpc.stub.StreamObserver) Digest(build.bazel.remote.execution.v2.Digest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) After(org.junit.After) AdditionalAnswers.answerVoid(org.mockito.AdditionalAnswers.answerVoid) Status(io.grpc.Status) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Instance(build.buildfarm.instance.Instance) ByteString(com.google.protobuf.ByteString) ByteStreamUploader(build.buildfarm.instance.stub.ByteStreamUploader) List(java.util.List) CHUNK_SIZE(build.buildfarm.server.ByteStreamService.CHUNK_SIZE) Mockito.any(org.mockito.Mockito.any) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) DigestUtil(build.buildfarm.common.DigestUtil) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) FeedbackOutputStream(build.buildfarm.common.io.FeedbackOutputStream) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) Server(io.grpc.Server) Before(org.junit.Before) HashCode(com.google.common.hash.HashCode) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Write(build.buildfarm.common.Write) Mockito.verify(org.mockito.Mockito.verify) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) TimeUnit(java.util.concurrent.TimeUnit) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) Channel(io.grpc.Channel) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Aggregations

Digest (build.bazel.remote.execution.v2.Digest)1 RequestMetadata (build.bazel.remote.execution.v2.RequestMetadata)1 DigestUtil (build.buildfarm.common.DigestUtil)1 SHA256 (build.buildfarm.common.DigestUtil.HashFunction.SHA256)1 Write (build.buildfarm.common.Write)1 FeedbackOutputStream (build.buildfarm.common.io.FeedbackOutputStream)1 Instance (build.buildfarm.instance.Instance)1 ByteStreamUploader (build.buildfarm.instance.stub.ByteStreamUploader)1 CHUNK_SIZE (build.buildfarm.server.ByteStreamService.CHUNK_SIZE)1 ByteStreamGrpc (com.google.bytestream.ByteStreamGrpc)1 ByteStreamStub (com.google.bytestream.ByteStreamGrpc.ByteStreamStub)1 ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)1 ReadResponse (com.google.bytestream.ByteStreamProto.ReadResponse)1 WriteRequest (com.google.bytestream.ByteStreamProto.WriteRequest)1 WriteResponse (com.google.bytestream.ByteStreamProto.WriteResponse)1 Lists (com.google.common.collect.Lists)1 HashCode (com.google.common.hash.HashCode)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1