Skip to content

Commit e6e4424

Browse files
committed
fix: enables emulator tests
Enables emulator for integration tests that were previously being skipped. The emulator now provides the features for theses tests to run with it.
1 parent 6f47b8a commit e6e4424

20 files changed

+85
-60
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/testing/RemoteSpannerHelper.java

-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.cloud.spanner.SpannerException;
2626
import com.google.cloud.spanner.SpannerExceptionFactory;
2727
import com.google.cloud.spanner.SpannerOptions;
28-
import com.google.common.base.Strings;
2928
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
3029
import java.util.ArrayList;
3130
import java.util.Arrays;
@@ -58,10 +57,6 @@ public SpannerOptions getOptions() {
5857
return options;
5958
}
6059

61-
public boolean isEmulator() {
62-
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
63-
}
64-
6560
public Spanner getClient() {
6661
return client;
6762
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner.testing;
18+
19+
import com.google.common.base.Strings;
20+
21+
/**
22+
* Utility class for checking emulator state for tests
23+
*/
24+
public class SpannerEmulatorHelper {
25+
26+
public static boolean isUsingEmulator() {
27+
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
28+
}
29+
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/IntegrationTestEnv.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
1920
import static com.google.common.base.Preconditions.checkState;
2021

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

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITReadOnlySpannerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

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

170171
@Test
171172
public void testAnalyzeQuery() {
172-
assumeFalse("analyze query is not supported on the emulator", env.getTestHelper().isEmulator());
173+
assumeFalse("analyze query is not supported on the emulator", isUsingEmulator());
173174
try (ITConnection connection = createConnection()) {
174175
for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
175176
try (ResultSet rs =

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITReadWriteAutocommitSpannerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

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

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlMusicScriptTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
1920
import static org.hamcrest.CoreMatchers.equalTo;
2021
import static org.hamcrest.CoreMatchers.is;
2122
import static org.hamcrest.MatcherAssert.assertThat;
@@ -61,7 +62,7 @@ public void test01_RunScript() throws Exception {
6162
public void test02_RunAbortedTest() {
6263
assumeFalse(
6364
"concurrent transactions are not supported on the emulator",
64-
env.getTestHelper().isEmulator());
65+
isUsingEmulator());
6566

6667
final long SINGER_ID = 2L;
6768
final long VENUE_ID = 68L;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITSqlScriptTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
20+
1921
import com.google.cloud.spanner.ErrorCode;
2022
import com.google.cloud.spanner.ParallelIntegrationTest;
2123
import com.google.cloud.spanner.SpannerException;
@@ -76,7 +78,7 @@ public void test02_InsertTestData() throws Exception {
7678
INSERT_AND_VERIFY_TEST_DATA,
7779
SqlScriptVerifier.class);
7880
} catch (SpannerException e) {
79-
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
81+
if (isUsingEmulator() && e.getErrorCode() == ErrorCode.ALREADY_EXISTS) {
8082
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
8183
// transaction will return the same error as the error generated by a previous (update)
8284
// statement.
@@ -102,7 +104,7 @@ public void test04_TestGetCommitTimestamp() throws Exception {
102104
TEST_GET_COMMIT_TIMESTAMP,
103105
SqlScriptVerifier.class);
104106
} catch (SpannerException e) {
105-
if (env.getTestHelper().isEmulator() && e.getErrorCode() == ErrorCode.INVALID_ARGUMENT) {
107+
if (isUsingEmulator() && e.getErrorCode() == ErrorCode.INVALID_ARGUMENT) {
106108
// Errors in a transaction are 'sticky' on the emulator, so any query in the same
107109
// transaction will return the same error as the error generated by a previous statement.
108110
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITTransactionRetryTest.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import static com.google.cloud.spanner.testing.SpannerEmulatorHelper.isUsingEmulator;
1920
import static org.hamcrest.CoreMatchers.equalTo;
2021
import static org.hamcrest.CoreMatchers.is;
2122
import static org.hamcrest.MatcherAssert.assertThat;
@@ -490,7 +491,7 @@ public void testAbortWithResultSetFullyConsumed() {
490491
public void testAbortWithConcurrentInsert() {
491492
assumeFalse(
492493
"concurrent transactions are not supported on the emulator",
493-
env.getTestHelper().isEmulator());
494+
isUsingEmulator());
494495
AbortInterceptor interceptor = new AbortInterceptor(0);
495496
try (ITConnection connection =
496497
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -527,7 +528,7 @@ public void testAbortWithConcurrentInsert() {
527528
public void testAbortWithConcurrentDelete() {
528529
assumeFalse(
529530
"concurrent transactions are not supported on the emulator",
530-
env.getTestHelper().isEmulator());
531+
isUsingEmulator());
531532
AbortInterceptor interceptor = new AbortInterceptor(0);
532533
// first insert two test records
533534
try (ITConnection connection = createConnection()) {
@@ -568,7 +569,7 @@ public void testAbortWithConcurrentDelete() {
568569
public void testAbortWithConcurrentUpdate() {
569570
assumeFalse(
570571
"concurrent transactions are not supported on the emulator",
571-
env.getTestHelper().isEmulator());
572+
isUsingEmulator());
572573
AbortInterceptor interceptor = new AbortInterceptor(0);
573574
// first insert two test records
574575
try (ITConnection connection = createConnection()) {
@@ -614,7 +615,7 @@ public void testAbortWithConcurrentUpdate() {
614615
public void testAbortWithUnseenConcurrentInsert() {
615616
assumeFalse(
616617
"concurrent transactions are not supported on the emulator",
617-
env.getTestHelper().isEmulator());
618+
isUsingEmulator());
618619
AbortInterceptor interceptor = new AbortInterceptor(0);
619620
try (ITConnection connection =
620621
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -664,7 +665,7 @@ public void testAbortWithUnseenConcurrentInsert() {
664665
public void testAbortWithUnseenConcurrentInsertAbortOnNext() {
665666
assumeFalse(
666667
"concurrent transactions are not supported on the emulator",
667-
env.getTestHelper().isEmulator());
668+
isUsingEmulator());
668669
// no calls to next(), this should succeed
669670
assertThat(testAbortWithUnseenConcurrentInsertAbortOnNext(0) >= 1, is(true));
670671
// 1 call to next() should also succeed, as there were 2 records in the original result set
@@ -688,7 +689,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
688689
throws AbortedDueToConcurrentModificationException {
689690
assumeFalse(
690691
"concurrent transactions are not supported on the emulator",
691-
env.getTestHelper().isEmulator());
692+
isUsingEmulator());
692693
int retries = 0;
693694
clearTable();
694695
clearStatistics();
@@ -750,7 +751,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
750751
public void testAbortWithConcurrentInsertAndContinue() {
751752
assumeFalse(
752753
"concurrent transactions are not supported on the emulator",
753-
env.getTestHelper().isEmulator());
754+
isUsingEmulator());
754755
AbortInterceptor interceptor = new AbortInterceptor(0);
755756
try (ITConnection connection =
756757
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -962,7 +963,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
962963
public void testNestedAbortWithConcurrentInsert() {
963964
assumeFalse(
964965
"concurrent transactions are not supported on the emulator",
965-
env.getTestHelper().isEmulator());
966+
isUsingEmulator());
966967
AbortInterceptor interceptor =
967968
new AbortInterceptor(0) {
968969
private boolean alreadyAborted = false;
@@ -1027,7 +1028,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
10271028
public void testAbortWithDifferentUpdateCount() {
10281029
assumeFalse(
10291030
"concurrent transactions are not supported on the emulator",
1030-
env.getTestHelper().isEmulator());
1031+
isUsingEmulator());
10311032
AbortInterceptor interceptor = new AbortInterceptor(0);
10321033
// first insert two test records
10331034
try (ITConnection connection = createConnection()) {
@@ -1075,7 +1076,7 @@ public void testAbortWithDifferentUpdateCount() {
10751076
public void testAbortWithExceptionOnSelect() {
10761077
assumeFalse(
10771078
"resume after error in transaction is not supported on the emulator",
1078-
env.getTestHelper().isEmulator());
1079+
isUsingEmulator());
10791080
AbortInterceptor interceptor = new AbortInterceptor(0);
10801081
// first insert two test records
10811082
try (ITConnection connection = createConnection()) {
@@ -1127,7 +1128,7 @@ public void testAbortWithExceptionOnSelect() {
11271128
public void testAbortWithExceptionOnSelectAndConcurrentModification() {
11281129
assumeFalse(
11291130
"concurrent transactions are not supported on the emulator",
1130-
env.getTestHelper().isEmulator());
1131+
isUsingEmulator());
11311132
boolean abortedDueToConcurrentModification = false;
11321133
AbortInterceptor interceptor = new AbortInterceptor(0);
11331134
// first insert two test records
@@ -1197,7 +1198,7 @@ public void testAbortWithExceptionOnSelectAndConcurrentModification() {
11971198
public void testAbortWithExceptionOnInsertAndConcurrentModification() {
11981199
assumeFalse(
11991200
"concurrent transactions are not supported on the emulator",
1200-
env.getTestHelper().isEmulator());
1201+
isUsingEmulator());
12011202
boolean abortedDueToConcurrentModification = false;
12021203
AbortInterceptor interceptor = new AbortInterceptor(0);
12031204
// first insert two test records
@@ -1266,7 +1267,7 @@ public void testAbortWithExceptionOnInsertAndConcurrentModification() {
12661267
public void testAbortWithDroppedTableConcurrentModification() {
12671268
assumeFalse(
12681269
"concurrent transactions are not supported on the emulator",
1269-
env.getTestHelper().isEmulator());
1270+
isUsingEmulator());
12701271
boolean abortedDueToConcurrentModification = false;
12711272
AbortInterceptor interceptor = new AbortInterceptor(0);
12721273
// first insert two test records
@@ -1331,7 +1332,7 @@ public void testAbortWithDroppedTableConcurrentModification() {
13311332
public void testAbortWithInsertOnDroppedTableConcurrentModification() {
13321333
assumeFalse(
13331334
"concurrent transactions are not supported on the emulator",
1334-
env.getTestHelper().isEmulator());
1335+
isUsingEmulator());
13351336
boolean abortedDueToConcurrentModification = false;
13361337
AbortInterceptor interceptor = new AbortInterceptor(0);
13371338
// first insert two test records
@@ -1393,7 +1394,7 @@ public void testAbortWithInsertOnDroppedTableConcurrentModification() {
13931394
public void testAbortWithCursorHalfwayDroppedTableConcurrentModification() {
13941395
assumeFalse(
13951396
"concurrent transactions are not supported on the emulator",
1396-
env.getTestHelper().isEmulator());
1397+
isUsingEmulator());
13971398
boolean abortedDueToConcurrentModification = false;
13981399
AbortInterceptor interceptor = new AbortInterceptor(0);
13991400
// first insert two test records
@@ -1548,7 +1549,7 @@ public void testRetryHighAbortRate() {
15481549
public void testAbortWithConcurrentInsertOnEmptyTable() {
15491550
assumeFalse(
15501551
"concurrent transactions are not supported on the emulator",
1551-
env.getTestHelper().isEmulator());
1552+
isUsingEmulator());
15521553
AbortInterceptor interceptor = new AbortInterceptor(0);
15531554
try (ITConnection connection =
15541555
createConnection(interceptor, new CountTransactionRetryListener())) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITAsyncAPITest.java

-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.fail;
21-
import static org.junit.Assume.assumeFalse;
2221

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

280279
@Test
281280
public void asyncRunnerFireAndForgetInvalidUpdate() throws Exception {
282-
assumeFalse(
283-
"errors in read/write transactions on emulator are sticky",
284-
env.getTestHelper().isEmulator());
285281
try {
286282
assertThat(client.singleUse().readRow("TestTable", Key.of("k999"), ALL_COLUMNS)).isNull();
287283
AsyncRunner runner = client.runAsync();

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITBackupTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.it;
1818

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

9495
@BeforeClass
9596
public static void doNotRunOnEmulator() {
96-
assumeFalse("backups are not supported on the emulator", env.getTestHelper().isEmulator());
97+
assumeFalse("backups are not supported on the emulator", isUsingEmulator());
9798
}
9899

99100
@Before

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java

-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertNotNull;
2121
import static org.junit.Assert.fail;
22-
import static org.junit.Assume.assumeFalse;
2322

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

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

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITCommitTimestampTest.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.fail;
21-
import static org.junit.Assume.assumeFalse;
2221

2322
import com.google.cloud.Timestamp;
2423
import com.google.cloud.spanner.Database;
@@ -325,19 +324,14 @@ public void interleavedTableHierarchy1() {
325324
.build()));
326325
fail("missing expected exception");
327326
} catch (SpannerException e) {
328-
// TODO: Remove when the emulator returns the same error code as Cloud Spanner.
329-
if (!env.getTestHelper().isEmulator()) {
330-
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
331-
}
327+
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
332328
}
333329
}
334330

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

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDatabaseAdminTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner.it;
1818

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

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

0 commit comments

Comments
 (0)