Thursday, November 1, 2012

Workflow Application part2

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.

  1. First open HelloWorkFlow project from last article Workflow Application part1.
  2. 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 completes
      workflowApp.Completed = (e) =>
        {
          output = e.Outputs;
          actionThreadID = Thread.CurrentThread.ManagedThreadId;

          // Signal the test thread the workflow is done
          sync.Set();
        };

      workflowApp.Run();

      // Wait for the sync event for 1 second
      sync.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 yet
      var 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 = 0
      Dim output As IDictionary(Of String, Object) = Nothing
      Dim workflowApp As New WorkflowApplication( _
        New SayHello() With {.UserName = "Test"})

      workflowApp.Completed = _
        Function(e)
          output = e.Outputs
          actionThreadID = Thread.CurrentThread.ManagedThreadId

          ' Signal the test thread the workflow is done
          sync.Set()

          ' VB requires a lambda expression to return a value
          ' It is not used with Action(Of T)
          Return Nothing
        End Function

      workflowApp.Run()

      ' Wait for the sync event for 1 second
      sync.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 yet
      Dim 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 Sub
    Note : Delegates, not events
    WorkflowApplication.Completed and other properties of WorkflowApplication are not events but delegates. Handling them requires you to provide a method, anonymous delegate or lambda expression.
  3. Press CTRL + SHIFT + B to rebuild the application.
  4. Press CTRL+R, T to run tests in current context.
  5. Verify that the test run passes.
    The test passed
    The test passed
    1. 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
      The test results show the workflow ran on a different thread

      No comments:

      Post a Comment

      Automatic Traffic Exchange

      YallaTech Facebook page