<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Felice Pollano Blog - MoQ</title>
    <link>http://www.felicepollano.com/</link>
    <description>The official Fatica Labs Blog!</description>
    <language>en-us</language>
    <copyright>Felice Pollano</copyright>
    <lastBuildDate>Wed, 21 Mar 2012 15:53:26 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>felice@felicepollano.com</managingEditor>
    <webMaster>felice@felicepollano.com</webMaster>
    <item>
      <trackback:ping>http://www.felicepollano.com/Trackback.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</trackback:ping>
      <pingback:server>http://www.felicepollano.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.felicepollano.com/PermaLink.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</pingback:target>
      <dc:creator>Felice Pollano</dc:creator>
      <wfw:comment>http://www.felicepollano.com/CommentView.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</wfw:comment>
      <wfw:commentRss>http://www.felicepollano.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some days ago I came <a href="http://code.google.com/p/moq/issues/detail?id=249" target="_blank">in
this issue regarding thread non-safety</a> when using <a href="https://github.com/Moq" target="_blank">MoQ</a>.
So I simple create <a href="https://github.com/FelicePollano/Moq4" target="_blank">my
own fork on GitHub</a> and solved the issue, that was really easy to do, and as a
result I obtained a Mock stable even when mocked methods are called from multiple
threads. I created a bounch of test to prove that worked, here below one as a sample:
</p>
        <p>
          <a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_thumb.png" width="429" height="349" />
          </a>
        </p>
        <p>
So with this I made the Mock thread safe at the infrastructure level, that mean no
more strange NullreferenceException and others. <strong><em>But what if we want our
mock not thread safe? </em></strong>I mean there could be situation in which <strong>we
want to ensure the system under test calls a certain method from a single thread</strong>, 
in other word we want the mock to <strong>explicitly require single thread access
to certain methods</strong>. This could happen for example when we are mocking some
UI components, but there is such situations every time the object we are mocking is
intrinsically non thread safe and the SUT is multithreaded. So I extended the MoQ
fluent language from the internal and I obtain something like…  the example below: 
</p>
        <p>
          <a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="moqts" border="0" alt="moqts" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_thumb.png" width="542" height="478" />
          </a>
        </p>
        <p>
So in the setup phase we declare a method ( or a setter, or a getter, as usual ) to
be SingleThread(). This yield a mock throwing when the method is called from a different
thread from the one which did the setup. 
</p>
        <p>
If you are happy with this modifications ( you would for sure find helpful the thread
safety by its own ) feel free to check out <a href="https://github.com/FelicePollano/Moq4" target="_blank">my
code fork on GitHub</a>, in any case I’m trying to have that modification pulled from
the main stream.
</p>
        <img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=ece637df-2668-46c2-b0e7-9d9b1ef8ce64" />
      </body>
      <title>Moq4 thread safety and more</title>
      <guid isPermaLink="false">http://www.felicepollano.com/PermaLink.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</guid>
      <link>http://www.felicepollano.com/2012/03/21/Moq4ThreadSafetyAndMore.aspx</link>
      <pubDate>Wed, 21 Mar 2012 15:53:26 GMT</pubDate>
      <description>&lt;p&gt;
Some days ago I came &lt;a href="http://code.google.com/p/moq/issues/detail?id=249" target="_blank"&gt;in
this issue regarding thread non-safety&lt;/a&gt; when using &lt;a href="https://github.com/Moq" target="_blank"&gt;MoQ&lt;/a&gt;.
So I simple create &lt;a href="https://github.com/FelicePollano/Moq4" target="_blank"&gt;my
own fork on GitHub&lt;/a&gt; and solved the issue, that was really easy to do, and as a
result I obtained a Mock stable even when mocked methods are called from multiple
threads. I created a bounch of test to prove that worked, here below one as a sample:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/clip_image001_thumb.png" width="429" height="349"&gt;&lt;/a&gt; 
&lt;p&gt;
So with this I made the Mock thread safe at the infrastructure level, that mean no
more strange NullreferenceException and others. &lt;strong&gt;&lt;em&gt;But what if we want our
mock not thread safe? &lt;/em&gt;&lt;/strong&gt;I mean there could be situation in which &lt;strong&gt;we
want to ensure the system under test calls a certain method from a single thread&lt;/strong&gt;,&amp;nbsp;
in other word we want the mock to &lt;strong&gt;explicitly require single thread access
to certain methods&lt;/strong&gt;. This could happen for example when we are mocking some
UI components, but there is such situations every time the object we are mocking is
intrinsically non thread safe and the SUT is multithreaded. So I extended the MoQ
fluent language from the internal and I obtain something like…&amp;nbsp; the example below: 
&lt;p&gt;
&lt;a href="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="moqts" border="0" alt="moqts" src="http://www.felicepollano.com/public/WindowsLiveWriter/Moq4threadsafetyandmore_EB20/moqts_thumb.png" width="542" height="478"&gt;&lt;/a&gt; 
&lt;p&gt;
So in the setup phase we declare a method ( or a setter, or a getter, as usual ) to
be SingleThread(). This yield a mock throwing when the method is called from a different
thread from the one which did the setup. 
&lt;/p&gt;
&lt;p&gt;
If you are happy with this modifications ( you would for sure find helpful the thread
safety by its own ) feel free to check out &lt;a href="https://github.com/FelicePollano/Moq4" target="_blank"&gt;my
code fork on GitHub&lt;/a&gt;, in any case I’m trying to have that modification pulled from
the main stream.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.felicepollano.com/aggbug.ashx?id=ece637df-2668-46c2-b0e7-9d9b1ef8ce64" /&gt;</description>
      <comments>http://www.felicepollano.com/CommentView.aspx?guid=ece637df-2668-46c2-b0e7-9d9b1ef8ce64</comments>
      <category>C#</category>
      <category>CodeProject</category>
      <category>MoQ</category>
    </item>
  </channel>
</rss>