future =
client.mutateRowAsync(
- RowMutation.create(
- testEnvRule.env().getTableId(), testEnvRule.env().getRowPrefix() + "-" + i)
+ RowMutation.create(testEnvRule.env().getTableId(), rowPrefix + "-" + i)
.setCell(testEnvRule.env().getFamilyId(), "", "value"));
futures.add(future);
}
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnv.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnv.java
deleted file mode 100644
index 69c70efc3189..000000000000
--- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnv.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright 2018 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
- *
- * https://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.bigtable.data.v2.it.env;
-
-import com.google.cloud.bigtable.data.v2.BigtableDataClient;
-
-/**
- * Defines the interface of a target environment.
- *
- * This allows for integration tests to run against either production or an emulator.
- */
-public interface TestEnv {
- void start() throws Exception;
-
- void stop() throws Exception;
-
- BigtableDataClient getDataClient();
-
- String getProjectId();
-
- String getInstanceId();
-
- String getTableId();
-
- String getFamilyId();
-
- String getRowPrefix();
-}
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java
new file mode 100644
index 000000000000..3e959e56365c
--- /dev/null
+++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2018 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
+ *
+ * https://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.bigtable.test_helpers.env;
+
+import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
+import com.google.cloud.bigtable.data.v2.BigtableDataClient;
+import org.threeten.bp.Instant;
+import org.threeten.bp.temporal.ChronoUnit;
+
+/**
+ * Defines the interface of a target environment.
+ *
+ *
This allows for integration tests to run against either production or an emulator.
+ */
+public abstract class AbstractTestEnv {
+ private static final String PREFIX = "temp-";
+
+ abstract void start() throws Exception;
+
+ abstract void stop() throws Exception;
+
+ public abstract BigtableDataClient getDataClient();
+
+ public abstract BigtableTableAdminClient getTableAdminClient();
+
+ public abstract String getProjectId();
+
+ public abstract String getInstanceId();
+
+ public abstract String getTableId();
+
+ public String getFamilyId() {
+ return "cf";
+ }
+
+ public String generateTableId(String suffix) {
+ return newPrefix() + "-" + suffix;
+ }
+
+ private static String newPrefix() {
+ return newPrefix(Instant.now());
+ }
+
+ private static String newPrefix(Instant instant) {
+ return String.format(PREFIX + "015%d", instant.getEpochSecond());
+ }
+
+ void cleanUpStale() {
+ cleanupStaleTables();
+ }
+
+ private void cleanupStaleTables() {
+ String stalePrefix = newPrefix(Instant.now().minus(1, ChronoUnit.DAYS));
+
+ for (String tableId : getTableAdminClient().listTables()) {
+ if (!tableId.startsWith(PREFIX)) {
+ continue;
+ }
+ if (stalePrefix.compareTo(tableId) > 0) {
+ getTableAdminClient().deleteTable(tableId);
+ }
+ }
+ }
+}
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/DirectPathEnv.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java
similarity index 74%
rename from google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/DirectPathEnv.java
rename to google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java
index cf3356ee1f30..143f61adc59a 100644
--- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/DirectPathEnv.java
+++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java
@@ -13,24 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.google.cloud.bigtable.data.v2.it.env;
+package com.google.cloud.bigtable.test_helpers.env;
-import com.google.api.core.ApiFuture;
-import com.google.api.core.ApiFutures;
-import com.google.api.gax.rpc.ServerStream;
+import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
-import com.google.cloud.bigtable.data.v2.models.Query;
-import com.google.cloud.bigtable.data.v2.models.Row;
-import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
-import com.google.common.collect.Lists;
import java.io.IOException;
-import java.util.List;
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
/**
* Test environment that uses an existing bigtable table in the directpath environment.
@@ -48,7 +37,7 @@
*
{@code bigtable.table}
*
*/
-public class DirectPathEnv implements TestEnv {
+public class DirectPathEnv extends AbstractTestEnv {
// TODO(igorbernstein): move direct path conditional logic to gax
private static final String DIRECT_PATH_ENV_VAR = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH";
private static final String DIRECT_PATH_END_POINT =
@@ -62,10 +51,9 @@ public class DirectPathEnv implements TestEnv {
private final String projectId;
private final String instanceId;
private final String tableId;
- private static final String FAMILY_ID = "cf";
- private String rowPrefix;
private BigtableDataClient dataClient;
+ private BigtableTableAdminClient tableAdminClient;
static DirectPathEnv create() {
return new DirectPathEnv(
@@ -75,17 +63,16 @@ static DirectPathEnv create() {
getRequiredProperty(TABLE_PROPERTY_NAME));
}
- public DirectPathEnv(
+ private DirectPathEnv(
boolean directPathEnabled, String projectId, String instanceId, String tableId) {
this.directPathEnabled = directPathEnabled;
this.projectId = projectId;
this.instanceId = instanceId;
this.tableId = tableId;
- this.rowPrefix = UUID.randomUUID() + "-";
}
@Override
- public void start() throws IOException {
+ void start() throws IOException {
BigtableDataSettings.Builder settingsBuilder =
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId);
@@ -105,12 +92,13 @@ public void start() throws IOException {
}
dataClient = BigtableDataClient.create(settingsBuilder.build());
+ tableAdminClient = BigtableTableAdminClient.create(projectId, instanceId);
}
@Override
- public void stop() throws Exception {
- deleteRows();
+ void stop() throws Exception {
dataClient.close();
+ tableAdminClient.close();
}
@Override
@@ -118,6 +106,11 @@ public BigtableDataClient getDataClient() {
return dataClient;
}
+ @Override
+ public BigtableTableAdminClient getTableAdminClient() {
+ return tableAdminClient;
+ }
+
@Override
public String getProjectId() {
return projectId;
@@ -133,30 +126,6 @@ public String getTableId() {
return tableId;
}
- @Override
- public String getFamilyId() {
- return FAMILY_ID;
- }
-
- @Override
- public String getRowPrefix() {
- return rowPrefix;
- }
-
- private void deleteRows() throws InterruptedException, ExecutionException, TimeoutException {
- Query query = Query.create(tableId).prefix(rowPrefix);
-
- List> futures = Lists.newArrayList();
- ServerStream rows = dataClient.readRows(query);
- for (Row row : rows) {
- ApiFuture future =
- dataClient.mutateRowAsync(RowMutation.create(tableId, row.getKey()).deleteRow());
- futures.add(future);
- }
-
- ApiFutures.allAsList(futures).get(10, TimeUnit.MINUTES);
- }
-
private static String getRequiredProperty(String prop) {
String value = System.getProperty(prop);
if (value == null || value.isEmpty()) {
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/EmulatorEnv.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/EmulatorEnv.java
similarity index 85%
rename from google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/EmulatorEnv.java
rename to google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/EmulatorEnv.java
index ab483423fb3f..352faf63c9cc 100644
--- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/EmulatorEnv.java
+++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/EmulatorEnv.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.google.cloud.bigtable.data.v2.it.env;
+package com.google.cloud.bigtable.test_helpers.env;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
@@ -22,18 +22,23 @@
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.emulator.v2.Emulator;
-public class EmulatorEnv implements TestEnv {
+public class EmulatorEnv extends AbstractTestEnv {
private static final String PROJECT_ID = "fake-project";
private static final String INSTANCE_ID = "fake-instance";
private static final String TABLE_ID = "default-table";
- private static final String FAMILY_ID = "cf";
private Emulator emulator;
private BigtableTableAdminClient tableAdminClient;
private BigtableDataClient dataClient;
+ public static EmulatorEnv createBundled() {
+ return new EmulatorEnv();
+ }
+
+ private EmulatorEnv() {}
+
@Override
- public void start() throws Exception {
+ void start() throws Exception {
emulator = Emulator.createBundled();
emulator.start();
@@ -50,11 +55,11 @@ public void start() throws Exception {
.setInstanceId("fake-instance")
.build());
- tableAdminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(FAMILY_ID));
+ tableAdminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(getFamilyId()));
}
@Override
- public void stop() throws Exception {
+ void stop() throws Exception {
tableAdminClient.close();
dataClient.close();
emulator.stop();
@@ -75,18 +80,13 @@ public String getTableId() {
return TABLE_ID;
}
- @Override
- public String getRowPrefix() {
- return "fake-";
- }
-
@Override
public BigtableDataClient getDataClient() {
return dataClient;
}
@Override
- public String getFamilyId() {
- return FAMILY_ID;
+ public BigtableTableAdminClient getTableAdminClient() {
+ return tableAdminClient;
}
}
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/ProdEnv.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/ProdEnv.java
similarity index 60%
rename from google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/ProdEnv.java
rename to google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/ProdEnv.java
index 6f3f333350dd..daecf9df7b56 100644
--- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/ProdEnv.java
+++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/ProdEnv.java
@@ -13,22 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.google.cloud.bigtable.data.v2.it.env;
+package com.google.cloud.bigtable.test_helpers.env;
-import com.google.api.core.ApiFuture;
-import com.google.api.core.ApiFutures;
-import com.google.api.gax.rpc.ServerStream;
+import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
-import com.google.cloud.bigtable.data.v2.models.Query;
-import com.google.cloud.bigtable.data.v2.models.Row;
-import com.google.cloud.bigtable.data.v2.models.RowMutation;
-import com.google.common.collect.Lists;
import java.io.IOException;
-import java.util.List;
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
/**
* Test environment that uses an existing bigtable table. The table must have a pre-existing family
@@ -40,7 +29,7 @@
* {@code bigtable.table}
*
*/
-public class ProdEnv implements TestEnv {
+class ProdEnv extends AbstractTestEnv {
private static final String PROJECT_PROPERTY_NAME = "bigtable.project";
private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance";
private static final String TABLE_PROPERTY_NAME = "bigtable.table";
@@ -48,10 +37,9 @@ public class ProdEnv implements TestEnv {
private final String projectId;
private final String instanceId;
private final String tableId;
- private static final String FAMILY_ID = "cf";
- private String rowPrefix;
private BigtableDataClient dataClient;
+ private BigtableTableAdminClient tableAdminClient;
static ProdEnv fromSystemProperties() {
return new ProdEnv(
@@ -60,22 +48,22 @@ static ProdEnv fromSystemProperties() {
getRequiredProperty(TABLE_PROPERTY_NAME));
}
- public ProdEnv(String projectId, String instanceId, String tableId) {
+ private ProdEnv(String projectId, String instanceId, String tableId) {
this.projectId = projectId;
this.instanceId = instanceId;
this.tableId = tableId;
- this.rowPrefix = UUID.randomUUID() + "-";
}
@Override
- public void start() throws IOException {
+ void start() throws IOException {
dataClient = BigtableDataClient.create(projectId, instanceId);
+ tableAdminClient = BigtableTableAdminClient.create(projectId, instanceId);
}
@Override
- public void stop() throws Exception {
- deleteRows();
+ void stop() throws Exception {
dataClient.close();
+ tableAdminClient.close();
}
@Override
@@ -83,6 +71,11 @@ public BigtableDataClient getDataClient() {
return dataClient;
}
+ @Override
+ public BigtableTableAdminClient getTableAdminClient() {
+ return tableAdminClient;
+ }
+
@Override
public String getProjectId() {
return projectId;
@@ -98,30 +91,6 @@ public String getTableId() {
return tableId;
}
- @Override
- public String getFamilyId() {
- return FAMILY_ID;
- }
-
- @Override
- public String getRowPrefix() {
- return rowPrefix;
- }
-
- private void deleteRows() throws InterruptedException, ExecutionException, TimeoutException {
- Query query = Query.create(tableId).prefix(rowPrefix);
-
- List> futures = Lists.newArrayList();
- ServerStream rows = dataClient.readRows(query);
- for (Row row : rows) {
- ApiFuture future =
- dataClient.mutateRowAsync(RowMutation.create(tableId, row.getKey()).deleteRow());
- futures.add(future);
- }
-
- ApiFutures.allAsList(futures).get(10, TimeUnit.MINUTES);
- }
-
private static String getRequiredProperty(String prop) {
String value = System.getProperty(prop);
if (value == null || value.isEmpty()) {
diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java
similarity index 90%
rename from google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java
rename to google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java
index 6e1484b541ce..2cd505f9fbec 100644
--- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/env/TestEnvRule.java
+++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package com.google.cloud.bigtable.data.v2.it.env;
+package com.google.cloud.bigtable.test_helpers.env;
import static com.google.common.truth.TruthJUnit.assume;
@@ -44,7 +44,7 @@ public class TestEnvRule extends ExternalResource {
private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";
private static final String ENV_PROPERTY = "bigtable.env";
- private TestEnv testEnv;
+ private AbstractTestEnv testEnv;
@Override
protected void before() throws Throwable {
@@ -58,7 +58,7 @@ protected void before() throws Throwable {
switch (env) {
case "emulator":
- testEnv = new EmulatorEnv();
+ testEnv = EmulatorEnv.createBundled();
break;
case "prod":
testEnv = ProdEnv.fromSystemProperties();
@@ -77,6 +77,12 @@ protected void before() throws Throwable {
@Override
protected void after() {
+ try {
+ env().cleanUpStale();
+ } catch (Exception e) {
+ LOGGER.log(Level.WARNING, "Failed to cleanup environment", e);
+ }
+
try {
testEnv.stop();
} catch (Exception e) {
@@ -85,7 +91,7 @@ protected void after() {
testEnv = null;
}
- public TestEnv env() {
+ public AbstractTestEnv env() {
return testEnv;
}
}