|
17 | 17 |
|
18 | 18 | import temporalio.api.workflowservice.v1 |
19 | 19 | from temporalio import activity, workflow |
20 | | -from temporalio.api.workflowservice.v1.request_response_pb2 import ResetActivityRequest |
21 | 20 | from temporalio.client import ( |
22 | 21 | AsyncActivityHandle, |
23 | 22 | Client, |
@@ -1489,34 +1488,62 @@ async def h(): |
1489 | 1488 | ) |
1490 | 1489 | assert result.result == "details: Some detail" |
1491 | 1490 |
|
1492 | | -async def test_activity_reset(client: Client, worker: ExternalWorker): |
1493 | 1491 |
|
| 1492 | +async def test_activity_reset(client: Client, worker: ExternalWorker): |
1494 | 1493 | @activity.defn |
1495 | 1494 | async def reset_activity() -> None: |
1496 | | - |
1497 | | - await client.workflow_service.reset_activity(temporalio.api.workflowservice.v1.ResetActivityRequest( |
| 1495 | + req = temporalio.api.workflowservice.v1.ResetActivityRequest( |
1498 | 1496 | namespace=client.namespace, |
1499 | 1497 | execution=temporalio.api.common.v1.WorkflowExecution( |
1500 | 1498 | workflow_id=activity.info().workflow_id, |
1501 | 1499 | run_id=activity.info().workflow_run_id, |
1502 | 1500 | ), |
1503 | 1501 | id=activity.info().activity_id, |
1504 | | - )) |
| 1502 | + ) |
| 1503 | + activity.logger.info(f"Sending reset request: {req}") |
| 1504 | + await client.workflow_service.reset_activity(req) |
1505 | 1505 | reset = False |
1506 | 1506 | for _ in range(5): |
| 1507 | + activity.heartbeat() |
1507 | 1508 | try: |
1508 | 1509 | if reset: |
1509 | 1510 | return None |
1510 | | - await asyncio.sleep(1) |
| 1511 | + await asyncio.sleep(0.3) |
1511 | 1512 | except Exception as e: |
1512 | 1513 | activity.logger.warning("Exception: ", e) |
1513 | 1514 | reset = True |
1514 | 1515 | raise |
1515 | 1516 |
|
1516 | 1517 | assert False |
1517 | 1518 |
|
1518 | | - await _execute_workflow_with_activity( |
1519 | | - client, worker, reset_activity |
1520 | | - ) |
| 1519 | + await _execute_workflow_with_activity(client, worker, reset_activity) |
| 1520 | + |
1521 | 1521 |
|
| 1522 | +async def test_activity_reset_catch(client: Client, worker: ExternalWorker): |
| 1523 | + @activity.defn |
| 1524 | + async def wait_cancel() -> str: |
| 1525 | + req = temporalio.api.workflowservice.v1.ResetActivityRequest( |
| 1526 | + namespace=client.namespace, |
| 1527 | + execution=temporalio.api.common.v1.WorkflowExecution( |
| 1528 | + workflow_id=activity.info().workflow_id, |
| 1529 | + run_id=activity.info().workflow_run_id, |
| 1530 | + ), |
| 1531 | + id=activity.info().activity_id, |
| 1532 | + ) |
| 1533 | + activity.logger.info(f"Sending reset request: {req}") |
| 1534 | + await client.workflow_service.reset_activity(req) |
| 1535 | + try: |
| 1536 | + while True: |
| 1537 | + await asyncio.sleep(0.3) |
| 1538 | + activity.heartbeat() |
| 1539 | + except asyncio.CancelledError: |
| 1540 | + details = activity.cancellation_details() |
| 1541 | + assert details is not None |
| 1542 | + return "Got cancelled error, reset? " + str(details.reset) |
1522 | 1543 |
|
| 1544 | + result = await _execute_workflow_with_activity( |
| 1545 | + client, |
| 1546 | + worker, |
| 1547 | + wait_cancel, |
| 1548 | + ) |
| 1549 | + assert result.result == "Got cancelled error, reset? True" |
0 commit comments