Implement heartbeat timeout on SWF activity tasks
This commit is contained in:
parent
f576f3765c
commit
90c8797abd
8 changed files with 178 additions and 14 deletions
|
|
@ -6,7 +6,7 @@ from moto.swf.models import (
|
|||
ActivityType,
|
||||
)
|
||||
|
||||
from ..utils import make_workflow_execution
|
||||
from ..utils import make_workflow_execution, ACTIVITY_TASK_TIMEOUTS
|
||||
|
||||
|
||||
def test_activity_task_creation():
|
||||
|
|
@ -17,6 +17,7 @@ def test_activity_task_creation():
|
|||
input="optional",
|
||||
scheduled_event_id=117,
|
||||
workflow_execution=wfe,
|
||||
timeouts=ACTIVITY_TASK_TIMEOUTS,
|
||||
)
|
||||
task.workflow_execution.should.equal(wfe)
|
||||
task.state.should.equal("SCHEDULED")
|
||||
|
|
@ -44,6 +45,7 @@ def test_activity_task_full_dict_representation():
|
|||
activity_type=ActivityType("foo", "v1.0"),
|
||||
input="optional",
|
||||
scheduled_event_id=117,
|
||||
timeouts=ACTIVITY_TASK_TIMEOUTS,
|
||||
workflow_execution=wfe,
|
||||
)
|
||||
at.start(1234)
|
||||
|
|
@ -69,6 +71,7 @@ def test_activity_task_reset_heartbeat_clock():
|
|||
activity_type="foo",
|
||||
input="optional",
|
||||
scheduled_event_id=117,
|
||||
timeouts=ACTIVITY_TASK_TIMEOUTS,
|
||||
workflow_execution=wfe,
|
||||
)
|
||||
|
||||
|
|
@ -78,3 +81,24 @@ def test_activity_task_reset_heartbeat_clock():
|
|||
task.reset_heartbeat_clock()
|
||||
|
||||
task.last_heartbeat_timestamp.should.equal(1420113600.0)
|
||||
|
||||
def test_activity_task_has_timedout():
|
||||
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,
|
||||
)
|
||||
task.has_timedout().should.equal(False)
|
||||
|
||||
# activity task timeout is 300s == 5mins
|
||||
with freeze_time("2015-01-01 12:06:00"):
|
||||
task.has_timedout().should.equal(True)
|
||||
task.process_timeouts()
|
||||
task.state.should.equal("TIMED_OUT")
|
||||
task.timeout_type.should.equal("HEARTBEAT")
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import boto
|
||||
from freezegun import freeze_time
|
||||
from sure import expect
|
||||
|
||||
from moto import mock_swf
|
||||
|
|
@ -8,18 +9,9 @@ from moto.swf.exceptions import (
|
|||
SWFUnknownResourceFault,
|
||||
)
|
||||
|
||||
from ..utils import setup_workflow
|
||||
from ..utils import setup_workflow, SCHEDULE_ACTIVITY_TASK_DECISION
|
||||
|
||||
|
||||
SCHEDULE_ACTIVITY_TASK_DECISION = {
|
||||
"decisionType": "ScheduleActivityTask",
|
||||
"scheduleActivityTaskDecisionAttributes": {
|
||||
"activityId": "my-activity-001",
|
||||
"activityType": { "name": "test-activity", "version": "v1.1" },
|
||||
"taskList": { "name": "activity-task-list" },
|
||||
}
|
||||
}
|
||||
|
||||
# PollForActivityTask endpoint
|
||||
@mock_swf
|
||||
def test_poll_for_activity_task_when_one():
|
||||
|
|
@ -183,8 +175,7 @@ def test_record_activity_task_heartbeat():
|
|||
])
|
||||
activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")["taskToken"]
|
||||
|
||||
resp = conn.record_activity_task_heartbeat(activity_token, details="some progress details")
|
||||
# TODO: check that "details" are reflected in ActivityTaskTimedOut event when a timeout occurs
|
||||
resp = conn.record_activity_task_heartbeat(activity_token)
|
||||
resp.should.equal({"cancelRequested": False})
|
||||
|
||||
@mock_swf
|
||||
|
|
@ -199,3 +190,21 @@ def test_record_activity_task_heartbeat_with_wrong_token():
|
|||
conn.record_activity_task_heartbeat.when.called_with(
|
||||
"bad-token", details="some progress details"
|
||||
).should.throw(SWFValidationException)
|
||||
|
||||
@mock_swf
|
||||
def test_record_activity_task_heartbeat_sets_details_in_case_of_timeout():
|
||||
conn = setup_workflow()
|
||||
decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"]
|
||||
conn.respond_decision_task_completed(decision_token, decisions=[
|
||||
SCHEDULE_ACTIVITY_TASK_DECISION
|
||||
])
|
||||
with freeze_time("2015-01-01 12:00:00"):
|
||||
activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")["taskToken"]
|
||||
conn.record_activity_task_heartbeat(activity_token, details="some progress details")
|
||||
|
||||
with freeze_time("2015-01-01 12:05:30"):
|
||||
# => Activity Task Heartbeat timeout reached!!
|
||||
resp = conn.get_workflow_execution_history("test-domain", conn.run_id, "uid-abcd1234")
|
||||
resp["events"][-2]["eventType"].should.equal("ActivityTaskTimedOut")
|
||||
attrs = resp["events"][-2]["activityTaskTimedOutEventAttributes"]
|
||||
attrs["details"].should.equal("some progress details")
|
||||
|
|
|
|||
34
tests/test_swf/responses/test_timeouts.py
Normal file
34
tests/test_swf/responses/test_timeouts.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import boto
|
||||
from freezegun import freeze_time
|
||||
from sure import expect
|
||||
|
||||
from moto import mock_swf
|
||||
|
||||
from ..utils import setup_workflow, SCHEDULE_ACTIVITY_TASK_DECISION
|
||||
|
||||
|
||||
# Activity Task Heartbeat timeout
|
||||
# Default value in workflow helpers: 5 mins
|
||||
@mock_swf
|
||||
def test_activity_task_heartbeat_timeout():
|
||||
with freeze_time("2015-01-01 12:00:00"):
|
||||
conn = setup_workflow()
|
||||
decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"]
|
||||
conn.respond_decision_task_completed(decision_token, decisions=[
|
||||
SCHEDULE_ACTIVITY_TASK_DECISION
|
||||
])
|
||||
conn.poll_for_activity_task("test-domain", "activity-task-list", identity="surprise")
|
||||
|
||||
with freeze_time("2015-01-01 12:04:30"):
|
||||
resp = conn.get_workflow_execution_history("test-domain", conn.run_id, "uid-abcd1234")
|
||||
resp["events"][-1]["eventType"].should.equal("ActivityTaskStarted")
|
||||
|
||||
with freeze_time("2015-01-01 12:05:30"):
|
||||
# => Activity Task Heartbeat timeout reached!!
|
||||
resp = conn.get_workflow_execution_history("test-domain", conn.run_id, "uid-abcd1234")
|
||||
|
||||
resp["events"][-2]["eventType"].should.equal("ActivityTaskTimedOut")
|
||||
attrs = resp["events"][-2]["activityTaskTimedOutEventAttributes"]
|
||||
attrs["timeoutType"].should.equal("HEARTBEAT")
|
||||
|
||||
resp["events"][-1]["eventType"].should.equal("DecisionTaskScheduled")
|
||||
|
|
@ -9,6 +9,29 @@ from moto.swf.models import (
|
|||
)
|
||||
|
||||
|
||||
# Some useful constants
|
||||
# Here are some activity timeouts we use in moto/swf tests ; they're extracted
|
||||
# from semi-real world example, the goal is mostly to have predictible and
|
||||
# intuitive behaviour in moto/swf own tests...
|
||||
ACTIVITY_TASK_TIMEOUTS = {
|
||||
"heartbeatTimeout": "300", # 5 mins
|
||||
"scheduleToStartTimeout": "1800", # 30 mins
|
||||
"startToCloseTimeout": "1800", # 30 mins
|
||||
"scheduleToCloseTimeout": "2700", # 45 mins
|
||||
}
|
||||
|
||||
# Some useful decisions
|
||||
SCHEDULE_ACTIVITY_TASK_DECISION = {
|
||||
"decisionType": "ScheduleActivityTask",
|
||||
"scheduleActivityTaskDecisionAttributes": {
|
||||
"activityId": "my-activity-001",
|
||||
"activityType": { "name": "test-activity", "version": "v1.1" },
|
||||
"taskList": { "name": "activity-task-list" },
|
||||
}
|
||||
}
|
||||
for key, value in ACTIVITY_TASK_TIMEOUTS.iteritems():
|
||||
SCHEDULE_ACTIVITY_TASK_DECISION["scheduleActivityTaskDecisionAttributes"][key] = value
|
||||
|
||||
# A test Domain
|
||||
def get_basic_domain():
|
||||
return Domain("test-domain", "90")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue