Skip to content

Commit 52f8684

Browse files
authored
test: speed up ITDmlReturningTest (#3248)
The initialization code for ITDmlReturningTest was borked and initialized the database over and over again for each test method. This pushes Spanner into throttling the DDL requests, which again makes both this and other integration tests slow. Updates #3247
1 parent 35907c6 commit 52f8684

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

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

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertThrows;
2424
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assume.assumeFalse;
2625

2726
import com.google.cloud.spanner.AsyncResultSet;
2827
import com.google.cloud.spanner.AsyncResultSet.CallbackResponse;
28+
import com.google.cloud.spanner.DatabaseClient;
2929
import com.google.cloud.spanner.Dialect;
3030
import com.google.cloud.spanner.ErrorCode;
31+
import com.google.cloud.spanner.KeySet;
3132
import com.google.cloud.spanner.Mutation;
3233
import com.google.cloud.spanner.ParallelIntegrationTest;
3334
import com.google.cloud.spanner.ResultSet;
@@ -39,15 +40,15 @@
3940
import com.google.cloud.spanner.connection.StatementResult;
4041
import com.google.cloud.spanner.connection.StatementResult.ResultType;
4142
import com.google.cloud.spanner.connection.TransactionMode;
42-
import com.google.cloud.spanner.testing.EmulatorSpannerHelper;
43+
import com.google.common.base.Preconditions;
4344
import com.google.common.collect.ImmutableList;
4445
import com.google.common.collect.ImmutableMap;
4546
import java.util.ArrayList;
4647
import java.util.Arrays;
4748
import java.util.Collections;
48-
import java.util.HashMap;
49+
import java.util.HashSet;
4950
import java.util.List;
50-
import java.util.Map;
51+
import java.util.Set;
5152
import java.util.concurrent.ExecutionException;
5253
import java.util.concurrent.Executors;
5354
import org.junit.Before;
@@ -81,12 +82,7 @@ public class ITDmlReturningTest extends ITAbstractSpannerTest {
8182
+ " SingerId BIGINT PRIMARY KEY,"
8283
+ " FirstName character varying(1024),"
8384
+ " LastName character varying(1024))");
84-
private final Map<Dialect, Boolean> IS_INITIALIZED = new HashMap<>();
85-
86-
public ITDmlReturningTest() {
87-
IS_INITIALIZED.put(Dialect.GOOGLE_STANDARD_SQL, false);
88-
IS_INITIALIZED.put(Dialect.POSTGRESQL, false);
89-
}
85+
private static final Set<Dialect> IS_INITIALIZED = new HashSet<>();
9086

9187
@Parameter public Dialect dialect;
9288

@@ -96,41 +92,34 @@ public static Object[] data() {
9692
}
9793

9894
private boolean checkAndSetInitialized() {
99-
if ((dialect == Dialect.GOOGLE_STANDARD_SQL) && !IS_INITIALIZED.get(dialect)) {
100-
IS_INITIALIZED.put(dialect, true);
101-
return true;
102-
}
103-
if ((dialect == Dialect.POSTGRESQL) && !IS_INITIALIZED.get(dialect)) {
104-
IS_INITIALIZED.put(dialect, true);
105-
return true;
106-
}
107-
return false;
95+
return !IS_INITIALIZED.add(dialect);
10896
}
10997

11098
@Before
11199
public void setupTable() {
112-
assumeFalse(
113-
"DML Returning is not supported in the emulator", EmulatorSpannerHelper.isUsingEmulator());
114-
if (checkAndSetInitialized()) {
100+
if (!checkAndSetInitialized()) {
115101
database =
116102
env.getTestHelper()
117103
.createTestDatabase(dialect, Collections.singleton(DDL_MAP.get(dialect)));
118-
List<String> firstNames = Arrays.asList("ABC", "ABC", "DEF", "PQR", "ABC");
119-
List<String> lastNames = Arrays.asList("XYZ", "DEF", "XYZ", "ABC", "GHI");
120-
List<Mutation> mutations = new ArrayList<>();
121-
for (int id = 1; id <= 5; id++) {
122-
mutations.add(
123-
Mutation.newInsertBuilder("SINGERS")
124-
.set("SINGERID")
125-
.to(id)
126-
.set("FIRSTNAME")
127-
.to(firstNames.get(id - 1))
128-
.set("LASTNAME")
129-
.to(lastNames.get(id - 1))
130-
.build());
131-
}
132-
env.getTestHelper().getDatabaseClient(database).write(mutations);
133104
}
105+
DatabaseClient client = env.getTestHelper().getDatabaseClient(database);
106+
client.write(ImmutableList.of(Mutation.delete("SINGERS", KeySet.all())));
107+
108+
List<String> firstNames = Arrays.asList("ABC", "ABC", "DEF", "PQR", "ABC");
109+
List<String> lastNames = Arrays.asList("XYZ", "DEF", "XYZ", "ABC", "GHI");
110+
List<Mutation> mutations = new ArrayList<>();
111+
for (int id = 1; id <= 5; id++) {
112+
mutations.add(
113+
Mutation.newInsertBuilder("SINGERS")
114+
.set("SINGERID")
115+
.to(id)
116+
.set("FIRSTNAME")
117+
.to(firstNames.get(id - 1))
118+
.set("LASTNAME")
119+
.to(lastNames.get(id - 1))
120+
.build());
121+
}
122+
env.getTestHelper().getDatabaseClient(database).write(mutations);
134123
}
135124

136125
@Test
@@ -211,9 +200,9 @@ public void testDmlReturningExecuteUpdateAsync() {
211200
public void testDmlReturningExecuteBatchUpdate() {
212201
try (Connection connection = createConnection()) {
213202
connection.setAutocommit(false);
214-
final Statement UPDATE_STMT = UPDATE_RETURNING_MAP.get(dialect);
203+
final Statement updateStmt = Preconditions.checkNotNull(UPDATE_RETURNING_MAP.get(dialect));
215204
long[] counts =
216-
connection.executeBatchUpdate(ImmutableList.of(UPDATE_STMT, UPDATE_STMT, UPDATE_STMT));
205+
connection.executeBatchUpdate(ImmutableList.of(updateStmt, updateStmt, updateStmt));
217206
assertArrayEquals(counts, new long[] {3, 3, 3});
218207
}
219208
}
@@ -222,10 +211,10 @@ public void testDmlReturningExecuteBatchUpdate() {
222211
public void testDmlReturningExecuteBatchUpdateAsync() {
223212
try (Connection connection = createConnection()) {
224213
connection.setAutocommit(false);
225-
final Statement UPDATE_STMT = UPDATE_RETURNING_MAP.get(dialect);
214+
final Statement updateStmt = Preconditions.checkNotNull(UPDATE_RETURNING_MAP.get(dialect));
226215
long[] counts =
227216
connection
228-
.executeBatchUpdateAsync(ImmutableList.of(UPDATE_STMT, UPDATE_STMT, UPDATE_STMT))
217+
.executeBatchUpdateAsync(ImmutableList.of(updateStmt, updateStmt, updateStmt))
229218
.get();
230219
assertArrayEquals(counts, new long[] {3, 3, 3});
231220
} catch (ExecutionException | InterruptedException e) {

0 commit comments

Comments
 (0)