Search in sources :

Example 1 with ReadRequest

use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.

the class DatabaseClientImplTest method testReadWriteExecuteReadWithTag.

@Test
public void testReadWriteExecuteReadWithTag() {
    DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
    TransactionRunner runner = client.readWriteTransaction(Options.tag("app=spanner,env=test,action=txn"));
    runner.run(transaction -> {
        try (ResultSet resultSet = transaction.read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.tag("app=spanner,env=test,action=read"))) {
            while (resultSet.next()) {
            }
        }
        return null;
    });
    List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
    assertThat(requests).hasSize(1);
    ReadRequest request = requests.get(0);
    assertNotNull(request.getRequestOptions());
    assertThat(request.getRequestOptions().getRequestTag()).isEqualTo("app=spanner,env=test,action=read");
    assertThat(request.getRequestOptions().getTransactionTag()).isEqualTo("app=spanner,env=test,action=txn");
}
Also used : ReadRequest(com.google.spanner.v1.ReadRequest) Test(org.junit.Test)

Example 2 with ReadRequest

use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.

the class DatabaseClientImplTest method testExecuteReadWithTag.

@Test
public void testExecuteReadWithTag() {
    DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
    try (ResultSet resultSet = client.singleUse().read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.tag("app=spanner,env=test,action=read"))) {
        while (resultSet.next()) {
        }
    }
    List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
    assertThat(requests).hasSize(1);
    ReadRequest request = requests.get(0);
    assertNotNull(request.getRequestOptions());
    assertThat(request.getRequestOptions().getRequestTag()).isEqualTo("app=spanner,env=test,action=read");
    assertThat(request.getRequestOptions().getTransactionTag()).isEmpty();
}
Also used : ReadRequest(com.google.spanner.v1.ReadRequest) Test(org.junit.Test)

Example 3 with ReadRequest

use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.

the class SpannerClientTest method readTest.

@Test
public void readTest() throws Exception {
    ResultSet expectedResponse = ResultSet.newBuilder().setMetadata(ResultSetMetadata.newBuilder().build()).addAllRows(new ArrayList<ListValue>()).setStats(ResultSetStats.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
    ResultSet actualResponse = client.read(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ReadRequest actualRequest = ((ReadRequest) actualRequests.get(0));
    Assert.assertEquals(request.getSession(), actualRequest.getSession());
    Assert.assertEquals(request.getTransaction(), actualRequest.getTransaction());
    Assert.assertEquals(request.getTable(), actualRequest.getTable());
    Assert.assertEquals(request.getIndex(), actualRequest.getIndex());
    Assert.assertEquals(request.getColumnsList(), actualRequest.getColumnsList());
    Assert.assertEquals(request.getKeySet(), actualRequest.getKeySet());
    Assert.assertEquals(request.getLimit(), actualRequest.getLimit());
    Assert.assertEquals(request.getResumeToken(), actualRequest.getResumeToken());
    Assert.assertEquals(request.getPartitionToken(), actualRequest.getPartitionToken());
    Assert.assertEquals(request.getRequestOptions(), actualRequest.getRequestOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListValue(com.google.protobuf.ListValue) PartialResultSet(com.google.spanner.v1.PartialResultSet) ResultSet(com.google.spanner.v1.ResultSet) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.spanner.v1.ReadRequest) PartitionReadRequest(com.google.spanner.v1.PartitionReadRequest) Test(org.junit.Test)

Example 4 with ReadRequest

use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.

the class SpannerClientTest method readExceptionTest.

@Test
public void readExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSpanner.addException(exception);
    try {
        ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
        client.read(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ReadRequest(com.google.spanner.v1.ReadRequest) PartitionReadRequest(com.google.spanner.v1.PartitionReadRequest) Test(org.junit.Test)

Example 5 with ReadRequest

use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.

the class SpannerClientTest method streamingReadTest.

@Test
public void streamingReadTest() throws Exception {
    PartialResultSet expectedResponse = PartialResultSet.newBuilder().setMetadata(ResultSetMetadata.newBuilder().build()).addAllValues(new ArrayList<Value>()).setChunkedValue(true).setResumeToken(ByteString.EMPTY).setStats(ResultSetStats.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
    MockStreamObserver<PartialResultSet> responseObserver = new MockStreamObserver<>();
    ServerStreamingCallable<ReadRequest, PartialResultSet> callable = client.streamingReadCallable();
    callable.serverStreamingCall(request, responseObserver);
    List<PartialResultSet> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) ByteString(com.google.protobuf.ByteString) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) PartialResultSet(com.google.spanner.v1.PartialResultSet) ReadRequest(com.google.spanner.v1.ReadRequest) PartitionReadRequest(com.google.spanner.v1.PartitionReadRequest) Test(org.junit.Test)

Aggregations

ReadRequest (com.google.spanner.v1.ReadRequest)11 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)6 PartialResultSet (com.google.spanner.v1.PartialResultSet)6 Session (com.google.spanner.v1.Session)5 PartitionReadRequest (com.google.spanner.v1.PartitionReadRequest)4 ResultSet (com.google.spanner.v1.ResultSet)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 GcpManagedChannel (com.google.grpc.gcp.GcpManagedChannel)2 ListValue (com.google.protobuf.ListValue)2 ManagedChannel (io.grpc.ManagedChannel)2 SpannerClient (com.google.cloud.spanner.v1.SpannerClient)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 Value (com.google.protobuf.Value)1 SpannerBlockingStub (com.google.spanner.v1.SpannerGrpc.SpannerBlockingStub)1 SpannerStub (com.google.spanner.v1.SpannerGrpc.SpannerStub)1 ExecutionException (java.util.concurrent.ExecutionException)1