Our project is good but it has one weakness. It verifies that the WorkflowThread returned is non-zero but it does not verify that it
returns the actual managed thread ID that the workflow ran on. Our project would
pass if the workflow always returned 1.
If we want to verify the actual thread ID we will need to use WorkflowApplication to invoke it. In this task we will modify the test to capture the thread of the workflow by obtaining it during a call to the WorkflowApplication.Completed action and then comparing that value to the value returned from the workflow.
If we want to verify the actual thread ID we will need to use WorkflowApplication to invoke it. In this task we will modify the test to capture the thread of the workflow by obtaining it during a call to the WorkflowApplication.Completed action and then comparing that value to the value returned from the workflow.
- First open HelloWorkFlow project from last article Workflow Application part1.
- Open
SayHelloFixture.cs (C#) or SayHelloFixture.vb (VB) and locate the ShouldReturnWorkflowThread test. Replace
the test with the following code, to use the WorkflowApplication class to run the workflow. This code will use
the WorkflowApplication.Completed
action to capture the output arguments and thread ID.C#/// <summary>/// Verifies that the workflow returns an Out Argument/// Name: WorkflowThread/// Type: Int32/// Value: Non-Zero, matches thread used for Completed action/// </summary>[TestMethod]public void ShouldReturnWorkflowThread(){AutoResetEvent sync = new AutoResetEvent(false);Int32 actionThreadID = 0;IDictionary<string, object> output = null;WorkflowApplication workflowApp =new WorkflowApplication(new SayHello(){UserName = "Test"});// Create an Action<T> using a lambda expression// To be invoked when the workflow completesworkflowApp.Completed = (e) =>{output = e.Outputs;actionThreadID = Thread.CurrentThread.ManagedThreadId;// Signal the test thread the workflow is donesync.Set();};workflowApp.Run();// Wait for the sync event for 1 secondsync.WaitOne(TimeSpan.FromSeconds(1));Assert.IsNotNull(output,"output not set, workflow may have timed out");Assert.IsTrue(output.ContainsKey("WorkflowThread"),"SayHello must contain an OutArgument named WorkflowThread");// Don't know for sure what it is yetvar outarg = output["WorkflowThread"];Assert.IsInstanceOfType(outarg, typeof(Int32),"WorkflowThread must be of type Int32");Assert.AreNotEqual(0, outarg,"WorkflowThread must not be zero");Debug.WriteLine("Test thread is " +Thread.CurrentThread.ManagedThreadId);Debug.WriteLine("Workflow thread is " + outarg.ToString());}Visual Basic''' <summary>''' Verifies that the SayHello workflow contains an Out Argument''' Name: WorkflowThread''' Type: Int32''' Value: Non-Zero, matches thread used for Completed action''' </summary>''' <remarks></remarks><TestMethod()> Public Sub ShouldReturnWorkflowThread()Dim sync As New AutoResetEvent(False)Dim actionThreadID As Int32 = 0Dim output As IDictionary(Of String, Object) = NothingDim workflowApp As New WorkflowApplication( _New SayHello() With {.UserName = "Test"})workflowApp.Completed = _Function(e)output = e.OutputsactionThreadID = Thread.CurrentThread.ManagedThreadId' Signal the test thread the workflow is donesync.Set()' VB requires a lambda expression to return a value' It is not used with Action(Of T)Return NothingEnd FunctionworkflowApp.Run()' Wait for the sync event for 1 secondsync.WaitOne(TimeSpan.FromSeconds(1))Assert.IsNotNull(output, "output not set, workflow may have timed out")Assert.IsTrue(output.ContainsKey("WorkflowThread"),"SayHello must contain an OutArgument named WorkflowThread")' Don't know for sure what it is yetDim outarg = output("WorkflowThread")Assert.IsInstanceOfType(outarg, GetType(Int32),"WorkflowThread must be of type Int32")Assert.AreNotEqual(0, output("WorkflowThread"),"WorkflowThread must not be zero")Debug.WriteLine("Test thread is " & _Thread.CurrentThread.ManagedThreadId)Debug.WriteLine("Workflow thread is " & outarg.ToString())End SubNote : Delegates, not eventsWorkflowApplication.Completed and other properties of WorkflowApplication are not events but delegates. Handling them requires you to provide a method, anonymous delegate or lambda expression.
- Press CTRL + SHIFT + B to rebuild the application.
- Press CTRL+R, T to run tests in current context.
- Verify
that the test run passes.The test passed
- Double
click on the test result to view the debug messages about the threads.The test results show the workflow ran on a different thread
No comments:
Post a Comment