use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class SpannerClientTest method streamingReadExceptionTest.
@Test
public void streamingReadExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
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);
try {
List<PartialResultSet> actualResponses = responseObserver.future().get();
Assert.fail("No exception thrown");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
}
}
use of com.google.spanner.v1.ReadRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerTestCases method testRead.
void testRead() throws InterruptedException {
System.out.println("\nTestRead");
ManagedChannel channel = getChannel();
SpannerBlockingStub stub = getBlockingStub(channel);
Session session = stub.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
ReadRequest request = ReadRequest.newBuilder().setSession(session.getName()).setTable("small_table").setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("users").addColumns("firstname").addColumns("lastname").build();
BlockingCall<ReadRequest, ResultSet> blockingCall = (ReadRequest req) -> stub.read(req);
doTestBlocking(channel, request, blockingCall);
stub.deleteSession(DeleteSessionRequest.newBuilder().setName(session.getName()).build());
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class DatabaseClientImplTest method testReadWriteExecuteReadWithPriority.
@Test
public void testReadWriteExecuteReadWithPriority() {
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
TransactionRunner runner = client.readWriteTransaction();
runner.run(transaction -> {
try (ResultSet resultSet = transaction.read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.priority(RpcPriority.HIGH))) {
while (resultSet.next()) {
}
}
return null;
});
List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
assertThat(requests).hasSize(1);
ReadRequest request = requests.get(0);
assertNotNull(request.getRequestOptions());
assertEquals(Priority.PRIORITY_HIGH, request.getRequestOptions().getPriority());
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class MockSpannerServiceImpl method streamingRead.
@Override
public void streamingRead(final ReadRequest request, StreamObserver<PartialResultSet> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getSession());
Session session = sessions.get(request.getSession());
if (session == null) {
setSessionNotFound(request.getSession(), responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
streamingReadExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
// Get or start transaction
ByteString transactionId = getTransactionId(session, request.getTransaction());
if (!request.getPartitionToken().isEmpty()) {
List<ByteString> tokens = partitionTokens.get(partitionKey(session.getName(), transactionId));
if (tokens == null || !tokens.contains(request.getPartitionToken())) {
throw Status.INVALID_ARGUMENT.withDescription(String.format("Partition token %s is not a valid token for this transaction", request.getPartitionToken())).asRuntimeException();
}
}
simulateAbort(session, transactionId);
Iterable<String> cols = () -> request.getColumnsList().iterator();
Statement statement = StatementResult.createReadStatement(request.getTable(), request.getKeySet().getAll() ? KeySet.all() : KeySet.singleKey(Key.of()), cols);
StatementResult res = getResult(statement);
if (res == null) {
throw Status.NOT_FOUND.withDescription("No result found for " + statement.toString()).asRuntimeException();
}
if (res.getType() == StatementResult.StatementResultType.EXCEPTION) {
throw res.getException();
}
returnPartialResultSet(res.getResultSet(), transactionId, request.getTransaction(), responseObserver, getStreamingReadExecutionTime());
} catch (StatusRuntimeException e) {
responseObserver.onError(e);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class MockSpannerServiceImpl method read.
@Override
public void read(final ReadRequest request, StreamObserver<ResultSet> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getSession());
Session session = sessions.get(request.getSession());
if (session == null) {
setSessionNotFound(request.getSession(), responseObserver);
return;
}
sessionLastUsed.put(session.getName(), Instant.now());
try {
readExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
// Get or start transaction
ByteString transactionId = getTransactionId(session, request.getTransaction());
simulateAbort(session, transactionId);
Iterable<String> cols = () -> request.getColumnsList().iterator();
Statement statement = StatementResult.createReadStatement(request.getTable(), request.getKeySet().getAll() ? KeySet.all() : KeySet.singleKey(Key.of()), cols);
StatementResult res = getResult(statement);
returnResultSet(res.getResultSet(), transactionId, request.getTransaction(), responseObserver);
responseObserver.onCompleted();
} catch (StatusRuntimeException e) {
responseObserver.onError(e);
} catch (Throwable t) {
responseObserver.onError(Status.INTERNAL.asRuntimeException());
}
}
Aggregations