@@ -682,77 +682,57 @@ public void testRollbackToSavepointWithoutInternalRetriesInReadOnlyTransaction()
682
682
683
683
@ Test
684
684
public void testKeepAlive () throws InterruptedException , TimeoutException {
685
+ String keepAliveTag = "test_keep_alive_tag" ;
685
686
System .setProperty ("spanner.connection.keep_alive_interval_millis" , "1" );
687
+ System .setProperty ("spanner.connection.keep_alive_query_tag" , keepAliveTag );
686
688
try (Connection connection = createConnection ()) {
687
689
connection .setSavepointSupport (SavepointSupport .ENABLED );
688
690
connection .setKeepTransactionAlive (true );
689
691
// Start a transaction by executing a statement.
690
692
connection .execute (INSERT_STATEMENT );
691
693
// Verify that we get a keep-alive request.
692
- verifyHasKeepAliveRequest ();
694
+ verifyHasKeepAliveRequest (keepAliveTag );
693
695
// Set a savepoint, execute another statement, and rollback to the savepoint.
694
696
// The keep-alive should not be sent after the transaction has been rolled back to the
695
697
// savepoint.
696
698
connection .savepoint ("s1" );
697
699
connection .execute (INSERT_STATEMENT );
698
700
connection .rollbackToSavepoint ("s1" );
699
701
mockSpanner .waitForRequestsToContain (RollbackRequest .class , 1000L );
700
- // Wait for up to 2 milliseconds to make sure that any keep-alive requests that were in flight
701
- // have finished.
702
- try {
703
- mockSpanner .waitForRequestsToContain (
704
- r -> {
705
- if (!(r instanceof ExecuteSqlRequest )) {
706
- return false ;
707
- }
708
- ExecuteSqlRequest request = (ExecuteSqlRequest ) r ;
709
- return request .getSql ().equals ("SELECT 1" )
710
- && request
711
- .getRequestOptions ()
712
- .getRequestTag ()
713
- .equals ("connection.transaction-keep-alive" );
714
- },
715
- 2L );
716
- } catch (TimeoutException ignore ) {
717
- }
702
+ String keepAliveTagAfterRollback = "test_keep_alive_tag_after_rollback" ;
703
+ System .setProperty ("spanner.connection.keep_alive_query_tag" , keepAliveTagAfterRollback );
718
704
719
- // Verify that we don't get any keep-alive requests from this point.
720
- mockSpanner .clearRequests ();
705
+ // Verify that we don't get any new keep-alive requests from this point.
721
706
Thread .sleep (2L );
722
- assertEquals (0 , countKeepAliveRequest ());
707
+ assertEquals (0 , countKeepAliveRequest (keepAliveTagAfterRollback ));
723
708
// Resume the transaction and verify that we get a keep-alive again.
724
709
connection .execute (INSERT_STATEMENT );
725
- verifyHasKeepAliveRequest ();
710
+ verifyHasKeepAliveRequest (keepAliveTagAfterRollback );
726
711
} finally {
727
712
System .clearProperty ("spanner.connection.keep_alive_interval_millis" );
713
+ System .clearProperty ("spanner.connection.keep_alive_query_tag" );
728
714
}
729
715
}
730
716
731
- private void verifyHasKeepAliveRequest () throws InterruptedException , TimeoutException {
717
+ private void verifyHasKeepAliveRequest (String tag ) throws InterruptedException , TimeoutException {
732
718
mockSpanner .waitForRequestsToContain (
733
719
r -> {
734
720
if (!(r instanceof ExecuteSqlRequest )) {
735
721
return false ;
736
722
}
737
723
ExecuteSqlRequest request = (ExecuteSqlRequest ) r ;
738
724
return request .getSql ().equals ("SELECT 1" )
739
- && request
740
- .getRequestOptions ()
741
- .getRequestTag ()
742
- .equals ("connection.transaction-keep-alive" );
725
+ && request .getRequestOptions ().getRequestTag ().equals (tag );
743
726
},
744
727
1000L );
745
728
}
746
729
747
- private long countKeepAliveRequest () {
730
+ private long countKeepAliveRequest (String tag ) {
748
731
return mockSpanner .getRequestsOfType (ExecuteSqlRequest .class ).stream ()
749
732
.filter (
750
733
request ->
751
734
request .getSql ().equals ("SELECT 1" )
752
- && request
753
- .getRequestOptions ()
754
- .getRequestTag ()
755
- .equals ("connection.transaction-keep-alive" ))
735
+ && request .getRequestOptions ().getRequestTag ().equals (tag ))
756
736
.count ();
757
737
}
758
738
}
0 commit comments