Export to GitHub

moq - issue #381

Multithreading and invocation count


Posted on Oct 6, 2014 by Massive Cat

void Main() { var fooMock = new Mock<IFoo>(); fooMock.Setup(v => v.Foo()).Throws<Exception>();

var tasks = Enumerable.Repeat(0, 20)
                .Select(v =&gt; new Task(() =&gt; fooMock.Object.Foo()))
                .ToArray();

        foreach (var task in tasks)
        {
            task.Start();
        }

Action waitAllAction = () =&gt; Task.WaitAll(tasks);
waitAllAction.ShouldThrow&lt;AggregateException&gt;();

fooMock.Verify(v =&gt; v.Foo(), Times.Exactly(20)); //Verification fails

}

// Define other methods and classes here

public interface IFoo { int Foo(); }

Version: 4.2.1402.2112. Os: Win2008

Am I correct that verification should succeed

Status: New

Labels:
Type-Defect Priority-Medium