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

System.InvalidOperationException : Collection was modified; enumeration operation may not execute.

5 Answers 1269 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sreejith
Top achievements
Rank 1
Sreejith asked on 11 Oct 2019, 09:12 PM
01.
[Fact]
02.public void Reconcile_LocationGroupChangedEvent_ReconcileSuccessfully()
03.{
04.    using (var container = new MockingContainer<Assignment>(settings))
05.    {
06.        //Arrange
07.        container.Arrange<IFacade>(x =>x.Initialize()).OccursAtMost(1);
08.                 
09.        //Act
10.        container.Instance.Reconcile();
11. 
12.        //Assert
13.        container.AssertAll();
14.    }
15.}
16. 
17.// Method under test
18. 
19.public void Reconcile()
20.
21.    Task.Factory.StartNew(() => facade.Initialize());
22.}

 

I am getting intermittent errors when running the above Test. It fails so infrequently that I am unable to debug. When it fails, here is what I see in the logs.

Reconcile_Test [FAIL]
System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
Stack Trace:
     at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
     at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.<SelectManyIterator>d__23`3.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Telerik.JustMock.Core.MocksRepository.GetDebugView(Object mock)
     at Telerik.JustMock.DebugView.<>c.<get_CurrentState>b__1_0()
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
     at Telerik.JustMock.DebugView.TraceEvent(IndentLevel traceLevel, Func`1 message)
     at Telerik.JustMock.Core.Context.MockingContext.ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
     at Telerik.JustMock.Helpers.FluentHelper.<>c__DisplayClass13_0`1.<AssertAll>b__0()
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
     at Telerik.JustMock.AutoMock.MockResolver.ForEachMock(Action`1 action)
     at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal(Action guardedAction)
  E:\agt01\COM-CN-JOB1\AssignmentTest.cs(14,0): at AssignmentTest.Reconcile_Test()

5 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 14 Oct 2019, 11:39 AM

Hello Sreejith,

Most probably the issue is caused by the asynchronous call inside the method under test. I was unable to reproduce such behavior using the code snippet you have sent combined with some assumptions around it. It is not clear enough what exactly is the use case, so I would like to ask you to share more details. Please clarify also, whether the test is failing when it runs in bundle with others or just standalone? As far as I know, xUnit executes tests in parallel, so it could be also a possible source of troubles. It would be nice to share what version of JustMock you are using free or commercial?

Regards,


Ivo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sreejith
Top achievements
Rank 1
answered on 14 Oct 2019, 04:02 PM

Hi Ivo,

This test is failing intermittently in our build server. There, tests are run sequentially if it is in a single file, but we run different test classes in parallel.

In my local machine, it is more or less impossible to reproduce.

I am using JustMock free version 2018.1.117.2

0
Ivo
Telerik team
answered on 15 Oct 2019, 09:29 AM

Hello Sreejith, 

I am afraid that without stable reproduction it would be hard to identify the root cause of the issue. However, I encourage you to try the latest version, since R1 2018 we have addressed a few fixes regarding multithreading, they could resolve this particular problem too. 

I hope the provided information helps. Any other relevant information that might help for the reproduction and further investigation is also appreciated.  

Regards,


Ivo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
jh-bkraemer
Top achievements
Rank 1
Iron
answered on 15 Feb 2022, 09:52 AM | edited on 15 Feb 2022, 09:55 AM

We have the same issue appearing with the latest version:

15-Feb-2022 10:28:48	  Error Message:
15-Feb-2022 10:28:48	   System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
15-Feb-2022 10:28:48	  Stack Trace:
15-Feb-2022 10:28:48	     at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
15-Feb-2022 10:28:48	   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
15-Feb-2022 10:28:48	   at System.Linq.Enumerable.SelectManyIterator[TSource,TCollection,TResult](IEnumerable`1 source, Func`2 collectionSelector, Func`3 resultSelector)+MoveNext()
15-Feb-2022 10:28:48	   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
15-Feb-2022 10:28:48	   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
15-Feb-2022 10:28:48	   at Telerik.JustMock.Core.MocksRepository.GetDebugView(Object mock)
15-Feb-2022 10:28:48	   at Telerik.JustMock.DebugView.<>c.<get_CurrentState>b__1_0()
15-Feb-2022 10:28:48	   at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
15-Feb-2022 10:28:48	   at Telerik.JustMock.DebugView.TraceEvent(IndentLevel traceLevel, Func`1 message)
15-Feb-2022 10:28:48	   at Telerik.JustMock.Core.Context.MockingContext.ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
15-Feb-2022 10:28:48	   at Telerik.JustMock.Mock.<>c__DisplayClass1_0`1.<Arrange>b__0()
15-Feb-2022 10:28:48	   at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)

In the console runner, we are not expecting the DebugView to do anything. Especially not trying to run over IEnumerables that per definition might only be runnable once. Is it possible to disable the DebugView when not needed to avoid issues like this? Your internals seem to access DebugView.CurrentState which leads to this issues.

It is triggered by a simple call, that is not related to anything. So this seems to be some behind-the-scene issue.

Mock.Arrange(() => Task.Delay(Arg.AnyInt, Arg.IsAny<CancellationToken>())).Returns(Task.CompletedTask);

0
Ivo
Telerik team
answered on 15 Feb 2022, 10:07 AM

Hello Benjamin,

Many thanks for your findings, they could be extremely helpful for us to find the root cause of the issue. I am going to experiment a little bit and come back with a conclusion in a day or two.

Regards,
Ivo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Karina
Top achievements
Rank 1
commented on 15 Feb 2022, 04:09 PM

Hello, 

Just to give more input, the problem Benjamin describes is only happening when the DebugView is enabled.

DebugView.IsTraceEnabled = true;

Regards.

Ivo
Telerik team
commented on 16 Feb 2022, 08:10 AM

Hello Karina, thanks a lot for the input, it puts another important detail to the whole picture.
Tags
General Discussions
Asked by
Sreejith
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Sreejith
Top achievements
Rank 1
jh-bkraemer
Top achievements
Rank 1
Iron
Share this question
or