Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:32:11+00:00 2026-05-31T14:32:11+00:00

We’re currently facing some issues during Unit Testing. Our class is multithreading some function

  • 0

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:

public class Bar
{
    private readonly List<IFoo> _fooList;

    public Bar(List<IFoo> fooList)
    {
        _fooList = fooList;
    }

    public void Start()
    {
        var allTasks = new List<Task>();
        foreach (var foo in _fooList)
            allTasks.Add(Task.Factory.StartNew(() => foo.DoSomething()));

        Task.WaitAll(allTasks.ToArray());
    }
}

The Interface IFoo is defined as:

public interface IFoo
{
    void DoSomething();
    event EventHandler myEvent;
}

To reproduce the deadlock, our unittest does the following:
1. create some IFoo Mocks
2. Raise myEvent when DoSomething() gets called.

[TestMethod]
    public void Foo_RaiseBar()
    {
        var fooList = GenerateFooList(50);

        var target = new Bar(fooList);
        target.Start();
    }

    private List<IFoo> GenerateFooList(int max)
    {
        var mocks = new MockRepository();
        var fooList = new List<IFoo>();

        for (int i = 0; i < max; i++)
            fooList.Add(GenerateFoo(mocks));

        mocks.ReplayAll();
        return fooList;
    }

    private IFoo GenerateFoo(MockRepository mocks)
    {
        var foo = mocks.StrictMock<IFoo>();

        foo.myEvent += null;
        var eventRaiser = LastCall.On(foo).IgnoreArguments().GetEventRaiser();

        foo.DoSomething();
        LastCall.On(foo).WhenCalled(i => eventRaiser.Raise(foo, EventArgs.Empty));

        return foo;
    }

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

[In a sleep, wait, or join]
Rhino.Mocks.DLL!Rhino.Mocks.Impl.RhinoInterceptor.Intercept(Castle.Core.Interceptor.IInvocation
invocation) + 0x3d bytes

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.

 [MethodImpl(MethodImplOptions.Synchronized)]
    public void Intercept(IInvocation invocation)

Are we doing something completely wrong on setting up our Mockobjects or using them in a multithreaded environment? Any help or hint is welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:32:12+00:00Added an answer on May 31, 2026 at 2:32 pm

    This is a race condition in your code and not a bug in RhinoMocks. The problem occurs when you are setting up the allTasks task list in the Start() method:

    public void Start() 
    { 
        var allTasks = new List<Task>(); 
        foreach (var foo in _fooList) 
            // the next line has a bug
            allTasks.Add(Task.Factory.StartNew(() => foo.DoSomething())); 
    
        Task.WaitAll(allTasks.ToArray()); 
    } 
    

    You need to pass the foo instance explicitly into the task. The task will execute on a different thread and it’s very likely that the foreach loop will replace the value of foo before the task has started.

    This means that each foo.DoSomething() is being invoked sometimes never and sometimes more than once. For this reason, some of the tasks will block indefinitely because RhinoMocks can’t handle overlapped raising of events on the same instance from different threads and it gets into a deadlock.

    Replace this line in your Start method:

    allTasks.Add(Task.Factory.StartNew(() => foo.DoSomething())); 
    

    With this:

    allTasks.Add(Task.Factory.StartNew(f => ((IFoo)f).DoSomething(), foo));
    

    This is a classic bug that is subtle and very easy to overlook. It is sometimes referred to as “accessing a modified closure”.

    PS:

    Following the comments on this post, I rewrote this test using Moq. In this case it doesn’t block – but beware that expectations created on a given instance might not be satisfied unless the original bug is fixed as described. GenerateFoo() using Moq looks like this:

    private List<IFoo> GenerateFooList(int max)
    {
        var fooList = new List<IFoo>();
    
        for (int i = 0; i < max; i++)
            fooList.Add(GenerateFoo());
    
        return fooList;
    }
    
    private IFoo GenerateFoo()
    {
        var foo = new Mock<IFoo>();
        foo.Setup(f => f.DoSomething()).Raises(f => f.myEvent += null, EventArgs.Empty);
        return foo.Object;
    }
    

    It’s more elegant than RhinoMocks – and clearly more tolerant of multiple threads raising events on the same instance simultaneously. Although I don’t imagine this is a common requirement – personally I don’t often find scenarios where you can assume the subscribers to an event are thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.