use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.
the class SpannerClientTest method batchCreateSessionsTest2.
@Test
public void batchCreateSessionsTest2() throws Exception {
BatchCreateSessionsResponse expectedResponse = BatchCreateSessionsResponse.newBuilder().addAllSession(new ArrayList<Session>()).build();
mockSpanner.addResponse(expectedResponse);
String database = "database1789464955";
int sessionCount = 185691686;
BatchCreateSessionsResponse actualResponse = client.batchCreateSessions(database, sessionCount);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
BatchCreateSessionsRequest actualRequest = ((BatchCreateSessionsRequest) actualRequests.get(0));
Assert.assertEquals(database, actualRequest.getDatabase());
Assert.assertEquals(sessionCount, actualRequest.getSessionCount());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.v1.BatchCreateSessionsResponse in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerIntegrationTest method testBatchCreateSessionsBlocking.
@Test
public void testBatchCreateSessionsBlocking() throws Exception {
int sessionCount = 10;
SpannerBlockingStub stub = getSpannerBlockingStub();
BatchCreateSessionsRequest req = BatchCreateSessionsRequest.newBuilder().setDatabase(DATABASE_PATH).setSessionCount(sessionCount).build();
List<Session> sessions = new ArrayList<>();
// The first MAX_CHANNEL requests (without affinity) should be distributed 1 per channel.
for (int j = 0; j < MAX_CHANNEL; j++) {
BatchCreateSessionsResponse resp = stub.batchCreateSessions(req);
assertThat(resp.getSessionCount()).isEqualTo(sessionCount);
sessions.addAll(resp.getSessionList());
}
checkChannelRefs(MAX_CHANNEL, 0, sessionCount);
for (Session session : sessions) {
deleteSession(stub, session);
}
checkChannelRefs(MAX_CHANNEL, 0, 0);
}
use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.
the class MockSpannerServiceImpl method batchCreateSessions.
@Override
public void batchCreateSessions(BatchCreateSessionsRequest request, StreamObserver<BatchCreateSessionsResponse> responseObserver) {
requests.add(request);
Preconditions.checkNotNull(request.getDatabase());
String name = null;
try {
if (request.getSessionCount() <= 0) {
throw Status.INVALID_ARGUMENT.withDescription("Session count must be >= 0").asRuntimeException();
}
batchCreateSessionsExecutionTime.simulateExecutionTime(exceptions, stickyGlobalExceptions, freezeLock);
if (sessions.size() >= maxTotalSessions) {
throw Status.RESOURCE_EXHAUSTED.withDescription("Maximum number of sessions reached").asRuntimeException();
}
Timestamp now = getCurrentGoogleTimestamp();
BatchCreateSessionsResponse.Builder response = BatchCreateSessionsResponse.newBuilder();
int maxSessionsToCreate = Math.min(maxNumSessionsInOneBatch, request.getSessionCount());
for (int i = 0; i < Math.min(maxTotalSessions - sessions.size(), maxSessionsToCreate); i++) {
name = generateSessionName(request.getDatabase());
Session session = Session.newBuilder().setCreateTime(now).setName(name).setApproximateLastUseTime(now).build();
Session prev = sessions.putIfAbsent(name, session);
if (prev == null) {
if (sessions.size() <= maxTotalSessions) {
sessionLastUsed.put(name, Instant.now());
response.addSession(session);
numSessionsCreated.incrementAndGet();
} else {
sessions.remove(name);
}
} else {
// Someone else tried to create a session with the same id. This should not be possible
throw Status.ALREADY_EXISTS.asRuntimeException();
}
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
} catch (StatusRuntimeException e) {
if (name != null) {
sessions.remove(name);
}
responseObserver.onError(e);
} catch (Throwable e) {
if (name != null) {
sessions.remove(name);
}
responseObserver.onError(Status.INTERNAL.withDescription("Batch create sessions failed: " + e.getMessage()).asRuntimeException());
}
}
use of com.google.spanner.v1.BatchCreateSessionsResponse in project java-spanner by googleapis.
the class SpannerClientTest method batchCreateSessionsTest.
@Test
public void batchCreateSessionsTest() throws Exception {
BatchCreateSessionsResponse expectedResponse = BatchCreateSessionsResponse.newBuilder().addAllSession(new ArrayList<Session>()).build();
mockSpanner.addResponse(expectedResponse);
DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
int sessionCount = 185691686;
BatchCreateSessionsResponse actualResponse = client.batchCreateSessions(database, sessionCount);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
BatchCreateSessionsRequest actualRequest = ((BatchCreateSessionsRequest) actualRequests.get(0));
Assert.assertEquals(database.toString(), actualRequest.getDatabase());
Assert.assertEquals(sessionCount, actualRequest.getSessionCount());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations