public void AddAutoForwardTransitionWRequest_throws_ArgumentNullException_if_Condition_null()
        {
            var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var sut          = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            Assert.Throws <ArgumentNullException>(() => sut.AddAutoForwardTransition <string>(SaleEvent.Pay, SaleState.Complete, condition: null));
        }
        public async Task ExecuteAutoTransitionAsync_executes_AutoTransition_without_previous_state()
        {
            var sale = new Sale(saleID: 96)
            {
                State = SaleState.ChangeDue
            };
            var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay
                                                                                    , SaleState.Open
                                                                                    , SaleState.ChangeDue
                                                                                    , SaleState.ChangeDue
                                                                                    , "lastTransitionName");
            var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var sut          = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            sut.AddAutoForwardTransition(SaleEvent.ChangeGiven, SaleState.Complete, (sale1, _) => Task.FromResult(result: true));
            var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.ChangeGiven, sale);

            var autoTransitionResult = await sut.ExecuteAutoTransitionAsync(parameters, transitionResult);

            Assert.True(autoTransitionResult.WasTransitioned);
            Assert.Equal(SaleState.Complete, sale.State);
            Assert.Equal(SaleState.Complete, autoTransitionResult.CurrentState);
            Assert.Equal(SaleState.ChangeDue, autoTransitionResult.PreviousState);
            Assert.Equal(SaleState.Open, autoTransitionResult.StartingState);
        }
        public void ExecuteAutoTransitionAsync_can_be_cancelled()
        {
            var stopwatch = new Stopwatch();
            var sale      = new Sale(saleID: 96)
            {
                State = SaleState.ChangeDue
            };
            var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay
                                                                                    , SaleState.ChangeDue
                                                                                    , SaleState.ChangeDue
                                                                                    , SaleState.ChangeDue
                                                                                    , "lastTransitionName");
            var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var sut          = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            sut.AddAutoForwardTransition(SaleEvent.ChangeGiven, SaleState.Complete, (sale1, cancelToken) =>
            {
                do
                {
                    Task.Delay(millisecondsDelay: 99).Wait();
                } while (!cancelToken.IsCancellationRequested);

                return(Task.FromResult(result: !cancelToken.IsCancellationRequested));
            });

            using (var mutex = new Mutex(initiallyOwned: false))
                using (var cancelSource = new CancellationTokenSource())
                {
                    var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.ChangeGiven, sale, cancelSource.Token);
                    StateTransitionResult <SaleState, SaleEvent> autoTransitionResult = null;

                    Task.Factory.StartNew(async() =>
                    {
                        mutex.WaitOne();
                        autoTransitionResult = await sut.ExecuteAutoTransitionAsync(parameters, transitionResult);
                        mutex.ReleaseMutex();
                    }, TaskCreationOptions.LongRunning);

                    try
                    {
                        stopwatch.Start();
                        Task.Delay(millisecondsDelay: 2345).Wait();
                        cancelSource.Cancel();
                        mutex.WaitOne();
                    }
                    catch (Exception ex)
                    {
                        cancelSource.Cancel();
                    }

                    Assert.True(autoTransitionResult.WasCancelled);
                    Assert.False(autoTransitionResult.ConditionMet);
                    Assert.True(autoTransitionResult.TransitionDefined);
                    Assert.Equal(SaleState.ChangeDue, sale.State);
                    Assert.Equal(SaleState.ChangeDue, autoTransitionResult.CurrentState);
                }
        }