| 
									
										
										
										
											2015-11-02 10:26:40 +01:00
										 |  |  | from freezegun import freeze_time | 
					
						
							| 
									
										
										
										
											2021-10-18 19:44:29 +00:00
										 |  |  | import sure  # noqa # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2015-10-26 06:31:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 01:12:51 +01:00
										 |  |  | from moto.swf.exceptions import SWFWorkflowExecutionClosedError | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | from moto.swf.models import ActivityTask, ActivityType, Timeout | 
					
						
							| 
									
										
										
										
											2015-10-26 06:31:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 02:47:05 +01:00
										 |  |  | from ..utils import ( | 
					
						
							|  |  |  |     ACTIVITY_TASK_TIMEOUTS, | 
					
						
							|  |  |  |     make_workflow_execution, | 
					
						
							|  |  |  |     process_first_timeout, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-10-26 06:31:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_activity_task_creation(): | 
					
						
							|  |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  |     task = ActivityTask( | 
					
						
							|  |  |  |         activity_id="my-activity-123", | 
					
						
							|  |  |  |         activity_type="foo", | 
					
						
							|  |  |  |         input="optional", | 
					
						
							| 
									
										
										
										
											2015-10-26 23:16:59 +01:00
										 |  |  |         scheduled_event_id=117, | 
					
						
							| 
									
										
										
										
											2015-10-26 06:31:00 +01:00
										 |  |  |         workflow_execution=wfe, | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  |         timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							| 
									
										
										
										
											2015-10-26 06:31:00 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  |     task.workflow_execution.should.equal(wfe) | 
					
						
							|  |  |  |     task.state.should.equal("SCHEDULED") | 
					
						
							|  |  |  |     task.task_token.should_not.be.empty | 
					
						
							|  |  |  |     task.started_event_id.should.be.none | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task.start(123) | 
					
						
							|  |  |  |     task.state.should.equal("STARTED") | 
					
						
							|  |  |  |     task.started_event_id.should.equal(123) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task.complete() | 
					
						
							|  |  |  |     task.state.should.equal("COMPLETED") | 
					
						
							| 
									
										
										
										
											2015-10-26 23:16:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 12:29:57 +01:00
										 |  |  |     # NB: this doesn't make any sense for SWF, a task shouldn't go from a | 
					
						
							|  |  |  |     # "COMPLETED" state to a "FAILED" one, but this is an internal state on our | 
					
						
							|  |  |  |     # side and we don't care about invalid state transitions for now. | 
					
						
							|  |  |  |     task.fail() | 
					
						
							|  |  |  |     task.state.should.equal("FAILED") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:02:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 23:16:59 +01:00
										 |  |  | def test_activity_task_full_dict_representation(): | 
					
						
							|  |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  |     at = ActivityTask( | 
					
						
							|  |  |  |         activity_id="my-activity-123", | 
					
						
							|  |  |  |         activity_type=ActivityType("foo", "v1.0"), | 
					
						
							|  |  |  |         input="optional", | 
					
						
							|  |  |  |         scheduled_event_id=117, | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  |         timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							| 
									
										
										
										
											2015-10-26 23:16:59 +01:00
										 |  |  |         workflow_execution=wfe, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     at.start(1234) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fd = at.to_full_dict() | 
					
						
							|  |  |  |     fd["activityId"].should.equal("my-activity-123") | 
					
						
							|  |  |  |     fd["activityType"]["version"].should.equal("v1.0") | 
					
						
							|  |  |  |     fd["input"].should.equal("optional") | 
					
						
							|  |  |  |     fd["startedEventId"].should.equal(1234) | 
					
						
							|  |  |  |     fd.should.contain("taskToken") | 
					
						
							|  |  |  |     fd["workflowExecution"].should.equal(wfe.to_short_dict()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     at.start(1234) | 
					
						
							|  |  |  |     fd = at.to_full_dict() | 
					
						
							|  |  |  |     fd["startedEventId"].should.equal(1234) | 
					
						
							| 
									
										
										
										
											2015-11-02 10:26:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:02:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 10:26:40 +01:00
										 |  |  | def test_activity_task_reset_heartbeat_clock(): | 
					
						
							|  |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 12:00:00"): | 
					
						
							|  |  |  |         task = ActivityTask( | 
					
						
							|  |  |  |             activity_id="my-activity-123", | 
					
						
							|  |  |  |             activity_type="foo", | 
					
						
							|  |  |  |             input="optional", | 
					
						
							|  |  |  |             scheduled_event_id=117, | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  |             timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							| 
									
										
										
										
											2015-11-02 10:26:40 +01:00
										 |  |  |             workflow_execution=wfe, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 09:09:00 +01:00
										 |  |  |     task.last_heartbeat_timestamp.should.equal(1420113600.0) | 
					
						
							| 
									
										
										
										
											2015-11-02 10:26:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 13:00:00"): | 
					
						
							|  |  |  |         task.reset_heartbeat_clock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 09:09:00 +01:00
										 |  |  |     task.last_heartbeat_timestamp.should.equal(1420117200.0) | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:02:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 02:22:02 +01:00
										 |  |  | def test_activity_task_first_timeout(): | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 12:00:00"): | 
					
						
							|  |  |  |         task = ActivityTask( | 
					
						
							|  |  |  |             activity_id="my-activity-123", | 
					
						
							|  |  |  |             activity_type="foo", | 
					
						
							|  |  |  |             input="optional", | 
					
						
							|  |  |  |             scheduled_event_id=117, | 
					
						
							|  |  |  |             timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							|  |  |  |             workflow_execution=wfe, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2015-11-05 02:22:02 +01:00
										 |  |  |         task.first_timeout().should.be.none | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # activity task timeout is 300s == 5mins | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 12:06:00"): | 
					
						
							| 
									
										
										
										
											2015-11-05 02:22:02 +01:00
										 |  |  |         task.first_timeout().should.be.a(Timeout) | 
					
						
							| 
									
										
										
										
											2015-11-05 02:47:05 +01:00
										 |  |  |         process_first_timeout(task) | 
					
						
							| 
									
										
										
										
											2015-11-03 00:28:13 +01:00
										 |  |  |         task.state.should.equal("TIMED_OUT") | 
					
						
							|  |  |  |         task.timeout_type.should.equal("HEARTBEAT") | 
					
						
							| 
									
										
										
										
											2015-11-04 22:35:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:02:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-12 08:01:42 -05:00
										 |  |  | def test_activity_task_first_timeout_with_heartbeat_timeout_none(): | 
					
						
							|  |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     activity_task_timeouts = ACTIVITY_TASK_TIMEOUTS.copy() | 
					
						
							|  |  |  |     activity_task_timeouts["heartbeatTimeout"] = "NONE" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 12:00:00"): | 
					
						
							|  |  |  |         task = ActivityTask( | 
					
						
							|  |  |  |             activity_id="my-activity-123", | 
					
						
							|  |  |  |             activity_type="foo", | 
					
						
							|  |  |  |             input="optional", | 
					
						
							|  |  |  |             scheduled_event_id=117, | 
					
						
							|  |  |  |             timeouts=activity_task_timeouts, | 
					
						
							|  |  |  |             workflow_execution=wfe, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         task.first_timeout().should.be.none | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-04 22:35:45 +01:00
										 |  |  | def test_activity_task_cannot_timeout_on_closed_workflow_execution(): | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 12:00:00"): | 
					
						
							|  |  |  |         wfe = make_workflow_execution() | 
					
						
							|  |  |  |         wfe.start() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 13:58:00"): | 
					
						
							|  |  |  |         task = ActivityTask( | 
					
						
							|  |  |  |             activity_id="my-activity-123", | 
					
						
							|  |  |  |             activity_type="foo", | 
					
						
							|  |  |  |             input="optional", | 
					
						
							|  |  |  |             scheduled_event_id=117, | 
					
						
							|  |  |  |             timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							|  |  |  |             workflow_execution=wfe, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with freeze_time("2015-01-01 14:10:00"): | 
					
						
							| 
									
										
										
										
											2015-11-05 02:22:02 +01:00
										 |  |  |         task.first_timeout().should.be.a(Timeout) | 
					
						
							|  |  |  |         wfe.first_timeout().should.be.a(Timeout) | 
					
						
							| 
									
										
										
										
											2015-11-05 02:47:05 +01:00
										 |  |  |         process_first_timeout(wfe) | 
					
						
							| 
									
										
										
										
											2015-11-05 02:22:02 +01:00
										 |  |  |         task.first_timeout().should.be.none | 
					
						
							| 
									
										
										
										
											2015-11-05 01:12:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-02 14:02:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 01:12:51 +01:00
										 |  |  | def test_activity_task_cannot_change_state_on_closed_workflow_execution(): | 
					
						
							|  |  |  |     wfe = make_workflow_execution() | 
					
						
							|  |  |  |     wfe.start() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     task = ActivityTask( | 
					
						
							|  |  |  |         activity_id="my-activity-123", | 
					
						
							|  |  |  |         activity_type="foo", | 
					
						
							|  |  |  |         input="optional", | 
					
						
							|  |  |  |         scheduled_event_id=117, | 
					
						
							|  |  |  |         timeouts=ACTIVITY_TASK_TIMEOUTS, | 
					
						
							|  |  |  |         workflow_execution=wfe, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     wfe.complete(123) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 21:37:43 -05:00
										 |  |  |     task.timeout.when.called_with(Timeout(task, 0, "foo")).should.throw( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         SWFWorkflowExecutionClosedError | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-11-05 01:12:51 +01:00
										 |  |  |     task.complete.when.called_with().should.throw(SWFWorkflowExecutionClosedError) | 
					
						
							|  |  |  |     task.fail.when.called_with().should.throw(SWFWorkflowExecutionClosedError) |