2222
2323# Flags to keep between requests
2424SHOULD_THROTTLE = True
25- SHOULD_SKIP_WAIT = True
2625RETRY_REQUEST_COUNT = 0
2726
2827
@@ -55,16 +54,37 @@ class ResponseConfig:
5554 disconnect_after_headers = False
5655 generate_body_size : Optional [int ] = None
5756 json_path : str = None
58- throttle : bool = False
57+ forced_throttle : bool = False
5958 force_retry : bool = False
59+ should_skip_wait = False
6060 request_headers : Optional [List [Tuple [bytes , bytes ]]] = None
6161
62+ def __init__ (self , path , request = None ):
63+ self .path = path
64+ self .request_headers = request .headers
65+ self .json_path = None
66+ self .generate_body_size = None
67+ self .disconnect_after_headers = False
68+ self .forced_throttle = False
69+ self .force_retry = False
70+ self .should_skip_wait = False
71+
72+ if get_request_header_value (request , "before_finish" ) is not None :
73+ self .should_skip_wait = True
74+ if get_request_header_value (request , "force_throttle" ) is not None :
75+ self .forced_throttle = True
76+
6277 def _resolve_file_path (self , wrapper , request_type ):
6378 global SHOULD_THROTTLE
6479 if self .json_path is None :
80+ if self .forced_throttle :
81+ # force the throttle to happend, instead of just 50%.
82+ response_file = os .path .join (
83+ base_dir , request_type .name , f"throttle.json" )
84+ self .json_path = response_file
85+ return
6586 response_file = os .path .join (
6687 base_dir , request_type .name , f"{ self .path [1 :]} .json" )
67- print (response_file )
6888 if os .path .exists (response_file ) == False :
6989 wrapper .info (
7090 response_file , "not exist, using the default response" )
@@ -77,7 +97,6 @@ def _resolve_file_path(self, wrapper, request_type):
7797 response_file = os .path .join (
7898 base_dir , request_type .name , f"default.json" )
7999 else :
80- print ("throttle" )
81100 wrapper .info ("Throttling" )
82101 # Flip the flag
83102 SHOULD_THROTTLE = not SHOULD_THROTTLE
@@ -483,27 +502,21 @@ async def handle_mock_s3_request(wrapper, request):
483502 # TODO: support more type.
484503 wrapper .info ("unsupported request:" , request )
485504 request_type = S3Opts .CreateMultipartUpload
486- global SHOULD_SKIP_WAIT
487- if "before_finish" not in parsed_path .path or request_type is not S3Opts .UploadPart :
488- # restore the state
489- SHOULD_SKIP_WAIT = True
490- if "before_finish" in parsed_path .path and SHOULD_SKIP_WAIT and request_type is S3Opts .UploadPart :
491- print ("not wait for the request to complete" )
492- SHOULD_SKIP_WAIT = False
493- else :
505+
506+ if response_config is None :
507+ response_config = ResponseConfig (parsed_path .path , request )
508+
509+ if not response_config .should_skip_wait :
494510 while True :
495511 event = await wrapper .next_event ()
496512 if type (event ) is h11 .EndOfMessage :
497513 break
498514 assert type (event ) is h11 .Data
499-
500- if response_config is None :
501- response_config = ResponseConfig (parsed_path .path )
502- response_config .request_headers = request .headers
515+ else :
516+ print ("Skipping waiting for request body" )
503517
504518 response = response_config .resolve_response (
505519 wrapper , request_type , head_request = method == "HEAD" )
506-
507520 await send_response (wrapper , response )
508521
509522
0 commit comments