use of com.google.cloud.spanner.MockSpannerTestUtil.SELECT1 in project java-spanner by googleapis.
the class DatabaseClientImplTest method singleUseBoundAsync.
@Test
public void singleUseBoundAsync() throws Exception {
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
final AtomicInteger rowCount = new AtomicInteger();
ApiFuture<Void> res;
try (AsyncResultSet rs = client.singleUse(TimestampBound.ofExactStaleness(15L, TimeUnit.SECONDS)).executeQueryAsync(SELECT1)) {
res = rs.setCallback(executor, resultSet -> {
while (true) {
switch(resultSet.tryNext()) {
case OK:
rowCount.incrementAndGet();
break;
case DONE:
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
}
}
});
}
res.get();
assertThat(rowCount.get()).isEqualTo(1);
}
use of com.google.cloud.spanner.MockSpannerTestUtil.SELECT1 in project java-spanner by googleapis.
the class DatabaseClientImplTest method singleUseAsync.
@Test
public void singleUseAsync() throws Exception {
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
final AtomicInteger rowCount = new AtomicInteger();
ApiFuture<Void> res;
try (AsyncResultSet rs = client.singleUse().executeQueryAsync(SELECT1)) {
res = rs.setCallback(executor, resultSet -> {
while (true) {
switch(resultSet.tryNext()) {
case OK:
rowCount.incrementAndGet();
break;
case DONE:
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
}
}
});
}
res.get();
assertThat(rowCount.get()).isEqualTo(1);
}
use of com.google.cloud.spanner.MockSpannerTestUtil.SELECT1 in project java-spanner by googleapis.
the class DatabaseClientImplTest method testDatabaseOrInstanceIsDeletedAndThenRecreated.
/**
* Test showing that when a database is deleted while it is in use by a database client and then
* re-created with the same name, will continue to return {@link DatabaseNotFoundException}s until
* a new {@link DatabaseClient} is created.
*/
@Test
public void testDatabaseOrInstanceIsDeletedAndThenRecreated() throws Exception {
StatusRuntimeException[] exceptions = new StatusRuntimeException[] { SpannerExceptionFactoryTest.newStatusResourceNotFoundException("Database", SpannerExceptionFactory.DATABASE_RESOURCE_TYPE, DATABASE_NAME), SpannerExceptionFactoryTest.newStatusResourceNotFoundException("Instance", SpannerExceptionFactory.INSTANCE_RESOURCE_TYPE, INSTANCE_NAME) };
for (StatusRuntimeException exception : exceptions) {
try (Spanner spanner = SpannerOptions.newBuilder().setProjectId(TEST_PROJECT).setChannelProvider(channelProvider).setCredentials(NoCredentials.getInstance()).build().getService()) {
DatabaseClientImpl dbClient = (DatabaseClientImpl) spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
// Wait until all sessions have been created and prepared.
Stopwatch watch = Stopwatch.createStarted();
while (watch.elapsed(TimeUnit.SECONDS) < 5 && (dbClient.pool.getNumberOfSessionsBeingCreated() > 0)) {
Thread.sleep(1L);
}
// Simulate that the database or instance has been deleted.
mockSpanner.setStickyGlobalExceptions(true);
mockSpanner.addException(exception);
// All subsequent calls should fail with a DatabaseNotFoundException.
try (ResultSet rs = dbClient.singleUse().executeQuery(SELECT1)) {
assertThrows(ResourceNotFoundException.class, () -> rs.next());
}
assertThrows(ResourceNotFoundException.class, () -> dbClient.readWriteTransaction().run(transaction -> null));
// Now simulate that the database has been re-created. The database client should still
// throw DatabaseNotFoundExceptions, as it is not the same database. The server should not
// receive any new requests.
mockSpanner.reset();
// All subsequent calls should fail with a DatabaseNotFoundException.
assertThrows(ResourceNotFoundException.class, () -> dbClient.singleUse().executeQuery(SELECT1));
assertThrows(ResourceNotFoundException.class, () -> dbClient.readWriteTransaction().run(transaction -> null));
assertThat(mockSpanner.getRequests()).isEmpty();
// Now get a new database client. Normally multiple calls to Spanner#getDatabaseClient will
// return the same instance, but not when the instance has been invalidated by a
// DatabaseNotFoundException.
DatabaseClientImpl newClient = (DatabaseClientImpl) spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
assertThat(newClient).isNotSameInstanceAs(dbClient);
// Executing a query should now work without problems.
try (ResultSet rs = newClient.singleUse().executeQuery(SELECT1)) {
while (rs.next()) {
}
}
assertThat(mockSpanner.getRequests()).isNotEmpty();
}
mockSpanner.reset();
mockSpanner.removeAllExecutionTimes();
}
}
Aggregations