Skip to content

Commit 2d2a556

Browse files
committed
Add more tests for async run client
* Directly inherit sync client's methods in async mode
1 parent fb4d0aa commit 2d2a556

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

cli/polyaxon/_client/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ def push_offline_run(
32323232
class AsyncRunClient(RunClient):
32333233
_IS_ASYNC = True
32343234

3235-
async def _areset_agent(self, agent: Optional[str] = None):
3235+
async def _reset_agent(self, agent: Optional[str] = None):
32363236
if agent:
32373237
agent = await self.client.agents_v1.get_agent(self.owner, agent)
32383238
self.settings.agent = V1SettingsCatalog(
@@ -3244,7 +3244,7 @@ async def _areset_agent(self, agent: Optional[str] = None):
32443244
self.settings.namespace = agent.namespace
32453245
self.settings.artifacts_store = None
32463246

3247-
async def _ause_agent_host(self):
3247+
async def _use_agent_host(self):
32483248
if self.settings.agent and self.settings.agent.url:
32493249
await self.areset_client(
32503250
host=self.settings.agent.url,
@@ -3469,7 +3469,7 @@ async def get_events(
34693469
):
34703470
if not self.settings:
34713471
await self.refresh_data()
3472-
await self._ause_agent_host()
3472+
await self._use_agent_host()
34733473

34743474
params = get_streams_params(self.artifacts_store)
34753475
return await self.client.runs_v1.get_run_events(
@@ -3495,7 +3495,7 @@ async def get_multi_run_events(
34953495
):
34963496
if not self.settings:
34973497
await self.refresh_data()
3498-
await self._ause_agent_host()
3498+
await self._use_agent_host()
34993499

35003500
params = get_streams_params(self.artifacts_store)
35013501
return await self.client.runs_v1.get_multi_run_events(

cli/tests/test_client/test_async_run_client.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,80 @@ async def test_load_offline_run_is_sync_only():
528528

529529
with pytest.raises(PolyaxonClientException):
530530
await AsyncRunClient.load_offline_run("/tmp/run")
531+
532+
533+
@pytest.mark.asyncio
534+
async def test_restart_routes_to_copy_when_copy_flag_set():
535+
patch_settings()
536+
sdk_client = AsyncPolyaxonClientMock()
537+
response = make_run(name="restarted")
538+
sdk_client.runs_v1.copy_run = AsyncMock(return_value=response)
539+
sdk_client.runs_v1.restart_run = AsyncMock()
540+
client = make_client(sdk_client)
541+
542+
result = await client.restart(copy=True, name="restarted")
543+
544+
assert result is response
545+
assert sdk_client.runs_v1.copy_run.call_count == 1
546+
assert sdk_client.runs_v1.restart_run.call_count == 0
547+
assert "async_req" not in sdk_client.runs_v1.copy_run.call_args[1]
548+
549+
550+
@pytest.mark.asyncio
551+
async def test_restart_routes_to_restart_when_copy_not_set():
552+
patch_settings()
553+
sdk_client = AsyncPolyaxonClientMock()
554+
response = make_run(name="restarted")
555+
sdk_client.runs_v1.restart_run = AsyncMock(return_value=response)
556+
sdk_client.runs_v1.copy_run = AsyncMock()
557+
client = make_client(sdk_client)
558+
559+
result = await client.restart(name="restarted")
560+
561+
assert result is response
562+
assert sdk_client.runs_v1.restart_run.call_count == 1
563+
assert sdk_client.runs_v1.copy_run.call_count == 0
564+
assert "async_req" not in sdk_client.runs_v1.restart_run.call_args[1]
565+
566+
567+
@pytest.mark.asyncio
568+
async def test_resume_awaits_resume_run_with_built_body():
569+
patch_settings()
570+
sdk_client = AsyncPolyaxonClientMock()
571+
response = make_run(name="resumed")
572+
sdk_client.runs_v1.resume_run = AsyncMock(return_value=response)
573+
client = make_client(sdk_client)
574+
575+
result = await client.resume(name="resumed")
576+
577+
assert result is response
578+
assert sdk_client.runs_v1.resume_run.call_count == 1
579+
assert "async_req" not in sdk_client.runs_v1.resume_run.call_args[1]
580+
581+
582+
@pytest.mark.asyncio
583+
async def test_log_succeeded_awaits_create_run_status_via_log_status():
584+
patch_settings()
585+
sdk_client = AsyncPolyaxonClientMock()
586+
sdk_client.runs_v1.create_run_status = AsyncMock(return_value=None)
587+
client = make_client(sdk_client)
588+
client._run_data = make_run(status=V1Statuses.RUNNING)
589+
590+
await client.log_succeeded()
591+
592+
assert sdk_client.runs_v1.create_run_status.call_count == 1
593+
body = sdk_client.runs_v1.create_run_status.call_args[1]["body"]
594+
assert body["condition"].type == V1Statuses.SUCCEEDED
595+
596+
597+
@pytest.mark.asyncio
598+
async def test_log_end_status_short_circuits_when_already_done():
599+
patch_settings()
600+
sdk_client = AsyncPolyaxonClientMock()
601+
sdk_client.runs_v1.create_run_status = AsyncMock()
602+
client = make_client(sdk_client)
603+
client._run_data = make_run(status=V1Statuses.SUCCEEDED)
604+
605+
await client.log_failed()
606+
607+
assert sdk_client.runs_v1.create_run_status.call_count == 0

0 commit comments

Comments
 (0)