Search in sources :

Example 6 with ByteStreamImplBase

use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.

the class ByteStreamUploaderTest method uploadBlobPropagatesIOExceptionFromIncompleteUpload.

@Test(expected = IOException.class)
public void uploadBlobPropagatesIOExceptionFromIncompleteUpload() throws IOException, InterruptedException {
    serviceRegistry.addService(new ByteStreamImplBase() {

        @Override
        public StreamObserver<WriteRequest> write(StreamObserver<WriteResponse> responseObserver) {
            responseObserver.onNext(WriteResponse.newBuilder().setCommittedSize(1).build());
            responseObserver.onCompleted();
            return new StreamObserver<WriteRequest>() {

                @Override
                public void onNext(WriteRequest request) {
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                }
            };
        }
    });
    ByteStreamUploader uploader = new ByteStreamUploader(/* instanceName=*/
    null, InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(), /* callCredentials=*/
    null, /* callTimeoutSecs=*/
    1, NO_RETRIES);
    Chunker chunker = Chunker.builder().setInput(ByteString.copyFromUtf8("Hello, World!")).build();
    uploader.uploadBlob(HashCode.fromInt(42), chunker);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) Test(org.junit.Test)

Example 7 with ByteStreamImplBase

use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.

the class StubInstanceTest method inputStreamThrowsOnDeadlineExceededWithoutProgress.

@Test
public void inputStreamThrowsOnDeadlineExceededWithoutProgress() throws IOException, InterruptedException {
    serviceRegistry.addService(new ByteStreamImplBase() {

        @Override
        public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) {
            responseObserver.onError(Status.DEADLINE_EXCEEDED.asException());
        }
    });
    OutputStream out = mock(OutputStream.class);
    IOException ioException = null;
    Instance instance = newStubInstance("input-stream-deadline-exceeded");
    Digest timeoutDigest = Digest.newBuilder().setHash("timeout-blob-name").setSizeBytes(1).build();
    try (InputStream in = instance.newBlobInput(timeoutDigest, 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.DEADLINE_EXCEEDED);
    verifyZeroInteractions(out);
    instance.stop();
}
Also used : Status(io.grpc.Status) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) Instance(build.buildfarm.instance.Instance) Digest(build.bazel.remote.execution.v2.Digest) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) IOException(java.io.IOException) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 8 with ByteStreamImplBase

use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase 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();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) Write(build.buildfarm.common.Write) Instance(build.buildfarm.instance.Instance) ByteString(com.google.protobuf.ByteString) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) QueryWriteStatusResponse(com.google.bytestream.ByteStreamProto.QueryWriteStatusResponse) QueryWriteStatusRequest(com.google.bytestream.ByteStreamProto.QueryWriteStatusRequest) Test(org.junit.Test)

Example 9 with ByteStreamImplBase

use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project bazel-buildfarm by bazelbuild.

the class GrpcCASTest method getHandlesNotFound.

@Test
public void getHandlesNotFound() {
    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());
        }
    });
    GrpcCAS cas = new GrpcCAS(instanceName, InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(), mock(ByteStreamUploader.class), onExpirations);
    assertThat(cas.get(digest)).isNull();
    assertThat(readCalled.get()).isTrue();
}
Also used : ByteStreamUploader(build.buildfarm.instance.stub.ByteStreamUploader) Digest(build.bazel.remote.execution.v2.Digest) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 10 with ByteStreamImplBase

use of com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase in project tools_remote by bazelbuild.

the class GrpcRemoteCacheTest method testDownloadBlobMultipleChunks.

@Test
public void testDownloadBlobMultipleChunks() 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("abc")).build());
            responseObserver.onNext(ReadResponse.newBuilder().setData(ByteString.copyFromUtf8("def")).build());
            responseObserver.onNext(ReadResponse.newBuilder().setData(ByteString.copyFromUtf8("g")).build());
            responseObserver.onCompleted();
        }
    });
    assertThat(new String(client.downloadBlob(digest), UTF_8)).isEqualTo("abcdefg");
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ReadResponse(com.google.bytestream.ByteStreamProto.ReadResponse) ByteStreamImplBase(com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Aggregations

ByteStreamImplBase (com.google.bytestream.ByteStreamGrpc.ByteStreamImplBase)10 Test (org.junit.Test)10 Digest (build.bazel.remote.execution.v2.Digest)7 ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)7 ReadResponse (com.google.bytestream.ByteStreamProto.ReadResponse)7 ByteString (com.google.protobuf.ByteString)7 Instance (build.buildfarm.instance.Instance)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)4 WriteRequest (com.google.bytestream.ByteStreamProto.WriteRequest)3 WriteResponse (com.google.bytestream.ByteStreamProto.WriteResponse)3 StreamObserver (io.grpc.stub.StreamObserver)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Write (build.buildfarm.common.Write)2 ByteStreamUploader (build.buildfarm.instance.stub.ByteStreamUploader)2 QueryWriteStatusRequest (com.google.bytestream.ByteStreamProto.QueryWriteStatusRequest)2 QueryWriteStatusResponse (com.google.bytestream.ByteStreamProto.QueryWriteStatusResponse)2 Status (io.grpc.Status)2