Add SWF endpoint PollForDecisionTask and associated DecisionTask model

This commit is contained in:
Jean-Baptiste Barth 2015-10-11 14:02:55 +02:00
commit c16da9da2d
7 changed files with 213 additions and 3 deletions

View file

@ -0,0 +1,46 @@
import boto
from sure import expect
from moto import mock_swf
from moto.swf.exceptions import (
SWFUnknownResourceFault,
)
from .utils import mock_basic_workflow_type
@mock_swf
def setup_workflow():
conn = boto.connect_swf("the_key", "the_secret")
conn.register_domain("test-domain", "60", description="A test domain")
conn = mock_basic_workflow_type("test-domain", conn)
conn.register_activity_type("test-domain", "test-activity", "v1.1")
wfe = conn.start_workflow_execution("test-domain", "uid-abcd1234", "test-workflow", "v1.0")
conn.run_id = wfe["runId"]
return conn
# PollForDecisionTask endpoint
@mock_swf
def test_poll_for_decision_task_when_one():
conn = setup_workflow()
resp = conn.get_workflow_execution_history("test-domain", conn.run_id, "uid-abcd1234")
types = [evt["eventType"] for evt in resp["events"]]
types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled"])
resp = conn.poll_for_decision_task("test-domain", "queue", identity="srv01")
types = [evt["eventType"] for evt in resp["events"]]
types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled", "DecisionTaskStarted"])
resp["events"][-1]["decisionTaskStartedEventAttributes"]["identity"].should.equal("srv01")
@mock_swf
def test_poll_for_decision_task_when_none():
conn = setup_workflow()
conn.poll_for_decision_task("test-domain", "queue")
resp = conn.poll_for_decision_task("test-domain", "queue")
# this is the DecisionTask representation you get from the real SWF
# after waiting 60s when there's no decision to be taken
resp.should.equal({"previousStartedEventId": 0, "startedEventId": 0})

View file

@ -2,6 +2,7 @@ from sure import expect
from freezegun import freeze_time
from moto.swf.models import (
DecisionTask,
Domain,
GenericType,
HistoryEvent,
@ -115,7 +116,6 @@ def test_workflow_execution_creation_child_policy_logic():
WorkflowType("test-workflow", "v1.0"), "ab1234"
).should.throw(SWFDefaultUndefinedFault)
def test_workflow_execution_string_representation():
wft = get_basic_workflow_type()
wfe = WorkflowExecution(wft, "ab1234", child_policy="TERMINATE")
@ -182,6 +182,24 @@ def test_workflow_execution_full_dict_representation():
"taskStartToCloseTimeout": "300",
})
def test_workflow_execution_schedule_decision_task():
wft = get_basic_workflow_type()
wfe = WorkflowExecution(wft, "ab1234")
wfe.open_counts["openDecisionTasks"].should.equal(0)
wfe.schedule_decision_task()
wfe.open_counts["openDecisionTasks"].should.equal(1)
def test_workflow_execution_start_decision_task():
wft = get_basic_workflow_type()
wfe = WorkflowExecution(wft, "ab1234")
wfe.schedule_decision_task()
dt = wfe.decision_tasks[0]
wfe.start_decision_task(dt.task_token, identity="srv01")
dt = wfe.decision_tasks[0]
dt.state.should.equal("STARTED")
wfe.events[-1].event_type.should.equal("DecisionTaskStarted")
wfe.events[-1].identity.should.equal("srv01")
# HistoryEvent
@freeze_time("2015-01-01 12:00:00")
@ -207,3 +225,31 @@ def test_history_event_breaks_on_initialization_if_not_implemented():
HistoryEvent.when.called_with(
123, "UnknownHistoryEvent"
).should.throw(NotImplementedError)
# DecisionTask
def test_decision_task_creation():
wft = get_basic_workflow_type()
wfe = WorkflowExecution(wft, "ab1234")
dt = DecisionTask(wfe, 123)
dt.workflow_execution.should.equal(wfe)
dt.state.should.equal("SCHEDULED")
dt.task_token.should_not.be.empty
dt.started_event_id.should.be.none
def test_decision_task_full_dict_representation():
wft = get_basic_workflow_type()
wfe = WorkflowExecution(wft, "ab1234")
dt = DecisionTask(wfe, 123)
fd = dt.to_full_dict()
fd["events"].should.be.a("list")
fd["previousStartedEventId"].should.equal(0)
fd.should_not.contain("startedEventId")
fd.should.contain("taskToken")
fd["workflowExecution"].should.equal(wfe.to_short_dict())
fd["workflowType"].should.equal(wft.to_short_dict())
dt.start(1234)
fd = dt.to_full_dict()
fd["startedEventId"].should.equal(1234)