File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed
google-cloud-bigquerystorage/src
main/java/com/google/cloud/bigquery/storage/v1
test/java/com/google/cloud/bigquery/storage/v1 Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -353,7 +353,8 @@ public void run() {
353
353
} finally {
354
354
lock .unlock ();
355
355
}
356
- cleanupInflightRequests ();
356
+ cleanupConnectionAndRequests (
357
+ /* avoidBlocking= */ true ); // don't perform blocking operations while on user thread
357
358
});
358
359
this .appendThread .start ();
359
360
}
@@ -812,7 +813,10 @@ private void appendLoop() {
812
813
this .streamConnection .send (originalRequestBuilder .build ());
813
814
}
814
815
}
816
+ cleanupConnectionAndRequests (/* avoidBlocking= */ false );
817
+ }
815
818
819
+ private void cleanupConnectionAndRequests (boolean avoidBlocking ) {
816
820
log .info (
817
821
"Cleanup starts. Stream: "
818
822
+ streamName
@@ -828,7 +832,9 @@ private void appendLoop() {
828
832
// We can close the stream connection and handle the remaining inflight requests.
829
833
if (streamConnection != null ) {
830
834
this .streamConnection .close ();
831
- waitForDoneCallback (3 , TimeUnit .MINUTES );
835
+ if (!avoidBlocking ) {
836
+ waitForDoneCallback (3 , TimeUnit .MINUTES );
837
+ }
832
838
}
833
839
834
840
// At this point, there cannot be more callback. It is safe to clean up all inflight requests.
Original file line number Diff line number Diff line change @@ -650,14 +650,15 @@ public void testThrowExceptionWhileWithinAppendLoop_MaxWaitTimeExceed() throws E
650
650
null ,
651
651
client .getSettings (),
652
652
retrySettings );
653
- testBigQueryWrite .setResponseSleep (org .threeten .bp .Duration .ofSeconds (3 ));
653
+ org .threeten .bp .Duration durationSleep = org .threeten .bp .Duration .ofSeconds (2 );
654
+ testBigQueryWrite .setResponseSleep (durationSleep );
654
655
655
- long appendCount = 10 ;
656
+ long appendCount = 2 ;
656
657
for (int i = 0 ; i < appendCount ; i ++) {
657
658
testBigQueryWrite .addResponse (createAppendResponse (i ));
658
659
}
659
660
660
- // In total insert 5 requests,
661
+ // In total insert 'appendCount' requests,
661
662
List <ApiFuture <AppendRowsResponse >> futures = new ArrayList <>();
662
663
for (int i = 0 ; i < appendCount ; i ++) {
663
664
futures .add (
@@ -691,6 +692,18 @@ public void testThrowExceptionWhileWithinAppendLoop_MaxWaitTimeExceed() throws E
691
692
100 )
692
693
.get ());
693
694
assertThat (ex .getCause ()).hasMessageThat ().contains ("Request has waited in inflight queue" );
695
+
696
+ // Verify we can shutdown normally within the expected time.
697
+ long startCloseTime = System .currentTimeMillis ();
698
+ connectionWorker .close ();
699
+ long timeDiff = System .currentTimeMillis () - startCloseTime ;
700
+ assertTrue (
701
+ "timeDiff: "
702
+ + timeDiff
703
+ + " is more than total durationSleep: "
704
+ + (appendCount * durationSleep .toMillis ()),
705
+ timeDiff <= (appendCount * durationSleep .toMillis ()));
706
+ assertTrue (connectionWorker .isUserClosed ());
694
707
}
695
708
696
709
@ Test
You can’t perform that action at this time.
0 commit comments