Skip to content

Commit

Permalink
fix: enables emulator tests
Browse files Browse the repository at this point in the history
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 12, 2020
1 parent 6f47b8a commit e6e4424
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 60 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -58,10 +57,6 @@ public SpannerOptions getOptions() {
return options;
}

public boolean isEmulator() {
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
}

public Spanner getClient() {
return client;
}
Expand Down
@@ -0,0 +1,29 @@
/*
* 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 SpannerEmulatorHelper {

public static boolean isUsingEmulator() {
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
}
}
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

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

import com.google.api.gax.longrunning.OperationFuture;
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -16,6 +16,7 @@

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

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down Expand Up @@ -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 =
Expand Down
Expand Up @@ -16,6 +16,7 @@

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

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -16,6 +16,7 @@

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

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void test01_RunScript() throws Exception {
public void test02_RunAbortedTest() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());

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

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

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

import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.ParallelIntegrationTest;
import com.google.cloud.spanner.SpannerException;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
}
Expand Down
Expand Up @@ -16,6 +16,7 @@

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

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -490,7 +491,7 @@ public void testAbortWithResultSetFullyConsumed() {
public void testAbortWithConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
Expand Down Expand Up @@ -527,7 +528,7 @@ public void testAbortWithConcurrentInsert() {
public void testAbortWithConcurrentDelete() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
Expand Down Expand Up @@ -568,7 +569,7 @@ public void testAbortWithConcurrentDelete() {
public void testAbortWithConcurrentUpdate() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
Expand Down Expand Up @@ -614,7 +615,7 @@ public void testAbortWithConcurrentUpdate() {
public void testAbortWithUnseenConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
Expand Down Expand Up @@ -664,7 +665,7 @@ public void testAbortWithUnseenConcurrentInsert() {
public void testAbortWithUnseenConcurrentInsertAbortOnNext() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
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
Expand All @@ -688,7 +689,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
throws AbortedDueToConcurrentModificationException {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
int retries = 0;
clearTable();
clearStatistics();
Expand Down Expand Up @@ -750,7 +751,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
public void testAbortWithConcurrentInsertAndContinue() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
Expand Down Expand Up @@ -962,7 +963,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
public void testNestedAbortWithConcurrentInsert() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor =
new AbortInterceptor(0) {
private boolean alreadyAborted = false;
Expand Down Expand Up @@ -1027,7 +1028,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
public void testAbortWithDifferentUpdateCount() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
Expand Down Expand Up @@ -1075,7 +1076,7 @@ public void testAbortWithDifferentUpdateCount() {
public void testAbortWithExceptionOnSelect() {
assumeFalse(
"resume after error in transaction is not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
try (ITConnection connection = createConnection()) {
Expand Down Expand Up @@ -1127,7 +1128,7 @@ public void testAbortWithExceptionOnSelect() {
public void testAbortWithExceptionOnSelectAndConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
Expand Down Expand Up @@ -1197,7 +1198,7 @@ public void testAbortWithExceptionOnSelectAndConcurrentModification() {
public void testAbortWithExceptionOnInsertAndConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
Expand Down Expand Up @@ -1266,7 +1267,7 @@ public void testAbortWithExceptionOnInsertAndConcurrentModification() {
public void testAbortWithDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
Expand Down Expand Up @@ -1331,7 +1332,7 @@ public void testAbortWithDroppedTableConcurrentModification() {
public void testAbortWithInsertOnDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
Expand Down Expand Up @@ -1393,7 +1394,7 @@ public void testAbortWithInsertOnDroppedTableConcurrentModification() {
public void testAbortWithCursorHalfwayDroppedTableConcurrentModification() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
boolean abortedDueToConcurrentModification = false;
AbortInterceptor interceptor = new AbortInterceptor(0);
// first insert two test records
Expand Down Expand Up @@ -1548,7 +1549,7 @@ public void testRetryHighAbortRate() {
public void testAbortWithConcurrentInsertOnEmptyTable() {
assumeFalse(
"concurrent transactions are not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection =
createConnection(interceptor, new CountTransactionRetryListener())) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.it;

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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.cloud.Timestamp;
import com.google.cloud.spanner.Database;
Expand Down Expand Up @@ -325,19 +324,14 @@ public void interleavedTableHierarchy1() {
.build()));
fail("missing expected exception");
} catch (SpannerException e) {
// TODO: Remove when the emulator returns the same error code as Cloud Spanner.
if (!env.getTestHelper().isEmulator()) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}

// In interleaved table, use of commit timestamp in parent table is not
// allowed if child tables are not allow_commmit_timestamp=true
@Test
public void interleavedTableHierarchy2() {
// TODO: Remove the following line once the emulator is as strict as Cloud Spanner.
assumeFalse("The emulator allows this situation", env.getTestHelper().isEmulator());
Database db =
testHelper.createTestDatabase(
"CREATE TABLE T1 (ts TIMESTAMP OPTIONS (allow_commit_timestamp = true)) "
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.it;

import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
Expand Down Expand Up @@ -266,7 +267,7 @@ public void onClose(Status status, Metadata metadata) {
public void testRetryNonIdempotentRpcsReturningLongRunningOperations() throws Exception {
assumeFalse(
"Querying long-running operations is not supported on the emulator",
env.getTestHelper().isEmulator());
isUsingEmulator());

// RPCs that return a long-running operation such as CreateDatabase, CreateBackup and
// RestoreDatabase are non-idempotent and can normally not be automatically retried in case of a
Expand Down

0 comments on commit e6e4424

Please sign in to comment.