title: Multithreaded code makes Rhino Mocks cause a Deadlock
body: <p>We're currently facing some issues during Unit Testing. Our class is multithreading some function calls on Mocked objects using Rhino Mocks. Here's a example reduced to the minimum:</p>
  
  <pre><code>public class Bar
  {
      private readonly List&lt;IFoo&gt; _fooList;
  
      public Bar(List&lt;IFoo&gt; fooList)
      {
          _fooList = fooList;
      }
  
      public void Start()
      {
          var allTasks = new List&lt;Task&gt;();
          foreach (var foo in _fooList)
              allTasks.Add(Task.Factory.StartNew(() =&gt; foo.DoSomething()));
  
          Task.WaitAll(allTasks.ToArray());
      }
  }
  </code></pre>
  
  <p>The Interface IFoo is defined as:</p>
  
  <pre><code>public interface IFoo
  {
      void DoSomething();
      event EventHandler myEvent;
  }
  </code></pre>
  
  <p>To reproduce the deadlock, our unittest does the following:
  1. create some IFoo Mocks
  2. Raise myEvent when DoSomething() gets called.</p>
  
  <pre><code>[TestMethod]
      public void Foo_RaiseBar()
      {
          var fooList = GenerateFooList(50);
  
          var target = new Bar(fooList);
          target.Start();
      }
  
      private List&lt;IFoo&gt; GenerateFooList(int max)
      {
          var mocks = new MockRepository();
          var fooList = new List&lt;IFoo&gt;();
  
          for (int i = 0; i &lt; max; i++)
              fooList.Add(GenerateFoo(mocks));
  
          mocks.ReplayAll();
          return fooList;
      }
  
      private IFoo GenerateFoo(MockRepository mocks)
      {
          var foo = mocks.StrictMock&lt;IFoo&gt;();
  
          foo.myEvent += null;
          var eventRaiser = LastCall.On(foo).IgnoreArguments().GetEventRaiser();
  
          foo.DoSomething();
          LastCall.On(foo).WhenCalled(i =&gt; eventRaiser.Raise(foo, EventArgs.Empty));
  
          return foo;
      }
  </code></pre>
  
  <p>The more Foo's are generated, the more often the deadlock occurs. If the test won't block, run it several times, and it will.
  Stopping the debugging testrun shows, that all Tasks are still in TaskStatus.Running and the current worker thread is breaking at </p>
  
  <blockquote>
    <p>[In a sleep, wait, or join]<br>
      Rhino.Mocks.DLL!Rhino.Mocks.Impl.RhinoInterceptor.Intercept(Castle.Core.Interceptor.IInvocation
    invocation) + 0x3d bytes</p>
  </blockquote>
  
  <p>The weird thing which confuses us most is the fact, that the signature of the Intercept(...) Method is defined as Synchronized - but several Threads are located here. I've read several postings about Rhino Mocks and Multithreaded, but havn't found warnings (expected setting up the records) or limitations.</p>
  
  <pre><code> [MethodImpl(MethodImplOptions.Synchronized)]
      public void Intercept(IInvocation invocation)
  </code></pre>
  
  <p>Are we doing something completely wrong on setting up our Mockobjects or using them in a multithreaded environment? Any help or hint is welcome!</p>