Skip to content

Commit

Permalink
LastEvent API for Activity and ChildWorkflow should not return any ca…
Browse files Browse the repository at this point in the history
…ncel/cancelfailed event. However these events are parts of AllEvents API.
  • Loading branch information
gurmitteotia committed Oct 21, 2018
1 parent bf56eb2 commit 3ba9f52
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Setup()
[Test]
public void Should_populate_properties()
{
Assert.That(_activityCancelRequestedEvent.IsActive,Is.True);
Assert.That(_activityCancelRequestedEvent.IsActive,Is.False);
}

[Test]
Expand Down
51 changes: 36 additions & 15 deletions Guflow.Tests/Decider/Activity/ActivityItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ public void Can_be_configured_to_schedule_activity_with_timeouts()
Assert.That(decision.Timeouts.StartToCloseTimeout, Is.EqualTo(TimeSpan.FromSeconds(5)));
}


[Test]
public void Last_event_is_cached()
{
Expand All @@ -185,6 +184,18 @@ public void Last_event_is_cached()
Assert.IsTrue(ReferenceEquals(latestEvent, latestEventCached));
}

[Test]
public void Last_event_can_be_null()
{
var workflowHistoryEvents = new WorkflowHistoryEvents(_eventGraphBuilder.WorkflowStartedGraph());
_workflow.SetupGet(w => w.WorkflowHistoryEvents).Returns(workflowHistoryEvents);
var activityItem = new ActivityItem(_activityIdenity, _workflow.Object);

var latestEvent = activityItem.LastEvent();

Assert.IsNull(latestEvent);
}

[Test]
public void Last_event_is_timer_event_when_timer_events_are_newer_then_activity_event()
{
Expand Down Expand Up @@ -212,6 +223,30 @@ public void Last_event_is_activity_event_when_activity_events_are_newer_then_tim
Assert.That(latestEvent, Is.EqualTo(new ActivityFailedEvent(activityFailedEventGraph.First(), activityFailedEventGraph)));
}

[Test]
public void Last_event_is_activity_started_event_when_its_cancel_request_is_in_progress()
{
var eventGraph = _eventGraphBuilder.ActivityCancelRequestedGraph(_activityIdenity, "id").ToArray();
var activityItem = CreateActivityItemWith(eventGraph);


var last = activityItem.LastEvent();

Assert.That(last, Is.EqualTo(new ActivityStartedEvent(eventGraph.Skip(1).First(), eventGraph)));
}

[Test]
public void Last_event_is_activity_started_event_when_its_cancel_request_is_failed()
{
var eventGraph = _eventGraphBuilder.ActivityCancellationFailedGraph(_activityIdenity, "cause").ToArray();
var activityItem = CreateActivityItemWith(eventGraph);


var last = activityItem.LastEvent();

Assert.That(last, Is.EqualTo(new ActivityStartedEvent(eventGraph.Skip(1).First(), eventGraph)));
}

[Test]
public void Last_event_by_default_filter_out_reschedule_timer_events()
{
Expand Down Expand Up @@ -582,19 +617,5 @@ private ActivityItem CreateActivityItemWith(IEnumerable<HistoryEvent> eventGraph
_workflow.SetupGet(w => w.WorkflowHistoryEvents).Returns(workflowHistoryEvents);
return new ActivityItem(_activityIdenity, _workflow.Object);
}
private class WorkflowWithParentActivity : Workflow
{
public WorkflowWithParentActivity(string parentActivityName,string parentActivityVersion, string postionalName)
{
ScheduleActivity(parentActivityName, parentActivityVersion, postionalName);
}
}
private class WorkflowWithParentTimer : Workflow
{
public WorkflowWithParentTimer(string parentTimerName)
{
ScheduleTimer(parentTimerName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Setup()
[Test]
public void Active()
{
Assert.That(_event.IsActive);
Assert.That(_event.IsActive, Is.False);
}
}
}
43 changes: 43 additions & 0 deletions Guflow.Tests/Decider/ChildWorkflow/ChildWorkflowItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ public void All_events_can_return_child_workflow_cancelled_event__and_external_w
}));
}

[Test]
public void All_events_can_return_external_workflow_cancel_request_failed_event_and_workflow_started_event()
{
var cancelFailedEvent = _eventGraphBuilder.ChildWorkflowCancelRequestFailedEventGraph(_scheduleIdentity, "runid", "cause").ToArray();

var childWorkflow = ChildWorkflow(cancelFailedEvent);

var allEvents = childWorkflow.AllEvents();

Assert.That(allEvents, Is.EqualTo(new WorkflowItemEvent[]
{
new ExternalWorkflowCancelRequestFailedEvent(cancelFailedEvent.First()),
new ChildWorkflowStartedEvent(cancelFailedEvent.Skip(2).First(), cancelFailedEvent),
}));
}

[Test]
public void All_events_can_return_reschedule_timer_events()
{
Expand Down Expand Up @@ -417,6 +433,33 @@ public void Last_event_can_filter_out_reschedule_timer_event()
Assert.That(lastEvent, Is.EqualTo(new ChildWorkflowFailedEvent(failedEventGraph.First(), allEventsGraph)));
}

[Test]
public void Last_event_returns_child_workflow_started_event_when_its_cancellation_is_in_progress()
{
var eventGrpah = _eventGraphBuilder.ChildWorkflowCancellationRequestedEventGraph(_scheduleIdentity, "runid", "input").ToArray();

var childWorkflow = ChildWorkflow(eventGrpah);

var lastEvent = childWorkflow.LastEvent();

Assert.That(lastEvent, Is.EqualTo(new ChildWorkflowStartedEvent(eventGrpah.Skip(2).First(), eventGrpah)));


}

[Test]
public void Last_event_returns_child_workflow_started_event_when_its_cancellation_is_failed_and_it_was_started()
{
var cancelFailedEvent = _eventGraphBuilder.ChildWorkflowCancelRequestFailedEventGraph(_scheduleIdentity, "runid", "cause").ToArray();

var childWorkflow = ChildWorkflow(cancelFailedEvent);

var lastEvent = childWorkflow.LastEvent();

Assert.That(lastEvent, Is.EqualTo(new ChildWorkflowStartedEvent(cancelFailedEvent.Skip(2).First(), cancelFailedEvent)));

}

[Test]
public void Reschedule_decision_is_timer_schedule_decision_for_child_workflow_item()
{
Expand Down
82 changes: 81 additions & 1 deletion Guflow.Tests/Decider/EventGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ public IEnumerable<HistoryEvent> ChildWorkflowCancellationRequestedEventGraph(Id
{
WorkflowExecution = workflowExecution,
WorkflowType = workflowType,
InitiatedEventId = eventIds.EventId(EventIds.Scheduled)
InitiatedEventId = eventIds.EventId(EventIds.Scheduled),
}
});

Expand All @@ -1353,6 +1353,72 @@ public IEnumerable<HistoryEvent> ChildWorkflowCancellationRequestedEventGraph(Id
return historyEvents;
}

public IEnumerable<HistoryEvent> ChildWorkflowCancelRequestFailedEventGraph(Identity identity, string runId, string cause)
{
var historyEvents = new List<HistoryEvent>();
var eventIds = EventIds.ChildWorkflowCancelRequestFailedIds(ref _currentEventId);
var workflowExecution = new WorkflowExecution() { RunId = runId, WorkflowId = identity.Id };
var workflowType = new WorkflowType() { Name = identity.Name, Version = identity.Version };

historyEvents.Add(new HistoryEvent()
{
EventId = eventIds.EventId(EventIds.CancelRequestFailed),
EventType = EventType.RequestCancelExternalWorkflowExecutionFailed,
RequestCancelExternalWorkflowExecutionFailedEventAttributes = new RequestCancelExternalWorkflowExecutionFailedEventAttributes()
{
InitiatedEventId = eventIds.EventId(EventIds.CancelInitiated),
WorkflowId = workflowExecution.WorkflowId,
RunId = workflowExecution.RunId,
Cause = cause,
}
});

historyEvents.Add(new HistoryEvent()
{
EventId = eventIds.EventId(EventIds.CancelInitiated),
EventType = EventType.RequestCancelExternalWorkflowExecutionInitiated,
RequestCancelExternalWorkflowExecutionInitiatedEventAttributes = new RequestCancelExternalWorkflowExecutionInitiatedEventAttributes()
{
RunId = workflowExecution.RunId,
WorkflowId = workflowExecution.WorkflowId,
}
});

historyEvents.Add(new HistoryEvent()
{
EventId = eventIds.EventId(EventIds.Started),
EventType = EventType.ChildWorkflowExecutionStarted,

ChildWorkflowExecutionStartedEventAttributes = new ChildWorkflowExecutionStartedEventAttributes()
{
WorkflowExecution = workflowExecution,
WorkflowType = workflowType,
InitiatedEventId = eventIds.EventId(EventIds.Scheduled)
}
});

historyEvents.Add(new HistoryEvent()
{
EventId = eventIds.EventId(EventIds.Scheduled),
EventType = EventType.StartChildWorkflowExecutionInitiated,
StartChildWorkflowExecutionInitiatedEventAttributes = new StartChildWorkflowExecutionInitiatedEventAttributes()
{
Control = (new ScheduleData() { PN = identity.PositionalName }).ToJson(),
WorkflowId = identity.Id,
WorkflowType = workflowType,
Input = "input",
LambdaRole = "lambda_role",
ExecutionStartToCloseTimeout = "3",
TagList = new List<string>() { "tag1" },
TaskList = new Amazon.SimpleWorkflow.Model.TaskList() { Name = "name" },
TaskPriority = "1",
TaskStartToCloseTimeout = "4"
}
});

return historyEvents;
}

private class EventIds
{
public const string Completion = "Completion";
Expand Down Expand Up @@ -1599,6 +1665,20 @@ public static EventIds WorkflowCancelRequestedIds(ref long eventId)
return new EventIds(ids);
}

public static EventIds ChildWorkflowCancelRequestFailedIds(ref long eventId)
{
const long totalEvents = 4;
eventId += totalEvents;
var ids = new Dictionary<string, long>()
{
{CancelRequestFailed, eventId},
{CancelInitiated, eventId - 1},
{Started, eventId - 2},
{Scheduled, eventId - 3}
};
return new EventIds(ids);
}

public static EventIds WorkflowCancelRequestFailedIds(ref long eventId)
{
const long totalEvents = 2;
Expand Down
79 changes: 79 additions & 0 deletions Guflow.Tests/Decider/WorkflowHistoryEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class WorkflowHistoryEventsTests
private const string ActivityName = "Activity1";
private const string ActivityVersion = "1.0";
private const string TimerName = "timer1";
private const string WorkflowName = "Cname";
private const string WorkflowVersion = "1.0";
private const string LambdaName = "LambdaName";
private Identity _childWorkflow;

[SetUp]
Expand Down Expand Up @@ -330,6 +333,16 @@ public void Should_be_active_when_activity_cancellation_is_in_progress()
Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}


[Test]
public void Should_not_be_active_when_activity_is_cancelled()
{
var cancelledGraph = _builder.ActivityCancelledGraph(Identity.New(ActivityName, ActivityVersion), "id", "details");
var workflowHistoryEvents = new WorkflowHistoryEvents(cancelledGraph);

Assert.IsFalse(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_not_be_active_when_activity_is_completed()
{
Expand All @@ -349,6 +362,72 @@ public void Should_be_active_when_activity_is_just_started_after_failure()
Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_be_active_when_activity_is_started_but_its_cancel_request_failed()
{
var eventGraph = _builder.ActivityCancellationFailedGraph(Identity.New(ActivityName, ActivityVersion), "reason")
.Concat(_builder.ActivityFailedGraph(Identity.New(ActivityName, ActivityVersion), "id", "res", "detail"));
var workflowHistoryEvents = new WorkflowHistoryEvents(eventGraph);

Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_not_be_active_when_child_workflow_is_cancelled()
{
var eventGraph = _builder.ChildWorkflowCancelledEventGraph(Identity.New(WorkflowName, WorkflowVersion),
"rid", "input", "detail");

var workflowHistoryEvents = new WorkflowHistoryEvents(eventGraph);

Assert.IsFalse(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_be_active_when_child_workflow_is_started_and_its_cancellation_is_requested()
{
var eventGraph = _builder.ChildWorkflowCancellationRequestedEventGraph(Identity.New(WorkflowName, WorkflowVersion),
"rid", "input");

var workflowHistoryEvents = new WorkflowHistoryEvents(eventGraph);

Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_be_active_when_child_workflow_is_started_and_its_cancel_request_is_failed()
{
var startedGraph =
_builder.ChildWorkflowStartedEventGraph(Identity.New(WorkflowName, WorkflowVersion), "rid", "input");
var cancelFailedGraph = _builder.ExternalWorkflowCancelRequestFailedEvent(Identity.New(WorkflowName, WorkflowVersion),
"rid", "input");

var workflowHistoryEvents = new WorkflowHistoryEvents(cancelFailedGraph.Concat(startedGraph));

Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_not_be_active_when_child_workflow_is_completed()
{
var eventGraph = _builder.ChildWorkflowCompletedGraph(Identity.New(WorkflowName, WorkflowVersion),
"rid", "input", "detail");

var workflowHistoryEvents = new WorkflowHistoryEvents(eventGraph);

Assert.IsFalse(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_be_active_when_lambda_is_started()
{
var eventGraph = _builder.LambdaStartedEventGraph(Identity.Lambda(LambdaName), "input");

var workflowHistoryEvents = new WorkflowHistoryEvents(eventGraph);

Assert.IsTrue(workflowHistoryEvents.HasActiveEvent());
}

[Test]
public void Should_be_active_when_timer_is_started()
{
Expand Down
1 change: 0 additions & 1 deletion Guflow/Decider/Activity/ActivityCancelRequestedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class ActivityCancelRequestedEvent : WorkflowItemEvent
internal ActivityCancelRequestedEvent(HistoryEvent activityCancelRequestedEvent) : base(activityCancelRequestedEvent.EventId)
{
SwfIdentity = SwfIdentity.Raw(activityCancelRequestedEvent.ActivityTaskCancelRequestedEventAttributes.ActivityId);
IsActive = true;
}
}
}
Loading

0 comments on commit 3ba9f52

Please sign in to comment.