@@ -425,35 +425,45 @@ public void dropTable_IfExists_ForNonExistingTable_ShouldNotThrowAnyException()
425425 @ Test
426426 public void truncateTable_ShouldTruncateProperly ()
427427 throws ExecutionException , TransactionException {
428- DistributedTransactionManager manager = null ;
428+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
429+ // executing DMLs
430+ String table = "table_for_truncate" ;
431+
429432 try {
430433 // Arrange
431- Key partitionKey = new Key (COL_NAME2 , "aaa" , COL_NAME1 , 1 );
432- Key clusteringKey = new Key (COL_NAME4 , 2 , COL_NAME3 , "bbb" );
433- manager = transactionFactory .getTransactionManager ();
434- manager .put (
435- new Put (partitionKey , clusteringKey )
436- .withValue (COL_NAME5 , 3 )
437- .withValue (COL_NAME6 , "ccc" )
438- .withValue (COL_NAME7 , 4L )
439- .withValue (COL_NAME8 , 1.0f )
440- .withValue (COL_NAME9 , 1.0d )
441- .withValue (COL_NAME10 , true )
442- .withValue (COL_NAME11 , "ddd" .getBytes (StandardCharsets .UTF_8 ))
443- .forNamespace (namespace1 )
444- .forTable (TABLE1 ));
434+ Map <String , String > options = getCreationOptions ();
435+ admin .createTable (namespace1 , table , TABLE_METADATA , true , options );
436+ Key partitionKey = Key .of (COL_NAME2 , "aaa" , COL_NAME1 , 1 );
437+ Key clusteringKey = Key .of (COL_NAME4 , 2 , COL_NAME3 , "bbb" );
438+ transactionalInsert (
439+ Insert .newBuilder ()
440+ .namespace (namespace1 )
441+ .table (table )
442+ .partitionKey (partitionKey )
443+ .clusteringKey (clusteringKey )
444+ .intValue (COL_NAME5 , 3 )
445+ .textValue (COL_NAME6 , "ccc" )
446+ .bigIntValue (COL_NAME7 , 4L )
447+ .floatValue (COL_NAME8 , 1.0f )
448+ .doubleValue (COL_NAME9 , 1.0d )
449+ .booleanValue (COL_NAME10 , true )
450+ .blobValue (COL_NAME11 , "ddd" .getBytes (StandardCharsets .UTF_8 ))
451+ .build ());
445452
446453 // Act
447- admin .truncateTable (namespace1 , TABLE1 );
454+ admin .truncateTable (namespace1 , table );
448455
449456 // Assert
450457 List <Result > results =
451- manager .scan (new Scan (partitionKey ).forNamespace (namespace1 ).forTable (TABLE1 ));
458+ transactionalScan (
459+ Scan .newBuilder ()
460+ .namespace (namespace1 )
461+ .table (table )
462+ .partitionKey (partitionKey )
463+ .build ());
452464 assertThat (results ).isEmpty ();
453465 } finally {
454- if (manager != null ) {
455- manager .close ();
456- }
466+ admin .dropTable (namespace1 , table , true );
457467 }
458468 }
459469
@@ -501,7 +511,10 @@ public void tableExists_ShouldReturnCorrectResults() throws ExecutionException {
501511 @ Test
502512 public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorrectly ()
503513 throws Exception {
504- DistributedTransactionManager transactionManager = null ;
514+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
515+ // executing DMLs
516+ String table = "table_for_create_index" ;
517+
505518 try {
506519 // Arrange
507520 Map <String , String > options = getCreationOptions ();
@@ -525,12 +538,11 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
525538 metadataBuilder = metadataBuilder .addColumn (COL_NAME13 , DataType .TIMESTAMP );
526539 }
527540 TableMetadata metadata = metadataBuilder .build ();
528- admin .createTable (namespace1 , TABLE4 , metadata , options );
529- transactionManager = transactionFactory .getTransactionManager ();
541+ admin .createTable (namespace1 , table , metadata , options );
530542 InsertBuilder .Buildable insert =
531543 Insert .newBuilder ()
532544 .namespace (namespace1 )
533- .table (TABLE4 )
545+ .table (table )
534546 .partitionKey (Key .ofInt (COL_NAME1 , 1 ))
535547 .intValue (COL_NAME2 , 2 )
536548 .textValue (COL_NAME3 , "3" )
@@ -551,45 +563,42 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
551563 COL_NAME13 ,
552564 LocalDateTime .of (LocalDate .of (2020 , 6 , 2 ), LocalTime .of (12 , 2 , 6 , 123_000_000 )));
553565 }
554- transactionManager . insert (insert .build ());
566+ transactionalInsert (insert .build ());
555567
556568 // Act
557- admin .createIndex (namespace1 , TABLE4 , COL_NAME2 , options );
558- admin .createIndex (namespace1 , TABLE4 , COL_NAME3 , options );
559- admin .createIndex (namespace1 , TABLE4 , COL_NAME4 , options );
560- admin .createIndex (namespace1 , TABLE4 , COL_NAME5 , options );
561- admin .createIndex (namespace1 , TABLE4 , COL_NAME6 , options );
562- if (isIndexOnBooleanColumnSupported ()) {
563- admin .createIndex (namespace1 , TABLE4 , COL_NAME7 , options );
564- }
565- admin .createIndex (namespace1 , TABLE4 , COL_NAME8 , options );
566- admin .createIndex (namespace1 , TABLE4 , COL_NAME10 , options );
567- admin .createIndex (namespace1 , TABLE4 , COL_NAME11 , options );
568- admin .createIndex (namespace1 , TABLE4 , COL_NAME12 , options );
569+ admin .createIndex (namespace1 , table , COL_NAME2 , options );
570+ admin .createIndex (namespace1 , table , COL_NAME3 , options );
571+ admin .createIndex (namespace1 , table , COL_NAME4 , options );
572+ admin .createIndex (namespace1 , table , COL_NAME5 , options );
573+ admin .createIndex (namespace1 , table , COL_NAME6 , options );
574+ admin .createIndex (namespace1 , table , COL_NAME8 , options );
575+ admin .createIndex (namespace1 , table , COL_NAME10 , options );
576+ admin .createIndex (namespace1 , table , COL_NAME11 , options );
577+ admin .createIndex (namespace1 , table , COL_NAME12 , options );
569578 if (isTimestampTypeSupported ()) {
570- admin .createIndex (namespace1 , TABLE4 , COL_NAME13 , options );
579+ admin .createIndex (namespace1 , table , COL_NAME13 , options );
571580 }
572581
573582 // Assert
574- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME2 )).isTrue ();
575- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME3 )).isTrue ();
576- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME4 )).isTrue ();
577- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME5 )).isTrue ();
578- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME6 )).isTrue ();
583+ assertThat (admin .indexExists (namespace1 , table , COL_NAME2 )).isTrue ();
584+ assertThat (admin .indexExists (namespace1 , table , COL_NAME3 )).isTrue ();
585+ assertThat (admin .indexExists (namespace1 , table , COL_NAME4 )).isTrue ();
586+ assertThat (admin .indexExists (namespace1 , table , COL_NAME5 )).isTrue ();
587+ assertThat (admin .indexExists (namespace1 , table , COL_NAME6 )).isTrue ();
579588 if (isIndexOnBooleanColumnSupported ()) {
580- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME7 )).isTrue ();
589+ assertThat (admin .indexExists (namespace1 , table , COL_NAME7 )).isTrue ();
581590 }
582- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME8 )).isTrue ();
583- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME9 )).isTrue ();
584- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME10 )).isTrue ();
585- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME11 )).isTrue ();
586- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME12 )).isTrue ();
591+ assertThat (admin .indexExists (namespace1 , table , COL_NAME8 )).isTrue ();
592+ assertThat (admin .indexExists (namespace1 , table , COL_NAME9 )).isTrue ();
593+ assertThat (admin .indexExists (namespace1 , table , COL_NAME10 )).isTrue ();
594+ assertThat (admin .indexExists (namespace1 , table , COL_NAME11 )).isTrue ();
595+ assertThat (admin .indexExists (namespace1 , table , COL_NAME12 )).isTrue ();
587596 if (isTimestampTypeSupported ()) {
588- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME13 )).isTrue ();
597+ assertThat (admin .indexExists (namespace1 , table , COL_NAME13 )).isTrue ();
589598 }
590599
591600 Set <String > actualSecondaryIndexNames =
592- admin .getTableMetadata (namespace1 , TABLE4 ).getSecondaryIndexNames ();
601+ admin .getTableMetadata (namespace1 , table ).getSecondaryIndexNames ();
593602 assertThat (actualSecondaryIndexNames )
594603 .contains (
595604 COL_NAME2 ,
@@ -612,12 +621,8 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
612621 indexCount ++;
613622 }
614623 assertThat (actualSecondaryIndexNames ).hasSize (indexCount );
615-
616624 } finally {
617- admin .dropTable (namespace1 , TABLE4 , true );
618- if (transactionManager != null ) {
619- transactionManager .close ();
620- }
625+ admin .dropTable (namespace1 , table , true );
621626 }
622627 }
623628
@@ -692,11 +697,14 @@ public void createIndex_IfNotExists_ForAlreadyExistingIndex_ShouldNotThrowAnyExc
692697 @ Test
693698 public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly ()
694699 throws Exception {
695- DistributedTransactionManager transactionManager = null ;
700+ // Use a separate table name to avoid hitting the stale cache, which can cause test failure when
701+ // executing DMLs
702+ String table = "table_for_drop_index" ;
703+
696704 try {
697705 // Arrange
698706 Map <String , String > options = getCreationOptions ();
699- TableMetadata metadata =
707+ TableMetadata . Builder metadataBuilder =
700708 TableMetadata .newBuilder ()
701709 .addColumn (COL_NAME1 , DataType .INT )
702710 .addColumn (COL_NAME2 , DataType .INT )
@@ -707,6 +715,9 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
707715 .addColumn (COL_NAME7 , DataType .BOOLEAN )
708716 .addColumn (COL_NAME8 , DataType .BLOB )
709717 .addColumn (COL_NAME9 , DataType .TEXT )
718+ .addColumn (COL_NAME10 , DataType .DATE )
719+ .addColumn (COL_NAME11 , DataType .TIME )
720+ .addColumn (COL_NAME12 , DataType .TIMESTAMPTZ )
710721 .addPartitionKey (COL_NAME1 )
711722 .addSecondaryIndex (COL_NAME2 )
712723 .addSecondaryIndex (COL_NAME3 )
@@ -716,16 +727,22 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
716727 .addSecondaryIndex (COL_NAME8 )
717728 .addSecondaryIndex (COL_NAME9 )
718729 .addSecondaryIndex (COL_NAME9 )
719- .build ();
730+ .addSecondaryIndex (COL_NAME10 )
731+ .addSecondaryIndex (COL_NAME11 )
732+ .addSecondaryIndex (COL_NAME12 );
720733 if (isIndexOnBooleanColumnSupported ()) {
721- metadata = TableMetadata . newBuilder ( metadata ). addSecondaryIndex (COL_NAME7 ). build ( );
734+ metadataBuilder = metadataBuilder . addSecondaryIndex (COL_NAME7 );
722735 }
723- admin .createTable (namespace1 , TABLE4 , metadata , options );
724- transactionManager = transactionFactory .getTransactionManager ();
725- transactionManager .put (
726- Put .newBuilder ()
736+ if (isTimestampTypeSupported ()) {
737+ metadataBuilder .addColumn (COL_NAME13 , DataType .TIMESTAMP );
738+ metadataBuilder .addSecondaryIndex (COL_NAME13 );
739+ }
740+ admin .createTable (namespace1 , table , metadataBuilder .build (), options );
741+
742+ InsertBuilder .Buildable insert =
743+ Insert .newBuilder ()
727744 .namespace (namespace1 )
728- .table (TABLE4 )
745+ .table (table )
729746 .partitionKey (Key .ofInt (COL_NAME1 , 1 ))
730747 .intValue (COL_NAME2 , 2 )
731748 .textValue (COL_NAME3 , "3" )
@@ -735,34 +752,53 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
735752 .booleanValue (COL_NAME7 , true )
736753 .blobValue (COL_NAME8 , "8" .getBytes (StandardCharsets .UTF_8 ))
737754 .textValue (COL_NAME9 , "9" )
738- .build ());
755+ .timeValue (COL_NAME11 , LocalTime .of (12 , 2 , 6 , 123_456_000 ))
756+ .timestampTZValue (
757+ COL_NAME12 ,
758+ LocalDateTime .of (LocalDate .of (2020 , 6 , 2 ), LocalTime .of (12 , 2 , 6 , 123_000_000 ))
759+ .toInstant (ZoneOffset .UTC ));
760+ if (isTimestampTypeSupported ()) {
761+ insert .timestampValue (
762+ COL_NAME13 ,
763+ LocalDateTime .of (LocalDate .of (2020 , 6 , 2 ), LocalTime .of (12 , 2 , 6 , 123_000_000 )));
764+ }
765+ transactionalInsert (insert .build ());
739766
740767 // Act
741- admin .dropIndex (namespace1 , TABLE4 , COL_NAME2 );
742- admin .dropIndex (namespace1 , TABLE4 , COL_NAME3 );
743- admin .dropIndex (namespace1 , TABLE4 , COL_NAME4 );
744- admin .dropIndex (namespace1 , TABLE4 , COL_NAME5 );
745- admin .dropIndex (namespace1 , TABLE4 , COL_NAME6 );
768+ admin .dropIndex (namespace1 , table , COL_NAME2 );
769+ admin .dropIndex (namespace1 , table , COL_NAME3 );
770+ admin .dropIndex (namespace1 , table , COL_NAME4 );
771+ admin .dropIndex (namespace1 , table , COL_NAME5 );
772+ admin .dropIndex (namespace1 , table , COL_NAME6 );
746773 if (isIndexOnBooleanColumnSupported ()) {
747- admin .dropIndex (namespace1 , TABLE4 , COL_NAME7 );
774+ admin .dropIndex (namespace1 , table , COL_NAME7 );
775+ }
776+ admin .dropIndex (namespace1 , table , COL_NAME8 );
777+ admin .dropIndex (namespace1 , table , COL_NAME10 );
778+ admin .dropIndex (namespace1 , table , COL_NAME11 );
779+ admin .dropIndex (namespace1 , table , COL_NAME12 );
780+ if (isTimestampTypeSupported ()) {
781+ admin .dropIndex (namespace1 , table , COL_NAME13 );
748782 }
749- admin .dropIndex (namespace1 , TABLE4 , COL_NAME8 );
750783
751784 // Assert
752- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME2 )).isFalse ();
753- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME3 )).isFalse ();
754- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME4 )).isFalse ();
755- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME5 )).isFalse ();
756- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME6 )).isFalse ();
757- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME7 )).isFalse ();
758- assertThat (admin .indexExists (namespace1 , TABLE4 , COL_NAME8 )).isFalse ();
759- assertThat (admin .getTableMetadata (namespace1 , TABLE4 ).getSecondaryIndexNames ())
785+ assertThat (admin .indexExists (namespace1 , table , COL_NAME2 )).isFalse ();
786+ assertThat (admin .indexExists (namespace1 , table , COL_NAME3 )).isFalse ();
787+ assertThat (admin .indexExists (namespace1 , table , COL_NAME4 )).isFalse ();
788+ assertThat (admin .indexExists (namespace1 , table , COL_NAME5 )).isFalse ();
789+ assertThat (admin .indexExists (namespace1 , table , COL_NAME6 )).isFalse ();
790+ assertThat (admin .indexExists (namespace1 , table , COL_NAME7 )).isFalse ();
791+ assertThat (admin .indexExists (namespace1 , table , COL_NAME8 )).isFalse ();
792+ assertThat (admin .getTableMetadata (namespace1 , table ).getSecondaryIndexNames ())
760793 .containsOnly (COL_NAME9 );
761- } finally {
762- admin .dropTable (namespace1 , TABLE4 , true );
763- if (transactionManager != null ) {
764- transactionManager .close ();
794+ assertThat (admin .indexExists (namespace1 , table , COL_NAME10 )).isFalse ();
795+ assertThat (admin .indexExists (namespace1 , table , COL_NAME11 )).isFalse ();
796+ assertThat (admin .indexExists (namespace1 , table , COL_NAME12 )).isFalse ();
797+ if (isTimestampTypeSupported ()) {
798+ assertThat (admin .indexExists (namespace1 , table , COL_NAME13 )).isFalse ();
765799 }
800+ } finally {
801+ admin .dropTable (namespace1 , table , true );
766802 }
767803 }
768804
@@ -969,4 +1005,8 @@ protected boolean isIndexOnBooleanColumnSupported() {
9691005 protected boolean isTimestampTypeSupported () {
9701006 return true ;
9711007 }
1008+
1009+ protected abstract void transactionalInsert (Insert insert ) throws TransactionException ;
1010+
1011+ protected abstract List <Result > transactionalScan (Scan scan ) throws TransactionException ;
9721012}
0 commit comments