This is a migrated thread and some comments may be shown as answers.

Is JustMock threadsafe?

12 Answers 164 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Isaac Abraham
Top achievements
Rank 1
Isaac Abraham asked on 03 May 2013, 10:53 AM
I'm calling a mock on multiple threads. Sometimes this works just fine. But sometimes the mock call fails with a System.ArgumentException "An item with the same key has already been added.". Here's the end of the stack trace: -

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at    .  .   (MethodInfo   )
at    .   .   (MockInvocation    )
at    .   .Intercept(MockInvocation    )
at    .   .Intercept(MockInvocation    )
at    .   .Intercept(IInvocation    )

Now I'm not using any dictionaries in my code - is it possible that this is happening somewhere inside the mock?

12 Answers, 1 is accepted

Sort by
0
Accepted
Mihail
Telerik team
answered on 07 May 2013, 11:27 AM
Hi Isaac,

Recently we've found a multithreading racing issue inside JustMock. The fix is already committed and will be shipped with the next JustMock release. 


Kind regards,
Mihail
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 17 May 2013, 12:04 PM
Any idea when this will be in a public release?

Thanks
0
Kaloyan
Telerik team
answered on 17 May 2013, 12:55 PM
Hi Isaac,

The fix is included in the latest JustMock official build (JustMock 2013 Q1 SP2). Feel free to test if it works as expected for your scenario(s).

Kind regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 17 May 2013, 03:24 PM
I have the latest version from nuget. Here's a stack trace I'm getting - again, not sure if this is internal to JM but I can't hit a breakpoint in my code: -

at Telerik.JustMock.Setup.MethodInstance.  .   (      )
  at System.Collections.Generic.List`1.ForEach(Action`1 action)
  at Telerik.JustMock.Setup.MethodInstance.get_NumerOfTimesExecuted()
  at    .   .   .  ()
  at    .   .Assert(MethodInstance   ,        )
  at    .   .   (MethodCall    )
  at Telerik.JustMock.Mock.   (Occurs    )
  at Telerik.JustMock.Mock.  .   (      )
  at    .   .  [   ](       , Action`1   )
  at Telerik.JustMock.Mock.Assert(Expression`1 expression, Args args, Occurs occurs)

This occurs when calling the following code: -

Mock.Assert(() => mock.Foo(null, null, null), Args.Ignore(), Occurs.Exactly(5));
0
Kaloyan
Telerik team
answered on 17 May 2013, 03:32 PM
Hi again Isaac,

Is it possible to send us a sample project reproducing the issue. This will allow us to investigate it further and provide a proper solution.

Thanks in advance.

Greetings,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 03 Jun 2013, 01:19 PM
Here's a sample of an error I'm seeing with the latest version of JustMock: -

[TestFixture]
public class AsyncTests
{
    [Test]
    public async Task FirstTest()
    {
        var svc = Mock.Create<TestAsyncService>();
        Mock.Arrange(() => svc.DoSomethingAsync()).Returns(Task.Run(() => { }));
 
        await svc.DoSomethingAsync();
 
        Mock.Assert(() => svc.DoSomethingAsync());
    }
 
    [Test]
    public async Task SecondTest()
    {
        var svc = Mock.Create<TestAsyncService>();
        Mock.Arrange(() => svc.DoSomethingAsync()).Returns(Task.Run(() => { }));
 
        await svc.DoSomethingAsync();
 
        Mock.Assert(() => svc.DoSomethingAsync());
    }
}
 
public interface TestAsyncService
{
    Task DoSomethingAsync();
}

With the latest version of JustMock on nuget (2013.2.522), this fails the second test consistently. However, if you roll back to an older version of (2013.1.507), it works.

I've tried this with both Xunit and Nunit, same thing happens.
0
Kaloyan
Telerik team
answered on 06 Jun 2013, 07:18 AM
Hi Isaac,

Thank you for reporting the issue.

We managed to reproduce and fix this. It was a race issue in the free version of JustMock. However, the fix will be included in the next official release.

P.S. For reporting this bug, we have granted you some Telerik points.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 18 Jun 2013, 04:23 PM
Another similar issue I have found is that if you sequential mocking does not appear to be threadsafe i.e. if you set a mocked method to return true the first call and false the second, and fire off two calls to that on different threads, there is a chance that you will get true on both calls.
0
Kaloyan
Telerik team
answered on 20 Jun 2013, 12:34 PM
Hi Isaac,

Thank you for reporting this. We managed to reproduce it on our side and indeed it appears there is a bug in JustMock. We added it into our backlog and a fix should be shipped with the next official JustMock release.

I hope this works for you.

P.S. For reporting the issue, we have granted you some Telerik points.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 01 Aug 2013, 09:49 AM
Is this EVER going to be fixed? Come on guys. Here's a really, REALLY basic example of how the latest version JML (2013.2.611.0) is not threadsafe: -

public class TestClass
{
    [Fact]
    public void TestMethod()
    {
        var generator = Mock.Create<GuidGenerator>();
 
        var lotsOfGuids = Enumerable.Range(0, 10000)
                            .AsParallel()
                            .Select(x => generator.Generate())
                            .ToArray();
 
        // BOOM!
    }
}
 
public interface GuidGenerator
{
    Guid Generate();
}

This fails every time.

Seriously - if this sort of thing is not something I can rely on JM for, let me know and I can look at moving over to a different mocking framework - not a problem.
0
Stefan
Telerik team
answered on 02 Aug 2013, 07:13 AM
Hi Isaac,

Thanks for reporting this bug. I fixed it now. It was just a misplaced [ThreadStatic] attribute. The fix will be made available in the next internal build.

You can rely on us to continuously improve JustMock and in particular JustMock's suitability for asynchronous and parallel tests.

As a token of gratitude for reporting this bug I gave you some Telerik points.

Have a nice day!

Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Isaac Abraham
Top achievements
Rank 1
answered on 14 Aug 2013, 09:34 AM
Confirmed fixed - thanks! :-)
Tags
JustMock Free Edition
Asked by
Isaac Abraham
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Isaac Abraham
Top achievements
Rank 1
Kaloyan
Telerik team
Stefan
Telerik team
Share this question
or