Пример #1
0
        public async Task CompositeRuntimeState_WithoutCancellationAndEventTransitions_ExecutesAllSubStates()
        {
            var tracker = new TestTracker();

            var tcs2 = new TaskCompletionSource <object>();

            var state1 = new SimpleRuntimeState(
                "state1",
                tracker.StateEnterAction,
                tracker.StateExecutionAction,
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var state2 = new SimpleRuntimeState(
                "state2",
                tracker.StateEnterAction,
                async s =>
            {
                await tracker.StateExecutionAction(s);
                tcs2.SetResult(null);
            },
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var state3 = new SimpleRuntimeState(
                "state3",
                tracker.StateEnterAction,
                tracker.StateExecutionAction,
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var compositeState = new CompositeRuntimeState(
                "composite",
                tracker.StateEnterAction,
                tracker.StateExitAction,
                tracker.StateCanceledAction,
                new RuntimeTransition("T1", state1, tracker.TransitionAction, null));

            state1.AddEventTransition("E1", new RuntimeTransition("T2", state2, tracker.TransitionAction, null));
            state2.AddEventTransition("E2", new RuntimeTransition("T3", state3, tracker.TransitionAction, null));

            var task = compositeState.ExecuteAsync(CancellationToken.None);

            Assert.False(task.IsCompleted);

            var handled = await compositeState.PublishEventAsync("E1");

            Assert.True(handled);
            Assert.False(task.IsCompleted);

            await tcs2.Task;

            handled = await compositeState.PublishEventAsync("E2");

            Assert.True(handled);

            await task;

            Assert.Equal(">composite;@composite->state1;>state1;*state1;!state1;@state1->state2;>state2;*state2;!state2;@state2->state3;>state3;*state3;<state3;<composite;", tracker.ToString());
        }
Пример #2
0
        public async Task ExecuteAsync_WithCancellationWithoutTransitions_CompletesImmediatellyAndRunsCanceledActionAndThrowsOperationCanceledException()
        {
            using (var cts = new CancellationTokenSource())
            {
                var tcs = new TaskCompletionSource <object>();

                var tracker = new TestTracker();

                var state = new SimpleRuntimeState(
                    "test",
                    tracker.StateEnterAction,
                    async s =>
                {
                    await tracker.StateExecutionAction(s);
                    await tcs.Task;
                },
                    tracker.StateExitAction,
                    tracker.StateCanceledAction);

                var task = state.ExecuteAsync(cts.Token);

                cts.Cancel();
                tcs.SetResult(null);

                await Assert.ThrowsAsync <OperationCanceledException>(async() => await task);

                Assert.Equal(">test;*test;!test;", tracker.ToString());
            }
        }
Пример #3
0
        public async Task ParallelAllRuntimeState_WithoutCancellationAndEventTransitions_ExecutesAllSubStates()
        {
            var tracker = new TestTracker();

            var tcs1 = new TaskCompletionSource <object>(TaskCreationOptions.None);
            var tcs2 = new TaskCompletionSource <object>(TaskCreationOptions.None);
            var tcs3 = new TaskCompletionSource <object>(TaskCreationOptions.None);

            var state1 = new SimpleRuntimeState(
                "state1",
                tracker.StateEnterAction,
                async s =>
            {
                await tracker.StateExecutionAction(s);
                await tcs1.Task;
            },
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var state2 = new SimpleRuntimeState(
                "state2",
                tracker.StateEnterAction,
                async s =>
            {
                await tracker.StateExecutionAction(s);
                await tcs2.Task;
            },
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var state3 = new SimpleRuntimeState(
                "state3",
                tracker.StateEnterAction,
                async s =>
            {
                await tracker.StateExecutionAction(s);
                await tcs3.Task;
            },
                tracker.StateExitAction,
                tracker.StateCanceledAction);

            var parallelState = new ParallelAllRuntimeState(
                "parallel",
                tracker.StateEnterAction,
                tracker.StateExitAction,
                tracker.StateCanceledAction,
                new RuntimeStateBase[] { state1, state2, state3 });

            var task = parallelState.ExecuteAsync(CancellationToken.None);

            tcs2.TrySetResult(null);
            tcs3.TrySetResult(null);
            tcs1.TrySetResult(null);

            await task;

            var tracking = tracker.ToString();

            Assert.EndsWith("<parallel;", tracking);

            var endingIndex = tracking.IndexOf("<");

            Assert.True(tracking.IndexOf("<state1;", endingIndex) > 0);
            Assert.True(tracking.IndexOf("<state2;", endingIndex) > 0);
            Assert.True(tracking.IndexOf("<state3;", endingIndex) > 0);
        }