Skip to content

Commit f61c6d0

Browse files
authored
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.
1 parent 6f47b8a commit f61c6d0

20 files changed

+99
-104
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/** Utility class for checking emulator state for tests */
22+
public class EmulatorSpannerHelper {
23+
24+
public static final String SPANNER_EMULATOR_HOST = "SPANNER_EMULATOR_HOST";
25+
26+
/**
27+
* Checks whether the emulator is being used. This is done by checking if the
28+
* SPANNER_EMULATOR_HOST environment variable is set.
29+
*
30+
* @return true if the emulator is being used. Returns false otherwise.
31+
*/
32+
public static boolean isUsingEmulator() {
33+
return !Strings.isNullOrEmpty(System.getenv(SPANNER_EMULATOR_HOST));
34+
}
35+
}

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

+8-2
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,8 +57,15 @@ public SpannerOptions getOptions() {
5857
return options;
5958
}
6059

60+
/**
61+
* Checks whether the emulator is being used.
62+
*
63+
* @deprecated use {@link EmulatorSpannerHelper#isUsingEmulator()} instead.
64+
* @return true if the emulator is being used. Returns false otherwise.
65+
*/
66+
@Deprecated
6167
public boolean isEmulator() {
62-
return !Strings.isNullOrEmpty(System.getenv("SPANNER_EMULATOR_HOST"));
68+
return EmulatorSpannerHelper.isUsingEmulator();
6369
}
6470

6571
public Spanner getClient() {

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.EmulatorSpannerHelper.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.EmulatorSpannerHelper.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.EmulatorSpannerHelper.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-3
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.EmulatorSpannerHelper.isUsingEmulator;
1920
import static org.hamcrest.CoreMatchers.equalTo;
2021
import static org.hamcrest.CoreMatchers.is;
2122
import static org.hamcrest.MatcherAssert.assertThat;
@@ -59,9 +60,7 @@ public void test01_RunScript() throws Exception {
5960

6061
@Test
6162
public void test02_RunAbortedTest() {
62-
assumeFalse(
63-
"concurrent transactions are not supported on the emulator",
64-
env.getTestHelper().isEmulator());
63+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
6564

6665
final long SINGER_ID = 2L;
6766
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.EmulatorSpannerHelper.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-47
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.EmulatorSpannerHelper.isUsingEmulator;
1920
import static org.hamcrest.CoreMatchers.equalTo;
2021
import static org.hamcrest.CoreMatchers.is;
2122
import static org.hamcrest.MatcherAssert.assertThat;
@@ -488,9 +489,7 @@ public void testAbortWithResultSetFullyConsumed() {
488489

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

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

567564
@Test
568565
public void testAbortWithConcurrentUpdate() {
569-
assumeFalse(
570-
"concurrent transactions are not supported on the emulator",
571-
env.getTestHelper().isEmulator());
566+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
572567
AbortInterceptor interceptor = new AbortInterceptor(0);
573568
// first insert two test records
574569
try (ITConnection connection = createConnection()) {
@@ -612,9 +607,7 @@ public void testAbortWithConcurrentUpdate() {
612607
*/
613608
@Test
614609
public void testAbortWithUnseenConcurrentInsert() {
615-
assumeFalse(
616-
"concurrent transactions are not supported on the emulator",
617-
env.getTestHelper().isEmulator());
610+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
618611
AbortInterceptor interceptor = new AbortInterceptor(0);
619612
try (ITConnection connection =
620613
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -662,9 +655,7 @@ public void testAbortWithUnseenConcurrentInsert() {
662655
*/
663656
@Test
664657
public void testAbortWithUnseenConcurrentInsertAbortOnNext() {
665-
assumeFalse(
666-
"concurrent transactions are not supported on the emulator",
667-
env.getTestHelper().isEmulator());
658+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
668659
// no calls to next(), this should succeed
669660
assertThat(testAbortWithUnseenConcurrentInsertAbortOnNext(0) >= 1, is(true));
670661
// 1 call to next() should also succeed, as there were 2 records in the original result set
@@ -686,9 +677,7 @@ public void testAbortWithUnseenConcurrentInsertAbortOnNext() {
686677

687678
private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
688679
throws AbortedDueToConcurrentModificationException {
689-
assumeFalse(
690-
"concurrent transactions are not supported on the emulator",
691-
env.getTestHelper().isEmulator());
680+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
692681
int retries = 0;
693682
clearTable();
694683
clearStatistics();
@@ -748,9 +737,7 @@ private int testAbortWithUnseenConcurrentInsertAbortOnNext(int callsToNext)
748737
*/
749738
@Test
750739
public void testAbortWithConcurrentInsertAndContinue() {
751-
assumeFalse(
752-
"concurrent transactions are not supported on the emulator",
753-
env.getTestHelper().isEmulator());
740+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
754741
AbortInterceptor interceptor = new AbortInterceptor(0);
755742
try (ITConnection connection =
756743
createConnection(interceptor, new CountTransactionRetryListener())) {
@@ -960,9 +947,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
960947
*/
961948
@Test
962949
public void testNestedAbortWithConcurrentInsert() {
963-
assumeFalse(
964-
"concurrent transactions are not supported on the emulator",
965-
env.getTestHelper().isEmulator());
950+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
966951
AbortInterceptor interceptor =
967952
new AbortInterceptor(0) {
968953
private boolean alreadyAborted = false;
@@ -1025,9 +1010,7 @@ protected boolean shouldAbort(String statement, ExecutionStep step) {
10251010
*/
10261011
@Test
10271012
public void testAbortWithDifferentUpdateCount() {
1028-
assumeFalse(
1029-
"concurrent transactions are not supported on the emulator",
1030-
env.getTestHelper().isEmulator());
1013+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
10311014
AbortInterceptor interceptor = new AbortInterceptor(0);
10321015
// first insert two test records
10331016
try (ITConnection connection = createConnection()) {
@@ -1074,8 +1057,7 @@ public void testAbortWithDifferentUpdateCount() {
10741057
@Test
10751058
public void testAbortWithExceptionOnSelect() {
10761059
assumeFalse(
1077-
"resume after error in transaction is not supported on the emulator",
1078-
env.getTestHelper().isEmulator());
1060+
"resume after error in transaction is not supported on the emulator", isUsingEmulator());
10791061
AbortInterceptor interceptor = new AbortInterceptor(0);
10801062
// first insert two test records
10811063
try (ITConnection connection = createConnection()) {
@@ -1125,9 +1107,7 @@ public void testAbortWithExceptionOnSelect() {
11251107
*/
11261108
@Test
11271109
public void testAbortWithExceptionOnSelectAndConcurrentModification() {
1128-
assumeFalse(
1129-
"concurrent transactions are not supported on the emulator",
1130-
env.getTestHelper().isEmulator());
1110+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
11311111
boolean abortedDueToConcurrentModification = false;
11321112
AbortInterceptor interceptor = new AbortInterceptor(0);
11331113
// first insert two test records
@@ -1195,9 +1175,7 @@ public void testAbortWithExceptionOnSelectAndConcurrentModification() {
11951175
*/
11961176
@Test
11971177
public void testAbortWithExceptionOnInsertAndConcurrentModification() {
1198-
assumeFalse(
1199-
"concurrent transactions are not supported on the emulator",
1200-
env.getTestHelper().isEmulator());
1178+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
12011179
boolean abortedDueToConcurrentModification = false;
12021180
AbortInterceptor interceptor = new AbortInterceptor(0);
12031181
// first insert two test records
@@ -1264,9 +1242,7 @@ public void testAbortWithExceptionOnInsertAndConcurrentModification() {
12641242
*/
12651243
@Test
12661244
public void testAbortWithDroppedTableConcurrentModification() {
1267-
assumeFalse(
1268-
"concurrent transactions are not supported on the emulator",
1269-
env.getTestHelper().isEmulator());
1245+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
12701246
boolean abortedDueToConcurrentModification = false;
12711247
AbortInterceptor interceptor = new AbortInterceptor(0);
12721248
// first insert two test records
@@ -1329,9 +1305,7 @@ public void testAbortWithDroppedTableConcurrentModification() {
13291305
*/
13301306
@Test
13311307
public void testAbortWithInsertOnDroppedTableConcurrentModification() {
1332-
assumeFalse(
1333-
"concurrent transactions are not supported on the emulator",
1334-
env.getTestHelper().isEmulator());
1308+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
13351309
boolean abortedDueToConcurrentModification = false;
13361310
AbortInterceptor interceptor = new AbortInterceptor(0);
13371311
// first insert two test records
@@ -1391,9 +1365,7 @@ public void testAbortWithInsertOnDroppedTableConcurrentModification() {
13911365
*/
13921366
@Test
13931367
public void testAbortWithCursorHalfwayDroppedTableConcurrentModification() {
1394-
assumeFalse(
1395-
"concurrent transactions are not supported on the emulator",
1396-
env.getTestHelper().isEmulator());
1368+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
13971369
boolean abortedDueToConcurrentModification = false;
13981370
AbortInterceptor interceptor = new AbortInterceptor(0);
13991371
// first insert two test records
@@ -1546,9 +1518,7 @@ public void testRetryHighAbortRate() {
15461518

15471519
@Test
15481520
public void testAbortWithConcurrentInsertOnEmptyTable() {
1549-
assumeFalse(
1550-
"concurrent transactions are not supported on the emulator",
1551-
env.getTestHelper().isEmulator());
1521+
assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
15521522
AbortInterceptor interceptor = new AbortInterceptor(0);
15531523
try (ITConnection connection =
15541524
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.EmulatorSpannerHelper.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);

0 commit comments

Comments
 (0)