<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Unit Testing</title>
        <link>http://geekswithblogs.net/EltonStoneman/category/8936.aspx</link>
        <description>Unit Testing</description>
        <language>en-GB</language>
        <copyright>EltonStoneman</copyright>
        <managingEditor>elton.stoneman@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Verify Long-Running Tests with Retrying Asserts</title>
            <link>http://geekswithblogs.net/EltonStoneman/archive/2008/10/31/verify-long-running-tests-with-retrying-asserts.aspx</link>
            <description>&lt;p style="text-align: center;"&gt;&lt;span style="font-size: 10pt;"&gt;[Source: &lt;a href="http://geekswithblogs.net/EltonStoneman"&gt;http://geekswithblogs.net/EltonStoneman&lt;/a&gt;] &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;There are a few situations when you're unit testing a component which calls out to another worker – a SQL insert from a separate thread, or a file drop from an asynchronous service call. You want to verify that the worker's completed correctly, but don't want to make the test run any longer than needed. Previously I've used bespoke code in tests where this was required, but the number has been increasingly recently so I've added a &lt;strong&gt;RetryAssert&lt;/strong&gt; method to our fixture base class, for tests to use like this: &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.RetryAssert(&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;10&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"File not found"&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;Assertion&lt;/span&gt;(&lt;span style="color: blue;"&gt;delegate&lt;/span&gt;() { &lt;span style="color: blue;"&gt;return&lt;/span&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;File&lt;/span&gt;.Exists(&lt;span style="color: rgb(163, 21, 21);"&gt;"c:\\temp.txt"&lt;/span&gt;); })); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RetryAssert&lt;/strong&gt; takes an Assertion delegate (an anonymous one in this example) and repeatedly invokes it, pausing for the specified retry interval between calls, until the specified timeout is reached or the assertion returns true. At the first true assertion, the test is passed. If the assertion is false for every call until the timeout is reached, the test is failed.   &lt;/p&gt;
&lt;p&gt;The code is straightforward, but handy to have in your base class: &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Runs the given assertion repeatedly, until it returns true or the operation times out.  &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; If the assertion returns true at any point, the call issues passes the test; if the assertion  &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; fails every point up to the timeout, the call issues Assert.Fail.  &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="retryInterval"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Interval between assertion calls, in seconds&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="timeout"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Tiemout before test is failed, in seconds&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="failureMessage"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Message to write if test fails&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="assertion"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Assertion delegate to run&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; 			&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; RetryAssert(&lt;span style="color: blue;"&gt;int&lt;/span&gt; retryInterval, &lt;span style="color: blue;"&gt;int&lt;/span&gt; timeout, &lt;span style="color: blue;"&gt;string&lt;/span&gt; failureMessage, &lt;span style="color: rgb(43, 145, 175);"&gt;Assertion&lt;/span&gt; assertion)  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;{  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;bool&lt;/span&gt; assert = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;.StartAssertTimer(timeout * &lt;span style="color: red;"&gt;1000&lt;/span&gt;);  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;while&lt;/span&gt; (!assert &amp;amp;&amp;amp; !&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimeoutReached)  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;    {  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        assert = assertion();  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;if&lt;/span&gt; (assert)  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        {  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;.StopAssertTimer();  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;break&lt;/span&gt;;  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        }  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;else&lt;/span&gt; 		&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        {  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(retryInterval * &lt;span style="color: red;"&gt;1000&lt;/span&gt;);  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        }  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;    }  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.IsTrue(assert, failureMessage);  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; 			&lt;span style="color: blue;"&gt;delegate&lt;/span&gt; 			&lt;span style="color: blue;"&gt;bool&lt;/span&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;Assertion&lt;/span&gt;(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 			&lt;span style="color: blue;"&gt;bool&lt;/span&gt; _assertTimeoutReached; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; System.Timers.&lt;span style="color: rgb(43, 145, 175);"&gt;Timer&lt;/span&gt; _assertTimer; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; StartAssertTimer(&lt;span style="color: blue;"&gt;double&lt;/span&gt; timeoutMilliseconds)  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;{  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimeoutReached = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; System.Timers.&lt;span style="color: rgb(43, 145, 175);"&gt;Timer&lt;/span&gt;(timeoutMilliseconds);  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimer.Start();  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimer.Elapsed += &lt;span style="color: blue;"&gt;new&lt;/span&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;ElapsedEventHandler&lt;/span&gt;(assertTimer_Elapsed);  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; assertTimer_Elapsed(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;ElapsedEventArgs&lt;/span&gt; e)  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;{  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;.StopAssertTimer();  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimeoutReached = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;}  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; StopAssertTimer()  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;{  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimer.Stop();  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;this&lt;/span&gt;._assertTimer = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;}  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;
&lt;p&gt;Note that this is a straightforward example and isn't threadsafe, but is fine for the majority of cases. &lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126446"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=126446" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/EltonStoneman/aggbug/126446.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>EltonStoneman</dc:creator>
            <guid>http://geekswithblogs.net/EltonStoneman/archive/2008/10/31/verify-long-running-tests-with-retrying-asserts.aspx</guid>
            <pubDate>Sat, 01 Nov 2008 01:47:31 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/EltonStoneman/comments/126446.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/EltonStoneman/archive/2008/10/31/verify-long-running-tests-with-retrying-asserts.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/EltonStoneman/comments/commentRss/126446.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>