I have run into some multithreading trouble when using Rhino Mocks
(3.5), and have written example code to reproduce it.

When the code is executed from NUnit Gui, it always (as good as)
freezes at the first attempt. After stopping the test, and running it
again, it almost always completes without a problem.

The cause does not seem to be NUnit, as the test also freezes when
called from a windows forms app.

The variable "calls" may have to be increased to 50 or 100 to
reproduce the problem on different machines, but on my dev machine
(w7, dual core, 3gb) I can set it as low as 2, and it still freezes.
On a virtual XP machine (VirtualBox) with 512 mb ram, it seems to work
better, the number of calls have to be increased to consistently get
the freeezing in the same way.

Also note that when the expected number of calls to the mocked objects
is set to "Repeat.Any()" it never freezes! It makes me feel there
might bug in Rhino Mocks. Or have i done something completely wrong?

Here's the code to reproduce the problem:



using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;

namespace Threading.Tests
{
    [TestFixture]
    public class ThreadedTest
    {
        [Test]
        public void RhinoFreeze()
        {
            int calls = 5;

            MockRepository mocks = new MockRepository();
            ICalculator calculator = mocks.StrictMock<ICalculator>();
            IStorage storage = mocks.StrictMock<IStorage>();

            using (mocks.Record())
            {
                for (int i = 0; i < calls; i++)
                {
 
Expect.Call(calculator.Calculate(i)).Return(i);//.Repeat.Any();
                    storage.Store(i);
                    //LastCall.Repeat.Any();
                }
            }

            CalculationGenerator user = new
CalculationGenerator(calculator, storage);
            using (mocks.Playback())
            {
                user.CalculateAndStoreAsync(calls);
            }
        }
    }

    public class CalculationGenerator
    {
        ICalculator calculator;
        IStorage storage;

        public CalculationGenerator(ICalculator calculator, IStorage
storage)
        {
            this.calculator = calculator;
            this.storage = storage;
        }

        public void CalculateAndStoreAsync(int times)
        {
            Action<int> action = CalculateAndStore;
            List<IAsyncResult> asyncs = new List<IAsyncResult>(times);
            for (int i = 0; i < times; i++)
            {
                asyncs.Add(action.BeginInvoke(i, null, null));
            }

            foreach (var async in asyncs)
            {
                action.EndInvoke(async);
            }
        }

        private void CalculateAndStore(int i)
        {
            int result = this.calculator.Calculate(i);
            this.storage.Store(result);
        }
    }

    public interface IStorage
    {
        void Store(int i);
    }

    public interface ICalculator
    {
        int Calculate(int i);
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to