Skip to content

Commit

Permalink
fix: enables emulator tests (#380)
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 13, 2020
1 parent 6f47b8a commit f61c6d0
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 104 deletions.
@@ -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));
}
}
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,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() {
Expand Down
Expand Up @@ -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;
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.EmulatorSpannerHelper.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.EmulatorSpannerHelper.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.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
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.EmulatorSpannerHelper.isUsingEmulator;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())) {
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.EmulatorSpannerHelper.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

0 comments on commit f61c6d0

Please sign in to comment.