<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : Testing</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Testing/default.aspx</link><description>Tags: Testing</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>A Lesson in Serialization</title><link>http://blogs.msdn.com/jaredpar/archive/2008/09/02/a-lesson-in-serialization.aspx</link><pubDate>Tue, 02 Sep 2008 15:00:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8904579</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8904579.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8904579</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8904579</wfw:comment><description>&lt;p&gt;A few days ago, I recklessly added a [Serialization] attribute to a few of my immutable collection types.&amp;#160; I needed to pass data between AppDomain's and adding [Serialization] was the quick and dirty fix.&amp;#160; Compiled, ran and I didn't think much about it.&amp;#160; &lt;/p&gt;  &lt;p&gt;Luckily I was updating some unit tests last night and I remembered this and added a couple of serialization sanity tests.&amp;#160; Most of the tests passed first time but for my ImmutableStack class[1] was throwing an exception.&amp;#160; Well, it was actually my ImmutableQueue but it was failing in one of the inner ImmutableStack instances.&amp;#160; The test code was fairly straight forward&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;stack = &lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;.Create(&lt;span style="color: blue"&gt;new int&lt;/span&gt;[] { 1, 2, 3 });
&lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;stream = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;()) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;f = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryFormatter&lt;/span&gt;();
    f.Serialize(stream,stack);
    stream.Position = 0;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;obj = f.Deserialize(stream);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;stack2 = (&lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;)obj;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;stack3 = stack2.Reverse();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I did a bit of digging and discovered the exception was coming from the stack2.Reverse() call.&amp;#160; Jumped through the code and didn't see much wrong.&amp;#160; I had several existing tests around ImmutableStack.Reverse() and I couldn't see why Serialization would make any difference.&amp;#160; &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;&amp;lt;T&amp;gt; Reverse() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;&amp;lt;T&amp;gt;.Empty;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;current = &lt;span style="color: blue"&gt;this&lt;/span&gt;;
    &lt;span style="color: blue"&gt;while &lt;/span&gt;(current != &lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;&amp;lt;T&amp;gt;.Empty) {
        r = r.Push(current.Peek());
        current = current.Pop();
    }

    &lt;span style="color: blue"&gt;return &lt;/span&gt;r;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Can you see what's wrong with the code?&lt;/p&gt;

&lt;p&gt;It took me a few minutes of debugging and frustration.&amp;#160; The bug is in the while loop conditional.&amp;#160; Until you introduce serialization this code is just fine.&amp;#160; ImmutableStack&amp;lt;T&amp;gt;.Empty is a static readonly declaration.&amp;#160; The code implementation only allows for one to be created so it a singleton and equality can be done with a quick reference check. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EmptyImmutableStack &lt;/span&gt;s_empty = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EmptyImmutableStack&lt;/span&gt;();

&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ImmutableStack&lt;/span&gt;&amp;lt;T&amp;gt; Empty {
    &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;s_empty; }
}&lt;/pre&gt;

&lt;p&gt;Unfortunately serialization breaks the assumption that EmptyImmutableStack is a singleton.&amp;#160; The EmptyImmutableStack class is a singleton by convention only.&amp;#160; It's a private nested class that's only instantiated once per AppDomain.&amp;#160; There is nothing stopping the CLR or Serialization for that matter from creating a second instance.&amp;#160; In the case of deserialization that's exactly what happens.&amp;#160; The serializer isn't built to recognize this pattern and instead simply creates a new instance of EmptyImmutableStack upon deserialization.&amp;#160; &lt;/p&gt;

&lt;p&gt;This essentially prevents you from safely using a functional style Empty pattern inside a serializable collection.&amp;#160; &lt;/p&gt;

&lt;p&gt;The fix is simple enough, alter the conditional to be (!current.IsEmpty).&amp;#160; &lt;/p&gt;

&lt;p&gt;[1] The version of ImmutableStack I'm using is heavily based off of &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx"&gt;Eric Lippert's implementation&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8904579" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Testing/default.aspx">Testing</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Gotcha/default.aspx">Gotcha</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Immutable/default.aspx">Immutable</category></item><item><title>Simple class for tests involving a SynchronizationContext</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/25/simple-class-for-tests-involving-a-synchronizationcontext.aspx</link><pubDate>Mon, 25 Aug 2008 15:00:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8889917</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/8889917.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=8889917</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=8889917</wfw:comment><description>&lt;p&gt;Recently I had to test a class which heavily depended upon a &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx"&gt;SynchronizationContext&lt;/a&gt;.&amp;#160; This threw me off for about half an hour as I didn't want to write multi-threaded unit tests.&amp;#160; Multi-threaded code is difficult enough without adding needless threads.&amp;#160; &lt;/p&gt;  &lt;p&gt;The solution I came up with is simple and gives the unit test a large degree of control over the execution of posted delegates.&amp;#160; The resulting tests were much easier to code and understand. &lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TestSynchronizationContext &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;SynchronizationContext &lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Tuple&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;SendOrPostCallback&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; m_pending 
        = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Tuple&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;SendOrPostCallback&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;();

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Send(&lt;span style="color: #2b91af"&gt;SendOrPostCallback &lt;/span&gt;d, &lt;span style="color: blue"&gt;object &lt;/span&gt;state) {
        d(state);
    }

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Post(&lt;span style="color: #2b91af"&gt;SendOrPostCallback &lt;/span&gt;d, &lt;span style="color: blue"&gt;object &lt;/span&gt;state) {
        m_pending.Add(&lt;span style="color: #2b91af"&gt;Tuple&lt;/span&gt;.Create(d, state));
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;RunAllPosted() {
        m_pending.ForEach(x =&amp;gt; x.First(x.Second));
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8889917" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Testing/default.aspx">Testing</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/SynchronizationContext/default.aspx">SynchronizationContext</category></item><item><title>ISynchronizeInvoke ... now</title><link>http://blogs.msdn.com/jaredpar/archive/2008/01/07/isynchronizeinvoke-now.aspx</link><pubDate>Tue, 08 Jan 2008 05:25:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7022839</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/7022839.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=7022839</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=7022839</wfw:comment><description>&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke.aspx"&gt;ISynchronizeInvoke&lt;/a&gt; is an interface which allows you to execute a delegate synchronously or asynchronously.&amp;nbsp; The implementer of the interface can control how the delegate is executed.&amp;nbsp; In particular the implementer controls on which thread the delegate is executed.&amp;nbsp; &lt;/p&gt; &lt;p&gt;It's common for thread sensitive objects to implement this interface.&amp;nbsp; It allows consumers to execute long lived actions on a background thread and then use the ISynchronizeInvoke interface to get back onto the original thread.&amp;nbsp; System.Windows.Forms.Control is a great example of this usage pattern.&lt;/p&gt; &lt;p&gt;Several API's that I own are very thread aware and will often defer to a background thread for the completion of their operation.&amp;nbsp; It makes it easier to build UI on top of it.&amp;nbsp; They take an ISynchronizedInvoke as a parameter in order to properly signal the caller than an operation is completed.&amp;nbsp; &lt;/p&gt; &lt;p&gt;The difficulty can come in testing it.&amp;nbsp; Many implementers of ISynchronizeInvoke use message pumping to implement the behavior.&amp;nbsp; As a result it's not always easy to use for testing (unit testing in particular).&amp;nbsp; To work around this I designed an implementation of ISynchronizeInvoke that does not rely on the message pumping but provides a completely compliant ISynchronizeInvoke implementation.&amp;nbsp; &lt;/p&gt; &lt;p&gt;The idea is to just do it ... now.&amp;nbsp; I call the class ImmediateInvoke.&amp;nbsp; The basic methods are straight forward.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ISynchronizeInvoke&lt;/span&gt;.Invoke(&lt;span style="color: rgb(43,145,175)"&gt;Delegate&lt;/span&gt; method, &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt;[] args)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; method.DynamicInvoke(args);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ISynchronizeInvoke&lt;/span&gt;.InvokeRequired
        {
            &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;; }
        }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The other two methods are a little more tricky.&amp;nbsp; The require an implementation of &lt;a href="http://msdn2.microsoft.com/en-us/library/system.iasyncresult.aspx"&gt;IAsyncResult&lt;/a&gt;.&amp;nbsp; The basic usage pattern is the consumer will call BeginInvoke, peform some operations and finally call EndInvoke when it wants to join with the asynchronous operation.&amp;nbsp; I will use the thread pool to perform this operation and define a private nested class for the IAsnycResult implementation called AsyncResult&lt;/p&gt;
&lt;p&gt;The implementation needs a few variables to implement the contract. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;m_handle - An implementation of ManualResetEvent to satisfy the AsyncWaitHandle property&lt;/li&gt;
&lt;li&gt;m_completed - A simple int to capture whether or not we have completed&lt;/li&gt;
&lt;li&gt;m_return - Return of the delegate&lt;/li&gt;
&lt;li&gt;m_exception - Exception thrown by calling the delegate.&amp;nbsp; &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Most of the properties are straight forward.&lt;/p&gt;&lt;pre class="code"&gt;            &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; AsyncState
            {
                &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;; }
            }

            &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; System.Threading.&lt;span style="color: rgb(43,145,175)"&gt;WaitHandle&lt;/span&gt; AsyncWaitHandle
            {
                &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; m_handle; }
            }

            &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; CompletedSynchronously
            {
                &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;; }
            }

            &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; IsCompleted
            {
                &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; m_completed == 1; }
            }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Now we need to define a method to run the delegate passed to BeginInvoke in the thread pool and update the state as we go along.&amp;nbsp; I call this method directly from the constructor.&lt;/p&gt;&lt;pre class="code"&gt;            &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; RunDelegateAsync(&lt;span style="color: rgb(43,145,175)"&gt;Delegate&lt;/span&gt; method, &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt;[] args)
            {
                &lt;span style="color: rgb(43,145,175)"&gt;WaitCallback&lt;/span&gt; del = &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt;(&lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; unused)
                {
                    &lt;span style="color: rgb(0,0,255)"&gt;try
&lt;/span&gt;                    {
                        &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; temp = method.DynamicInvoke(args);
                        &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_return, temp);
                    }
                    &lt;span style="color: rgb(0,0,255)"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex)
                    {
                        &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_exception, ex);
                    }

                    &lt;span style="color: rgb(43,145,175)"&gt;Interlocked&lt;/span&gt;.Exchange(&lt;span style="color: rgb(0,0,255)"&gt;ref&lt;/span&gt; m_completed, 1);
                    m_handle.Set();
                };

                &lt;span style="color: rgb(43,145,175)"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(del);
            }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Notice I've avoided using a lock in this implementation.&amp;nbsp; This is safe for this case.&amp;nbsp; All of the members are set atomically.&amp;nbsp; Only m_completed can be accessed before the operation is completed and it is simply checked for the value 1.&amp;nbsp; Since the value is set atomically this is safe.&amp;nbsp; In the implementation of EndInvoke I will not access any of the other variables until the WaitHandle is signaled and then I will not make any decision based on the contents of their values (rather the abscence or presence).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Also notice that I did not explicitly dispose of the WaitHandle.&amp;nbsp; This is a quirk of the ISynchronizeInvoke interface.&amp;nbsp; It specifies that callers of BeginInvoke must call EndInvoke and that the IAsyncResult must be valid until EndInvoke is called.&amp;nbsp; As such you can't really free a resource inside of the IAsyncResult implementation.&amp;nbsp; In fact if you implement IDisposable, who will see it (generally not possible since C# and VB don't support covariant return types).&amp;nbsp; Instead you should free it as part of your EndInvoke implementation.&lt;/p&gt;
&lt;p&gt;Now BeginInvoke and EndInvoke.&lt;/p&gt;&lt;pre class="code"&gt;        &lt;span style="color: rgb(43,145,175)"&gt;IAsyncResult&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ISynchronizeInvoke&lt;/span&gt;.BeginInvoke(&lt;span style="color: rgb(43,145,175)"&gt;Delegate&lt;/span&gt; method, &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt;[] args)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;AsyncResult&lt;/span&gt;(method, args);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ISynchronizeInvoke&lt;/span&gt;.EndInvoke(&lt;span style="color: rgb(43,145,175)"&gt;IAsyncResult&lt;/span&gt; result)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; r = (&lt;span style="color: rgb(43,145,175)"&gt;AsyncResult&lt;/span&gt;)result;
            &lt;span style="color: rgb(0,0,255)"&gt;try
&lt;/span&gt;            {
                r.AsyncWaitHandle.WaitOne();
            }
            &lt;span style="color: rgb(0,0,255)"&gt;finally
&lt;/span&gt;            {
                r.AsyncWaitHandle.Close();
            }

            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (r.m_exception != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"Error during BeginInvoke"&lt;/span&gt;, r.m_exception);
            }
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; r.m_return;
        }&lt;/pre&gt;
&lt;p&gt;Unfortunately EndInvoke has to take care of two cases.&amp;nbsp; The first is the delegate completed successfully and produced a value which can now be returned as a part of the interface contract.&amp;nbsp; The other case is when the delegate throws and exception and it's a bit more tricky.&amp;nbsp; The exception cannot be simply re-thrown because you will loose all of the original call stack and generally speaking most of the data which would help track down the problem.&amp;nbsp; The better option is to throw a new exception and make this exception the inner exception.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This sample could be improved in a few ways (delay create the WaitHandle, rethrow with something other than System.Exception).&amp;nbsp; But it's a compliant version that gets the job done.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7022839" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Testing/default.aspx">Testing</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/ISynchronizeInvoke/default.aspx">ISynchronizeInvoke</category></item><item><title>Testing your code</title><link>http://blogs.msdn.com/jaredpar/archive/2007/09/26/testing-your-code.aspx</link><pubDate>Thu, 27 Sep 2007 00:29:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5152904</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5152904.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5152904</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5152904</wfw:comment><description>&lt;p&gt;This is a little off topic from my typical purely technical discussion.&lt;/p&gt; &lt;p&gt;I'm a huge advocate of automated code verification.&amp;nbsp; Unit testing is preferred but really any method is acceptable as long as it gets the job done of testing your code.&amp;nbsp; IMHO, testing is the most important part of a developers job.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Besides the 3 or 4 projects I am doing as part of my job at Microsoft, I work on &lt;strong&gt;numerous&lt;/strong&gt; side projects.&amp;nbsp; Most of these come in the form of tools, libraries or little fun gadgets.&amp;nbsp; These are written in a variety of languages; C#, VB, C++, Powershell Script, etc ...&amp;nbsp; &lt;/p&gt; &lt;p&gt;This is pretty common amongst many devs I know.&amp;nbsp; We love our job and code before, during and after work.&amp;nbsp; Some of the devs I know don't bother automating testing of any of their tools.&amp;nbsp; When asked they generally respond that it's not worth it.&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;br&gt;I disagree strongly for certain types of tools.&amp;nbsp; For tools and libraries I own that I expect long term usage on, I test methodically.&amp;nbsp; On any given day I may update a couple of the tools.&amp;nbsp; The only item that adds sanity to the situation is testing.&amp;nbsp; This allows me to respond quickly to customer requests for features, bug fixes, etc&amp;nbsp; and get quick verification that I haven't introduced a regression.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5152904" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Testing/default.aspx">Testing</category></item></channel></rss>