Skip to content
Permalink
Browse files
fix: enables emulator tests (#380)
Enables emulator for integration tests that were previously being skipped. The emulator now provides the features for theses tests to run with it.
  • Loading branch information
thiagotnunes committed Aug 13, 2020
1 parent 6f47b8a commit f61c6d0d332f15826499996a292acc7cbab267a7
Showing with 99 additions and 104 deletions.
  1. +35 −0 google-cloud-spanner/src/main/java/com/google/cloud/spanner/testing/EmulatorSpannerHelper.java
  2. +8 −2 google-cloud-spanner/src/main/java/com/google/cloud/spanner/testing/RemoteSpannerHelper.java
  3. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/IntegrationTestEnv.java
  4. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITReadOnlySpannerTest.java
  5. +2 −1 ...panner/src/test/java/com/google/cloud/spanner/connection/it/ITReadWriteAutocommitSpannerTest.java
  6. +2 −3 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlMusicScriptTest.java
  7. +4 −2 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlScriptTest.java
  8. +17 −47 ...le-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITTransactionRetryTest.java
  9. +0 −4 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITAsyncAPITest.java
  10. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITBackupTest.java
  11. +0 −5 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java
  12. +1 −7 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITCommitTimestampTest.java
  13. +2 −2 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDatabaseAdminTest.java
  14. +3 −4 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDatabaseTest.java
  15. +3 −4 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITInstanceAdminTest.java
  16. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITQueryOptionsTest.java
  17. +5 −5 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITQueryTest.java
  18. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITTransactionManagerAsyncTest.java
  19. +2 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITTransactionManagerTest.java
  20. +5 −12 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITTransactionTest.java
@@ -0,0 +1,35 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner.testing;

import com.google.common.base.Strings;

/** Utility class for checking emulator state for tests */
public class EmulatorSpannerHelper {

public static final String SPANNER_EMULATOR_HOST = "SPANNER_EMULATOR_HOST";

/**
* Checks whether the emulator is being used. This is done by checking if the
* SPANNER_EMULATOR_HOST environment variable is set.
*
* @return true if the emulator is being used. Returns false otherwise.
*/
public static boolean isUsingEmulator() {
return !Strings.isNullOrEmpty(System.getenv(SPANNER_EMULATOR_HOST));
}
}
@@ -25,7 +25,6 @@
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.common.base.Strings;
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
import java.util.ArrayList;
import java.util.Arrays;
@@ -58,8 +57,15 @@ public SpannerOptions getOptions() {
return options;
}

/**
* Checks whether the emulator is being used.
*
* @deprecated use {@link EmulatorSpannerHelper#isUsingEmulator()} instead.
* @return true if the emulator is being used. Returns false otherwise.
*/
@Deprecated
public boolean isEmulator() {
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
return EmulatorSpannerHelper.isUsingEmulator();
}

public Spanner getClient() {
@@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static com.google.common.base.Preconditions.checkState;

import com.google.api.gax.longrunning.OperationFuture;
@@ -79,7 +80,7 @@ protected void before() throws Throwable {
SpannerOptions options = config.spannerOptions();
String instanceProperty = System.getProperty(TEST_INSTANCE_PROPERTY, "");
InstanceId instanceId;
if (!instanceProperty.isEmpty()) {
if (!instanceProperty.isEmpty() && !isUsingEmulator()) {
instanceId = InstanceId.of(instanceProperty);
isOwnedInstance = false;
logger.log(Level.INFO, "Using existing test instance: {0}", instanceId);
@@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
@@ -169,7 +170,7 @@ public void testStatementTimeoutAutocommit() {

@Test
public void testAnalyzeQuery() {
assumeFalse("analyze query is not supported on the emulator", env.getTestHelper().isEmulator());
assumeFalse("analyze query is not supported on the emulator", isUsingEmulator());
try (ITConnection connection = createConnection()) {
for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
try (ResultSet rs =
@@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -76,7 +77,7 @@ public void test02_WriteMutation() {
public void test03_MultipleStatements_WithTimeouts() {
assumeFalse(
"Rolling back a transaction while an update statement is still in flight can cause the transaction to remain active on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
try (ITConnection connection = createConnection()) {
// do an insert that should succeed
assertThat(
@@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -59,9 +60,7 @@ public void test01_RunScript() throws Exception {

@Test
public void test02_RunAbortedTest() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());

final long SINGER_ID = 2L;
final long VENUE_ID = 68L;
@@ -16,6 +16,8 @@

package com.google.cloud.spanner.connection.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.ParallelIntegrationTest;
import com.google.cloud.spanner.SpannerException;
@@ -76,7 +78,7 @@ public void test02_InsertTestData() throws Exception {
INSERT_AND_VERIFY_TEST_DATA,
SqlScriptVerifier.class);
} catch (SpannerException e) {
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
if (isUsingEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
// transaction will return the same error as the error generated by a previous (update)
// statement.
@@ -102,7 +104,7 @@ public void test04_TestGetCommitTimestamp() throws Exception {
TEST_GET_COMMIT_TIMESTAMP,
SqlScriptVerifier.class);
} catch (SpannerException e) {
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.INVALID_ARGUMENT) {
if (isUsingEmulator() && e.getErrorCode() == ErrorCode.INVALID_ARGUMENT) {
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
// transaction will return the same error as the error generated by a previous statement.
}
@@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -488,9 +489,7 @@ public void testAbortWithResultSetFullyConsumed() {

@Test
public void testAbortWithConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -525,9 +524,7 @@ public void testAbortWithConcurrentInsert() {

@Test
public void testAbortWithConcurrentDelete() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
@@ -566,9 +563,7 @@ public void testAbortWithConcurrentDelete() {

@Test
public void testAbortWithConcurrentUpdate() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
@@ -612,9 +607,7 @@ public void testAbortWithConcurrentUpdate() {
*/
@Test
public void testAbortWithUnseenConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -662,9 +655,7 @@ public void testAbortWithUnseenConcurrentInsert() {
*/
@Test
public void testAbortWithUnseenConcurrentInsertAbortOnNext() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
// no calls to next(), this should succeed
assertThat(testAbortWithUnseenConcurrentInsertAbortOnNext(0) >= 1, is(true));
// 1 call to next() should also succeed, as there were 2 records in the original result set
@@ -686,9 +677,7 @@ public void testAbortWithUnseenConcurrentInsertAbortOnNext() {

private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
throws AbortedDueToConcurrentModificationException {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
int retries = 0;
clearTable();
clearStatistics();
@@ -748,9 +737,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
*/
@Test
public void testAbortWithConcurrentInsertAndContinue() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -960,9 +947,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
*/
@Test
public void testNestedAbortWithConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor =
new AbortInterceptor(0) {
private boolean alreadyAborted = false;
@@ -1025,9 +1010,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
*/
@Test
public void testAbortWithDifferentUpdateCount() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
@@ -1074,8 +1057,7 @@ public void testAbortWithDifferentUpdateCount() {
@Test
public void testAbortWithExceptionOnSelect() {
assumeFalse(
"resume after error in transaction is not supported on the emulator",
env.getTestHelper().isEmulator());
"resume after error in transaction is not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
@@ -1125,9 +1107,7 @@ public void testAbortWithExceptionOnSelect() {
*/
@Test
public void testAbortWithExceptionOnSelectAndConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
@@ -1195,9 +1175,7 @@ public void testAbortWithExceptionOnSelectAndConcurrentModification() {
*/
@Test
public void testAbortWithExceptionOnInsertAndConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
@@ -1264,9 +1242,7 @@ public void testAbortWithExceptionOnInsertAndConcurrentModification() {
*/
@Test
public void testAbortWithDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
@@ -1329,9 +1305,7 @@ public void testAbortWithDroppedTableConcurrentModification() {
*/
@Test
public void testAbortWithInsertOnDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
@@ -1391,9 +1365,7 @@ public void testAbortWithInsertOnDroppedTableConcurrentModification() {
*/
@Test
public void testAbortWithCursorHalfwayDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
@@ -1546,9 +1518,7 @@ public void testRetryHighAbortRate() {

@Test
public void testAbortWithConcurrentInsertOnEmptyTable() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.api.core.ApiFuture;
import com.google.cloud.spanner.AsyncResultSet;
@@ -279,9 +278,6 @@ public void columnNotFound() throws Exception {

@Test
public void asyncRunnerFireAndForgetInvalidUpdate() throws Exception {
assumeFalse(
"errors in read/write transactions on emulator are sticky",
env.getTestHelper().isEmulator());
try {
assertThat(client.singleUse().readRow("TestTable", Key.of("k999"), ALL_COLUMNS)).isNull();
AsyncRunner runner = client.runAsync();
@@ -16,6 +16,7 @@

package com.google.cloud.spanner.it;

import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
@@ -93,7 +94,7 @@ public class ITBackupTest {

@BeforeClass
public static void doNotRunOnEmulator() {
assumeFalse("backups are not supported on the emulator", env.getTestHelper().isEmulator());
assumeFalse("backups are not supported on the emulator", isUsingEmulator());
}

@Before
@@ -19,7 +19,6 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.spanner.AbortedException;
import com.google.cloud.spanner.Database;
@@ -61,10 +60,6 @@ public class ITClosedSessionTest {

@BeforeClass
public static void setUpDatabase() {
// TODO: Enable when the emulator returns ResourceInfo for Session not found errors.
assumeFalse(
"Emulator does not return ResourceInfo for Session not found errors",
env.getTestHelper().isEmulator());
// Empty database.
db = env.getTestHelper().createTestDatabase();
client = (DatabaseClientWithClosedSessionImpl) env.getTestHelper().getDatabaseClient(db);

0 comments on commit f61c6d0

Please sign in to comment.