Is JustMock threadsafe?

13 posts, 1 answers
  1. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 03 May 2013 Link to this post

    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?
  2. Answer
    Mihail
    Admin
    Mihail avatar
    75 posts

    Posted 07 May 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 17 May 2013 Link to this post

    Any idea when this will be in a public release?

    Thanks
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 17 May 2013 Link to this post

    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.
  6. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 17 May 2013 Link to this post

    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));
  7. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 17 May 2013 Link to this post

    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.
  8. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 03 Jun 2013 Link to this post

    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.
  9. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Jun 2013 Link to this post

    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.
  10. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 18 Jun 2013 Link to this post

    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.
  11. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 20 Jun 2013 Link to this post

    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.
  12. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 01 Aug 2013 Link to this post

    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.
  13. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 02 Aug 2013 Link to this post

    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.
  14. Isaac Abraham
    Isaac Abraham avatar
    21 posts
    Member since:
    Sep 2009

    Posted 14 Aug 2013 Link to this post

    Confirmed fixed - thanks! :-)
Back to Top
DevCraft R3 2016 release webinar banner