@@ -391,35 +391,45 @@ public void dropTable_IfExists_ForNonExistingTable_ShouldNotThrowAnyException()
391391 @ Test
392392 public void truncateTable_ShouldTruncateProperly ()
393393 throws ExecutionException , TransactionException {
394- DistributedTransactionManager manager = null ;
394+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
395+ // executing DMLs
396+ String table = "table_for_truncate" ;
397+
395398 try {
396399 // Arrange
397- Key partitionKey = new Key (COL_NAME2 , "aaa" , COL_NAME1 , 1 );
398- Key clusteringKey = new Key (COL_NAME4 , 2 , COL_NAME3 , "bbb" );
399- manager = transactionFactory .getTransactionManager ();
400- manager .put (
401- new Put (partitionKey , clusteringKey )
402- .withValue (COL_NAME5 , 3 )
403- .withValue (COL_NAME6 , "ccc" )
404- .withValue (COL_NAME7 , 4L )
405- .withValue (COL_NAME8 , 1.0f )
406- .withValue (COL_NAME9 , 1.0d )
407- .withValue (COL_NAME10 , true )
408- .withValue (COL_NAME11 , "ddd" .getBytes (StandardCharsets .UTF_8 ))
409- .forNamespace (namespace1 )
410- .forTable (TABLE1 ));
400+ Map <String , String > options = getCreationOptions ();
401+ admin .createTable (namespace1 , table , TABLE_METADATA , true , options );
402+ Key partitionKey = Key .of (COL_NAME2 , "aaa" , COL_NAME1 , 1 );
403+ Key clusteringKey = Key .of (COL_NAME4 , 2 , COL_NAME3 , "bbb" );
404+ transactionalInsert (
405+ Insert .newBuilder ()
406+ .namespace (namespace1 )
407+ .table (table )
408+ .partitionKey (partitionKey )
409+ .clusteringKey (clusteringKey )
410+ .intValue (COL_NAME5 , 3 )
411+ .textValue (COL_NAME6 , "ccc" )
412+ .bigIntValue (COL_NAME7 , 4L )
413+ .floatValue (COL_NAME8 , 1.0f )
414+ .doubleValue (COL_NAME9 , 1.0d )
415+ .booleanValue (COL_NAME10 , true )
416+ .blobValue (COL_NAME11 , "ddd" .getBytes (StandardCharsets .UTF_8 ))
417+ .build ());
411418
412419 // Act
413- admin .truncateTable (namespace1 , TABLE1 );
420+ admin .truncateTable (namespace1 , table );
414421
415422 // Assert
416423 List <Result > results =
417- manager .scan (new Scan (partitionKey ).forNamespace (namespace1 ).forTable (TABLE1 ));
424+ transactionalScan (
425+ Scan .newBuilder ()
426+ .namespace (namespace1 )
427+ .table (table )
428+ .partitionKey (partitionKey )
429+ .build ());
418430 assertThat (results ).isEmpty ();
419431 } finally {
420- if (manager != null ) {
421- manager .close ();
422- }
432+ admin .dropTable (namespace1 , table , true );
423433 }
424434 }
425435
@@ -467,7 +477,10 @@ public void tableExists_ShouldReturnCorrectResults() throws ExecutionException {
467477 @ Test
468478 public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorrectly ()
469479 throws Exception {
470- DistributedTransactionManager transactionManager = null ;
480+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
481+ // executing DMLs
482+ String table = "table_for_create_index" ;
483+
471484 try {
472485 // Arrange
473486 Map <String , String > options = getCreationOptions ();
@@ -485,12 +498,11 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
485498 .addPartitionKey (COL_NAME1 )
486499 .addSecondaryIndex (COL_NAME9 )
487500 .build ();
488- admin .createTable (namespace1 , TABLE4 , metadata , options );
489- transactionManager = transactionFactory .getTransactionManager ();
490- transactionManager .put (
491- Put .newBuilder ()
501+ admin .createTable (namespace1 , table , metadata , options );
502+ Insert insert =
503+ Insert .newBuilder ()
492504 .namespace (namespace1 )
493- .table (TABLE4 )
505+ .table (table )
494506 .partitionKey (Key .ofInt (COL_NAME1 , 1 ))
495507 .intValue (COL_NAME2 , 2 )
496508 .textValue (COL_NAME3 , "3" )
@@ -500,45 +512,43 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
500512 .booleanValue (COL_NAME7 , true )
501513 .blobValue (COL_NAME8 , "8" .getBytes (StandardCharsets .UTF_8 ))
502514 .textValue (COL_NAME9 , "9" )
503- .build ());
515+ .build ();
516+ transactionalInsert (insert );
504517
505518 // Act
506- admin .createIndex (namespace1 , TABLE4 , COL_NAME2 , options );
507- admin .createIndex (namespace1 , TABLE4 , COL_NAME3 , options );
508- admin .createIndex (namespace1 , TABLE4 , COL_NAME4 , options );
509- admin .createIndex (namespace1 , TABLE4 , COL_NAME5 , options );
510- admin .createIndex (namespace1 , TABLE4 , COL_NAME6 , options );
519+ admin .createIndex (namespace1 , table , COL_NAME2 , options );
520+ admin .createIndex (namespace1 , table , COL_NAME3 , options );
521+ admin .createIndex (namespace1 , table , COL_NAME4 , options );
522+ admin .createIndex (namespace1 , table , COL_NAME5 , options );
523+ admin .createIndex (namespace1 , table , COL_NAME6 , options );
511524 if (isIndexOnBooleanColumnSupported ()) {
512- admin .createIndex (namespace1 , TABLE4 , COL_NAME7 , options );
525+ admin .createIndex (namespace1 , table , COL_NAME7 , options );
513526 }
514- admin .createIndex (namespace1 , TABLE4 , COL_NAME8 , options );
527+ admin .createIndex (namespace1 , table , COL_NAME8 , options );
515528
516529 // Assert
517- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME2 )).isTrue ();
518- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME3 )).isTrue ();
519- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME4 )).isTrue ();
520- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME5 )).isTrue ();
521- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME6 )).isTrue ();
530+ assertThat (admin .indexExists (namespace1 , table , COL_NAME2 )).isTrue ();
531+ assertThat (admin .indexExists (namespace1 , table , COL_NAME3 )).isTrue ();
532+ assertThat (admin .indexExists (namespace1 , table , COL_NAME4 )).isTrue ();
533+ assertThat (admin .indexExists (namespace1 , table , COL_NAME5 )).isTrue ();
534+ assertThat (admin .indexExists (namespace1 , table , COL_NAME6 )).isTrue ();
522535 if (isIndexOnBooleanColumnSupported ()) {
523- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME7 )).isTrue ();
536+ assertThat (admin .indexExists (namespace1 , table , COL_NAME7 )).isTrue ();
524537 }
525- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME8 )).isTrue ();
538+ assertThat (admin .indexExists (namespace1 , table , COL_NAME8 )).isTrue ();
526539 if (isIndexOnBooleanColumnSupported ()) {
527- assertThat (admin .getTableMetadata (namespace1 , TABLE4 ).getSecondaryIndexNames ())
540+ assertThat (admin .getTableMetadata (namespace1 , table ).getSecondaryIndexNames ())
528541 .containsOnly (
529542 COL_NAME2 , COL_NAME3 , COL_NAME4 , COL_NAME5 , COL_NAME6 , COL_NAME7 , COL_NAME8 ,
530543 COL_NAME9 );
531544 } else {
532- assertThat (admin .getTableMetadata (namespace1 , TABLE4 ).getSecondaryIndexNames ())
545+ assertThat (admin .getTableMetadata (namespace1 , table ).getSecondaryIndexNames ())
533546 .containsOnly (
534547 COL_NAME2 , COL_NAME3 , COL_NAME4 , COL_NAME5 , COL_NAME6 , COL_NAME8 , COL_NAME9 );
535548 }
536549
537550 } finally {
538- admin .dropTable (namespace1 , TABLE4 , true );
539- if (transactionManager != null ) {
540- transactionManager .close ();
541- }
551+ admin .dropTable (namespace1 , table , true );
542552 }
543553 }
544554
@@ -613,7 +623,10 @@ public void createIndex_IfNotExists_ForAlreadyExistingIndex_ShouldNotThrowAnyExc
613623 @ Test
614624 public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly ()
615625 throws Exception {
616- DistributedTransactionManager transactionManager = null ;
626+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
627+ // executing DMLs
628+ String table = "table_for_drop_index" ;
629+
617630 try {
618631 // Arrange
619632 Map <String , String > options = getCreationOptions ();
@@ -641,12 +654,11 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
641654 if (isIndexOnBooleanColumnSupported ()) {
642655 metadata = TableMetadata .newBuilder (metadata ).addSecondaryIndex (COL_NAME7 ).build ();
643656 }
644- admin .createTable (namespace1 , TABLE4 , metadata , options );
645- transactionManager = transactionFactory .getTransactionManager ();
646- transactionManager .put (
647- Put .newBuilder ()
657+ admin .createTable (namespace1 , table , metadata , options );
658+ Insert insert =
659+ Insert .newBuilder ()
648660 .namespace (namespace1 )
649- .table (TABLE4 )
661+ .table (table )
650662 .partitionKey (Key .ofInt (COL_NAME1 , 1 ))
651663 .intValue (COL_NAME2 , 2 )
652664 .textValue (COL_NAME3 , "3" )
@@ -656,34 +668,32 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
656668 .booleanValue (COL_NAME7 , true )
657669 .blobValue (COL_NAME8 , "8" .getBytes (StandardCharsets .UTF_8 ))
658670 .textValue (COL_NAME9 , "9" )
659- .build ());
671+ .build ();
672+ transactionalInsert (insert );
660673
661674 // Act
662- admin .dropIndex (namespace1 , TABLE4 , COL_NAME2 );
663- admin .dropIndex (namespace1 , TABLE4 , COL_NAME3 );
664- admin .dropIndex (namespace1 , TABLE4 , COL_NAME4 );
665- admin .dropIndex (namespace1 , TABLE4 , COL_NAME5 );
666- admin .dropIndex (namespace1 , TABLE4 , COL_NAME6 );
675+ admin .dropIndex (namespace1 , table , COL_NAME2 );
676+ admin .dropIndex (namespace1 , table , COL_NAME3 );
677+ admin .dropIndex (namespace1 , table , COL_NAME4 );
678+ admin .dropIndex (namespace1 , table , COL_NAME5 );
679+ admin .dropIndex (namespace1 , table , COL_NAME6 );
667680 if (isIndexOnBooleanColumnSupported ()) {
668- admin .dropIndex (namespace1 , TABLE4 , COL_NAME7 );
681+ admin .dropIndex (namespace1 , table , COL_NAME7 );
669682 }
670- admin .dropIndex (namespace1 , TABLE4 , COL_NAME8 );
683+ admin .dropIndex (namespace1 , table , COL_NAME8 );
671684
672685 // Assert
673- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME2 )).isFalse ();
674- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME3 )).isFalse ();
675- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME4 )).isFalse ();
676- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME5 )).isFalse ();
677- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME6 )).isFalse ();
678- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME7 )).isFalse ();
679- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME8 )).isFalse ();
680- assertThat (admin .getTableMetadata (namespace1 , TABLE4 ).getSecondaryIndexNames ())
686+ assertThat (admin .indexExists (namespace1 , table , COL_NAME2 )).isFalse ();
687+ assertThat (admin .indexExists (namespace1 , table , COL_NAME3 )).isFalse ();
688+ assertThat (admin .indexExists (namespace1 , table , COL_NAME4 )).isFalse ();
689+ assertThat (admin .indexExists (namespace1 , table , COL_NAME5 )).isFalse ();
690+ assertThat (admin .indexExists (namespace1 , table , COL_NAME6 )).isFalse ();
691+ assertThat (admin .indexExists (namespace1 , table , COL_NAME7 )).isFalse ();
692+ assertThat (admin .indexExists (namespace1 , table , COL_NAME8 )).isFalse ();
693+ assertThat (admin .getTableMetadata (namespace1 , table ).getSecondaryIndexNames ())
681694 .containsOnly (COL_NAME9 );
682695 } finally {
683- admin .dropTable (namespace1 , TABLE4 , true );
684- if (transactionManager != null ) {
685- transactionManager .close ();
686- }
696+ admin .dropTable (namespace1 , table , true );
687697 }
688698 }
689699
@@ -886,4 +896,8 @@ public void getNamespaceNames_ShouldReturnCreatedNamespaces() throws ExecutionEx
886896 protected boolean isIndexOnBooleanColumnSupported () {
887897 return true ;
888898 }
899+
900+ protected abstract void transactionalInsert (Insert insert ) throws TransactionException ;
901+
902+ protected abstract List <Result > transactionalScan (Scan scan ) throws TransactionException ;
889903}
0 commit comments