Search in sources :

Example 1 with ByteStreamBlockingStub

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();
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) Channel(io.grpc.Channel) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with ByteStreamBlockingStub

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();
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) Channel(io.grpc.Channel) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) OutputStream(java.io.OutputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) ReadRequest(com.google.bytestream.ByteStreamProto.ReadRequest) Test(org.junit.Test)

Example 3 with ByteStreamBlockingStub

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()));
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) Channel(io.grpc.Channel) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) QueryWriteStatusResponse(com.google.bytestream.ByteStreamProto.QueryWriteStatusResponse) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 4 with ByteStreamBlockingStub

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());
}
Also used : Digest(build.bazel.remote.execution.v2.Digest) ByteString(com.google.protobuf.ByteString) WriteRequest(com.google.bytestream.ByteStreamProto.WriteRequest) InputStream(java.io.InputStream) Channel(io.grpc.Channel) WriteResponse(com.google.bytestream.ByteStreamProto.WriteResponse) Metadata(io.grpc.Metadata) ByteString(com.google.protobuf.ByteString) ClientCall(io.grpc.ClientCall) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) QueryWriteStatusResponse(com.google.bytestream.ByteStreamProto.QueryWriteStatusResponse) Test(org.junit.Test)

Example 5 with ByteStreamBlockingStub

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);
}
Also used : HashCode(com.google.common.hash.HashCode) ByteStreamStub(com.google.bytestream.ByteStreamGrpc.ByteStreamStub) ByteStreamBlockingStub(com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub) StubWriteOutputStream(build.buildfarm.common.grpc.StubWriteOutputStream) ByteString(com.google.protobuf.ByteString)

Aggregations

ByteStreamBlockingStub (com.google.bytestream.ByteStreamGrpc.ByteStreamBlockingStub)5 ByteString (com.google.protobuf.ByteString)5 Digest (build.bazel.remote.execution.v2.Digest)4 Channel (io.grpc.Channel)4 Test (org.junit.Test)4 QueryWriteStatusResponse (com.google.bytestream.ByteStreamProto.QueryWriteStatusResponse)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 StubWriteOutputStream (build.buildfarm.common.grpc.StubWriteOutputStream)1 ByteStreamStub (com.google.bytestream.ByteStreamGrpc.ByteStreamStub)1 ReadRequest (com.google.bytestream.ByteStreamProto.ReadRequest)1 WriteRequest (com.google.bytestream.ByteStreamProto.WriteRequest)1 WriteResponse (com.google.bytestream.ByteStreamProto.WriteResponse)1 HashCode (com.google.common.hash.HashCode)1 ClientCall (io.grpc.ClientCall)1 Metadata (io.grpc.Metadata)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1