コード例 #1
0
        public void ShouldCancelCountAt5()
        {
            const int expected = 10;
            var countUpdated = new AutoResetEvent(false);
            var countCanceled = new AutoResetEvent(false);
            var tracking = new MemoryTrackingParticipant();
            var target = CountModelFactory.CreateModel(tracking);

            target.CountUpdated = i =>
                {
                    if (i == 5)
                    {
                        countUpdated.Set();
                    }
                };

            target.CountCanceled = () => countCanceled.Set();

            try
            {
                target.StartCounting(expected);

                // Wait for the first update
                Assert.IsTrue(countUpdated.WaitOne(TestTimeout));
                target.CancelCounting();

                // Wait for the cancel
                Assert.IsTrue(countCanceled.WaitOne(TestTimeout));
            }
            finally
            {
                tracking.Trace();
            }
        }
コード例 #2
0
        public void AssertWillReturnAnAssertHostTracking()
        {
            var activity = CreateTestActivity();
            var host = new WorkflowApplication(activity);
            var target = new MemoryTrackingParticipant();
            host.Extensions.Add(target);

            host.RunEpisode();

            Assert.IsNotNull(target.Assert);
            Assert.IsInstanceOfType(target.Assert, typeof(MemoryTrackingParticipant.AssertHostTracking));
        }
コード例 #3
0
        public void ValidKeyShouldAuthorize()
        {
            // Arrange
            var securityDoor = new SecurityDoor { Timeout = this.timeout };
            var tracking = new MemoryTrackingParticipant();
            try
            {
                // Act
                // Insert a valid key
                securityDoor.InsertKey(Guid.NewGuid());

                // Assert
                Assert.AreEqual(DoorState.ClosedUnlocked, securityDoor.State);
            }
            finally
            {
                tracking.Trace();
                securityDoor.Trace();
            }
        }
コード例 #4
0
        public void InsertValidCard()
        {
            // Works with tracking
            var tracking = new MemoryTrackingParticipant();

            var atm = new AtmMachine();

            // Create a new state machine using enums to define the states I want
            var atmStateMachine = atm.StateMachine;

            // Add my tracking provider
            atmStateMachine.WorkflowApplication.Extensions.Add(tracking);

            try
            {
                // Turn the power on an wait for the CardInserted transition to enable
                atm.TurnPowerOn();

                // Insert a valid card and wait for the KeypadEnter transition
                atm.InsertCard(true);

                // Enter a pin
                atm.AcceptPin("1234");

                // Turn off the ATM
                atm.TurnPowerOff();

                // Verify the first three states occurred in the correct order.
                // Make sure you set the state name correctly
                AssertState.OccursInOrder(
                    "ATM StateMachine", tracking.Records, AtmState.Initialize, AtmState.InsertCard, AtmState.EnterPin);

                // Verify that you added an InitializeAtm activity to the Entry actions of the Initialize State
                AssertHelper.OccursInOrder(
                    "Transitions",
                    atm.Transitions,
                    AtmTrigger.PowerOn,
                    AtmTrigger.CardInserted,
                    AtmTrigger.KeypadEnter,
                    AtmTrigger.PowerOff);

                // Verify the prompts
                AssertHelper.OccursInOrder(
                    "Prompts", atm.DisplayedPrompts, Prompts.PleaseWait, Prompts.InsertCard, Prompts.EnterYourPin);

                // Verify the valid card
                Assert.AreEqual(1, atm.CardReaderResults.Count);
                Assert.AreEqual(CardStatus.Valid, atm.CardReaderResults[0].CardStatus);

                // Verify the exit action cleared the screen at least twice
                Assert.IsTrue(
                    atm.ClearCount >= 2,
                    "Verify that you added a ClearView activity to the Exit Action of the Initialize State");

                // Verify the camera control
                // AssertHelper.OccursInOrder("CameraControl", testViewModel.CameraControl, true);
            }
            finally
            {
                tracking.Trace();

                Trace.WriteLine(string.Empty);
                Trace.WriteLine("*** Workflow Definition ***");
                Trace.WriteLine(XamlServices.Save(atmStateMachine.WorkflowStateMachine));
            }
        }
コード例 #5
0
        public void PowerOnAtmScenario()
        {
            // Works with tracking
            var tracking = new MemoryTrackingParticipant();

            var atm = new AtmMachine();

            // Create a new state machine using enums to define the states I want
            var atmStateMachine = atm.StateMachine;

            // Add my tracking provider
            atmStateMachine.WorkflowApplication.Extensions.Add(tracking);

            try
            {
                // Start the state machine until it goes idle with the PowerOff trigger enabled
                atmStateMachine.Start(AtmTrigger.PowerOff);

                // Validate the current state is what you think it is
                Assert.AreEqual(AtmState.InsertCard, atmStateMachine.State);

                // Will block until the transition can fire (or timeout)
                atmStateMachine.Fire(AtmTrigger.PowerOff);

                // WaitForComplete for the atmStateMachine workflow to complete
                atmStateMachine.WaitForComplete();

                // Verify the first three states occurred in the correct order.
                // Make sure you set the state name correctly
                AssertState.OccursInOrder(
                    "ATM StateMachine", tracking.Records, AtmState.Initialize, AtmState.InsertCard);

                // Verify the prompts
                AssertHelper.OccursInOrder("Prompts", atm.DisplayedPrompts, Prompts.PleaseWait, Prompts.InsertCard);

                // Verify the exit action on Initialize and Entry action on PowerOff cleared the screen
                Assert.AreEqual(2, atm.ClearCount);
            }
            finally
            {
                tracking.Trace();

                Trace.WriteLine(string.Empty);
                Trace.WriteLine("*** Workflow Definition ***");
                Trace.WriteLine(XamlServices.Save(atmStateMachine.WorkflowStateMachine));
            }
        }
コード例 #6
0
        public void RecordsWillReturnTrackingRecordList()
        {
            var activity = CreateTestActivity();
            var host = new WorkflowApplication(activity);
            var target = new MemoryTrackingParticipant();
            host.Extensions.Add(target);

            host.RunEpisode();

            Assert.IsNotNull(target.Records);
            Assert.IsInstanceOfType(target.Records, typeof(TrackingRecordsList));
        }
コード例 #7
0
        public void WaitForWorkflowInstanceRecordWillSignalWaitHandleWhenRun()
        {
            var activity = CreateTestActivity();
            var host = new WorkflowApplication(activity);
            var target = new MemoryTrackingParticipant();
            host.Extensions.Add(target);
            var completed = new AutoResetEvent(false);

            // Add a handle to wait for the completed event
            target.WaitForWorkflowInstanceRecord(WorkflowInstanceRecordState.Completed, completed);

            host.Run();

            // Should signal
            Assert.IsTrue(completed.WaitOne(this.DefaultTimeout));
        }
コード例 #8
0
        public void TraceWillTraceRecords()
        {
            // Arrange
            var activity = CreateTestActivity();
            var host = new WorkflowApplication(activity);
            var target = new MemoryTrackingParticipant();
            host.Extensions.Add(target);
            var listener = new TestTraceListener();

            Trace.Listeners.Add(listener);

            // Act
            host.RunEpisode();
            target.Trace();

            // Assert
            Assert.IsTrue(listener.WriteLineCount > 0);
        }
コード例 #9
0
        public void WhenCountIsCanceledNoCompleteEventShouldBeRaised()
        {
            var countUpdated = new AutoResetEvent(false);
            var countCanceled = new AutoResetEvent(false);

            const int expected = 10;
            var tracking = new MemoryTrackingParticipant();
            var target = CountModelFactory.CreateModel(tracking);

            // Trigger the sync event on the first count
            target.CountUpdated = i => countUpdated.Set();

            target.CountCanceled = () => countCanceled.Set();
            target.CountCompleted = () => countCanceled.Set();

            try
            {
                target.StartCounting(expected);

                // Wait for the first update
                Assert.IsTrue(countUpdated.WaitOne(TestTimeout));
                target.CancelCounting();

                // Wait for the cancel
                Assert.IsTrue(countCanceled.WaitOne(TestTimeout));
            }
            finally
            {
                tracking.Trace();
            }
        }
コード例 #10
0
        public void ShouldUseDelayWhenCounting()
        {
            const int expected = 5;
            var countCompleted = new AutoResetEvent(false);
            var tracking = new MemoryTrackingParticipant();

            var target = CountModelFactory.CreateModel(tracking);
            target.CountCompleted = () => countCompleted.Set();

            try
            {
                var start = DateTime.Now;
                target.StartCounting(expected, 10);
                Assert.IsTrue(countCompleted.WaitOne(TestTimeout));

                var stop = DateTime.Now;

                // Should be at least 50msecs
                Assert.IsTrue(stop.Subtract(start).Milliseconds >= 50);
            }
            finally
            {
                tracking.Trace();
            }
        }
コード例 #11
0
        public void ShouldRaiseStartEvent()
        {
            var tracking = new MemoryTrackingParticipant();
            var target = CountModelFactory.CreateModel(tracking);
            var startEventRaised = false;
            var countCompleted = new AutoResetEvent(false);

            target.CountStarted = () => startEventRaised = true;
            target.CountCompleted = () => countCompleted.Set();
            try
            {
                target.StartCounting(1);
                Assert.IsTrue(countCompleted.WaitOne(TestTimeout));

                Assert.IsTrue(startEventRaised);
            }
            finally
            {
                tracking.Trace();
            }
        }
コード例 #12
0
        public void ShouldCountTo10()
        {
            var actual = -1;
            const int expected = 10;
            var tracking = new MemoryTrackingParticipant();
            var target = CountModelFactory.CreateModel(tracking);

            var countCompleted = new AutoResetEvent(false);

            target.CountCompleted = () => countCompleted.Set();
            target.CountUpdated = i => actual = i;

            try
            {
                target.StartCounting(expected);
                Assert.IsTrue(countCompleted.WaitOne(TestTimeout));
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                tracking.Trace();
            }
        }
コード例 #13
0
        public void AddTwoNumbersAndStoreInVariable()
        {
            // Create a state machine that uses ints to describe states and triggers
            // Using an int as the type requires a conversion function
            var target = new CodeStateMachine<TestState, TestTrigger>();

            var tracking = new MemoryTrackingParticipant();
            target.AddTracking(tracking);

            try
            {
                // Setup arguments that can be passed into the workflow
                // var arg1 = target.CreateInArgument<int>("Num1", 1);

                // Create a variable
                var sum = target.CreateVariable("Sum", typeof(int));

                // TODO: Think more about state, storing it, load it and passing it to activities

                // Access a variable from an expression
                target[TestState.One].Exit(
                    new Assign { To = new OutArgument<int>(sum), Value = new InArgument<int>(3) }).When(
                        TestTrigger.B, TestState.Two);

                // Will run the state machine and fire the trigger when ready
                // then wait for it to complete
                target.Fire(TestTrigger.B).WaitForComplete();

                AssertState.OccursInOrder(target.DisplayName, tracking.Records, TestState.One, TestState.Two);
            }
            finally
            {
                tracking.Trace();

                Trace.WriteLine(string.Empty);
                Trace.WriteLine("*** Workflow Definition ***");
                Trace.WriteLine(target.Xaml);
            }
        }
コード例 #14
0
        public void CreateAndRunStateStringSync()
        {
            // Create a state machine that uses ints to describe states and triggers
            // Using an int as the type requires a conversion function
            var target = new CodeStateMachine();

            var tracking = new MemoryTrackingParticipant();
            target.AddTracking(tracking);

            try
            {
                // State 1 will wait for trigger 2 which will transition to state 2
                target.AddState("One").When("Two", "Two");

                // State 2 will be the final state
                target.AddState("Two");

                // Will run the state machine and fire the trigger when ready
                target.Fire("Two").WaitForComplete();

                AssertState.OccursInOrder(target.DisplayName, tracking.Records, "One", "Two");
            }
            finally
            {
                tracking.Trace();

                Trace.WriteLine(string.Empty);
                Trace.WriteLine("*** Workflow Definition ***");
                Trace.WriteLine(XamlServices.Save(target.WorkflowStateMachine));
            }
        }
コード例 #15
0
        public void CreateAndRunStateStringEnum()
        {
            // Create a state machine that uses ints to describe states and triggers
            // Using an int as the type requires a conversion function
            var target = new CodeStateMachine<TestState, TestTrigger>();

            var tracking = new MemoryTrackingParticipant();
            target.AddTracking(tracking);

            try
            {
                // State One will wait for trigger B which will transition to state Two
                target[TestState.One].When(TestTrigger.B, TestState.Two);

                // Will run the state machine and fire the trigger when ready
                // then wait for it to complete
                target.Fire(TestTrigger.B).WaitForComplete();

                AssertState.OccursInOrder(target.DisplayName, tracking.Records, TestState.One, TestState.Two);
            }
            finally
            {
                tracking.Trace();

                Trace.WriteLine(string.Empty);
                Trace.WriteLine("*** Workflow Definition ***");
                Trace.WriteLine(XamlServices.Save(target.WorkflowStateMachine));
            }
        }