11from __future__ import absolute_import , print_function
22
33import sys
4+ import time
45from typing import List , Optional
56
67from globus_sdk import TransferAPIError , TransferClient , TransferData
2627task_id = None
2728archive_directory_listing : IterableTransferResponse = None
2829
30+ DEBUG_LONG_TRANSFER : bool = False # Set to true if testing token expiration handling
31+
32+
33+ def _debug_sleep_to_expire_token (context : str , retry_count : int = 0 ):
34+ """
35+ FOR DEBUGGING ONLY: Sleep to simulate token expiration during long operations.
36+
37+ Args:
38+ context: Description of where this is being called (e.g., "blocking", "non-blocking")
39+ retry_count: Current retry count (only sleep on first iteration)
40+ """
41+ if DEBUG_LONG_TRANSFER and retry_count == 0 :
42+ transfer_duration_mock_hours = 49
43+ logger .info (
44+ f"{ ts_utc ()} : TESTING ({ context } ): Sleeping for { transfer_duration_mock_hours } hours to let access token expire"
45+ )
46+ time .sleep (transfer_duration_mock_hours * 3600 )
47+ logger .info (
48+ f"{ ts_utc ()} : TESTING ({ context } ): Woke up after { transfer_duration_mock_hours } hours. "
49+ "Access token expired, RefreshTokenAuthorizer should automatically refresh on next API call."
50+ )
51+ return True # Indicates sleep happened
52+ return False # No sleep
53+
2954
3055def globus_activate (hpss : str ):
3156 """
@@ -88,6 +113,9 @@ def globus_transfer( # noqa: C901
88113 # Make a simple API call to trigger refresh if needed
89114 transfer_client .endpoint_autoactivate (local_endpoint , if_expires_in = 86400 )
90115 transfer_client .endpoint_autoactivate (remote_endpoint , if_expires_in = 86400 )
116+
117+ # FOR DEBUGGING: Test non-blocking mode token refresh
118+ _debug_sleep_to_expire_token ("non-blocking" )
91119 except Exception as e :
92120 logger .warning (f"Token refresh check: { e } " )
93121
@@ -214,6 +242,7 @@ def globus_block_wait(
214242 # Refresh token before each wait attempt
215243 transfer_client .endpoint_autoactivate (local_endpoint , if_expires_in = 86400 )
216244 transfer_client .endpoint_autoactivate (remote_endpoint , if_expires_in = 86400 )
245+
217246 logger .info (
218247 f"{ ts_utc ()} : on task_wait try { retry_count + 1 } out of { max_retries } "
219248 )
@@ -226,6 +255,11 @@ def globus_block_wait(
226255 else :
227256 curr_task = transfer_client .get_task (task_id )
228257 task_status = curr_task ["status" ]
258+
259+ # FOR DEBUGGING: Test blocking mode token refresh
260+ if _debug_sleep_to_expire_token ("blocking" , retry_count ):
261+ continue # Force another iteration to test refresh
262+
229263 if task_status == "SUCCEEDED" :
230264 break
231265 finally :
0 commit comments