@@ -149,6 +149,72 @@ def test_task_retries_do_not_repeat_failed_submission(self, mock_post):
149149
150150 mock_post .assert_called_once ()
151151
152+ @staticmethod
153+ def _response (status_code , payload = None , text = "" ):
154+ response = MagicMock ()
155+ response .status_code = status_code
156+ response .text = text
157+ response .json .return_value = payload
158+ return response
159+
160+ @patch ("wavespeed.api.client.time.sleep" )
161+ @patch ("wavespeed.api.client.requests.get" )
162+ @patch ("wavespeed.api.client.requests.post" )
163+ def test_task_retries_do_not_repost_after_poll_retry_exhaustion (
164+ self , mock_post , mock_get , _mock_sleep
165+ ):
166+ """A poll that exhausts its GET retries must not create a second task."""
167+ mock_post .return_value = self ._response (200 , {"data" : {"id" : "req-1" }})
168+ mock_get .return_value = self ._response (502 , text = "Bad Gateway" )
169+
170+ client = Client (api_key = "test-key" , max_retries = 2 , max_connection_retries = 1 )
171+ with self .assertRaises (RuntimeError ) as ctx :
172+ client .run ("wavespeed-ai/z-image/turbo" , {"prompt" : "test" })
173+
174+ self .assertIn ("req-1" , str (ctx .exception ))
175+ self .assertIn ("HTTP 502" , str (ctx .exception ))
176+ self .assertEqual (mock_post .call_count , 1 )
177+ self .assertEqual (mock_get .call_count , 2 )
178+
179+ @patch ("wavespeed.api.client.time.sleep" )
180+ @patch ("wavespeed.api.client.requests.get" )
181+ @patch ("wavespeed.api.client.requests.post" )
182+ def test_task_retries_do_not_repost_after_wait_timeout (
183+ self , mock_post , mock_get , _mock_sleep
184+ ):
185+ """A client-side wait timeout leaves the task alive and must not re-POST."""
186+ mock_post .return_value = self ._response (200 , {"data" : {"id" : "req-1" }})
187+ mock_get .return_value = self ._response (200 , {"data" : {"status" : "processing" }})
188+
189+ client = Client (api_key = "test-key" , max_retries = 2 )
190+ with self .assertRaises (TimeoutError ) as ctx :
191+ client .run ("wavespeed-ai/z-image/turbo" , {"prompt" : "test" }, timeout = 0 )
192+
193+ self .assertIn ("req-1" , str (ctx .exception ))
194+ self .assertEqual (mock_post .call_count , 1 )
195+
196+ @patch ("wavespeed.api.client.time.sleep" )
197+ @patch ("wavespeed.api.client.requests.get" )
198+ @patch ("wavespeed.api.client.requests.post" )
199+ def test_task_retries_rerun_when_task_reports_failed (
200+ self , mock_post , mock_get , _mock_sleep
201+ ):
202+ """A task that itself reports failed is re-run when max_retries > 0."""
203+ mock_post .side_effect = [
204+ self ._response (200 , {"data" : {"id" : "req-1" }}),
205+ self ._response (200 , {"data" : {"id" : "req-2" }}),
206+ ]
207+ mock_get .side_effect = [
208+ self ._response (200 , {"data" : {"status" : "failed" , "error" : "oom" }}),
209+ self ._response (200 , {"data" : {"status" : "completed" , "outputs" : ["url" ]}}),
210+ ]
211+
212+ client = Client (api_key = "test-key" , max_retries = 1 )
213+ result = client .run ("wavespeed-ai/z-image/turbo" , {"prompt" : "test" })
214+
215+ self .assertEqual (result , {"outputs" : ["url" ]})
216+ self .assertEqual (mock_post .call_count , 2 )
217+
152218 @patch ("wavespeed.api.client.requests.get" )
153219 def test_get_result_success (self , mock_get ):
154220 """Test successful result retrieval."""
0 commit comments