3737import java .io .IOException ;
3838import java .util .List ;
3939import java .util .Map ;
40+ import java .util .concurrent .TimeUnit ;
4041import org .junit .AfterClass ;
4142import org .junit .AssumptionViolatedException ;
4243import org .junit .Before ;
@@ -48,6 +49,7 @@ public class BigtableTableAdminClientIT {
4849 private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance" ;
4950
5051 private static BigtableTableAdminClient tableAdmin ;
52+ private static String prefix ;
5153
5254 @ BeforeClass
5355 public static void createClient () throws IOException {
@@ -60,6 +62,17 @@ public static void createClient() throws IOException {
6062
6163 InstanceName instanceName = InstanceName .parse (targetInstance );
6264 tableAdmin = BigtableTableAdminClient .create (instanceName );
65+
66+ // Setup a prefix to avoid collisions between concurrent test runs
67+ prefix = String .format ("020%d" , System .currentTimeMillis ());
68+
69+ // Cleanup old tables, under normal circumstances this will do nothing
70+ String stalePrefix = String .format ("020%d" , System .currentTimeMillis () - TimeUnit .DAYS .toMillis (1 ));
71+ for (TableName tableName : tableAdmin .listTables ()) {
72+ if (stalePrefix .compareTo (tableName .getTable ()) > 0 ) {
73+ tableAdmin .deleteTable (tableName .getTable ());
74+ }
75+ }
6376 }
6477
6578 @ AfterClass
@@ -79,7 +92,7 @@ public void setup() {
7992
8093 @ Test
8194 public void createTable () {
82- String tableId = "adminCreateTest" ;
95+ String tableId = getTableId ( "adminCreateTest" ) ;
8396 CreateTableRequest createTableReq =
8497 CreateTableRequest .of (tableId )
8598 .addFamily ("cf1" )
@@ -110,7 +123,7 @@ public void createTable() {
110123
111124 @ Test
112125 public void modifyFamilies () {
113- String tableId = "adminModifyFamTest" ;
126+ String tableId = getTableId ( "adminModifyFamTest" ) ;
114127 ModifyColumnFamiliesRequest modifyFamiliesReq = ModifyColumnFamiliesRequest .of (tableId );
115128
116129 modifyFamiliesReq
@@ -179,14 +192,14 @@ public void modifyFamilies() {
179192
180193 @ Test
181194 public void deleteTable () {
182- String tableId = "adminDeleteTest" ;
195+ String tableId = getTableId ( "adminDeleteTest" ) ;
183196 tableAdmin .createTable (CreateTableRequest .of (tableId ));
184197 tableAdmin .deleteTable (tableId );
185198 }
186199
187200 @ Test
188201 public void getTable () {
189- String tableId = "adminGetTest" ;
202+ String tableId = getTableId ( "adminGetTest" ) ;
190203
191204 try {
192205 tableAdmin .createTable (CreateTableRequest .of (tableId ));
@@ -200,7 +213,7 @@ public void getTable() {
200213
201214 @ Test
202215 public void listTables () {
203- String tableId = "adminListTest" ;
216+ String tableId = getTableId ( "adminListTest" ) ;
204217
205218 try {
206219 tableAdmin .createTable (CreateTableRequest .of (tableId ));
@@ -214,7 +227,7 @@ public void listTables() {
214227
215228 @ Test
216229 public void listTablesAsync () throws Exception {
217- String tableId = "adminListTest" ;
230+ String tableId = getTableId ( "adminListTest" ) ;
218231
219232 try {
220233 tableAdmin .createTable (CreateTableRequest .of (tableId ));
@@ -228,7 +241,7 @@ public void listTablesAsync() throws Exception {
228241
229242 @ Test
230243 public void dropRowRange () {
231- String tableId = "adminDropRowrangeTest" ;
244+ String tableId = getTableId ( "adminDropRowrangeTest" ) ;
232245
233246 try {
234247 tableAdmin .createTable (CreateTableRequest .of (tableId ));
@@ -241,7 +254,7 @@ public void dropRowRange() {
241254
242255 @ Test
243256 public void awaitReplication () {
244- String tableId = "adminConsistencyTest" ;
257+ String tableId = getTableId ( "adminConsistencyTest" ) ;
245258
246259 try {
247260 tableAdmin .createTable (CreateTableRequest .of (tableId ));
@@ -250,4 +263,8 @@ public void awaitReplication() {
250263 tableAdmin .deleteTable (tableId );
251264 }
252265 }
266+
267+ private static String getTableId (String name ) {
268+ return prefix + "-" + name ;
269+ }
253270}
0 commit comments