anshooarora / extentreports-csharp

Community version of Extent API for .NET has moved to https://github.com/extent-framework/
http://extentreports.com/
Other
47 stars 43 forks source link

Concurrent logging causes "Collection was modified" error #75

Closed mhgames closed 7 years ago

mhgames commented 7 years ago

I've never had issues with Java implementation of ExtentReports (version 2.4) running Selenium tests in parallel in the past.

But currently I'm running some API tests on C# implementation of ExtentReports in parallel and these tests are very fast, so when I'm calling .Log(...) on ExtentTest, I end up (from time to time) with System.InvalidOperationException (Collection was modified; enumeration operation may not execute.)

Here's part of the stacktrace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List1.ForEach(Action1 action) at AventStack.ExtentReports.Model.Test.UpdateTestStatusRecursive(Test test) at AventStack.ExtentReports.Model.Test.End() at AventStack.ExtentReports.Model.Test.<>c.b__73_0(Test x) at System.Collections.Generic.List1.ForEach(Action1 action) at AventStack.ExtentReports.Model.Test.EndChildTestsRecursive(Test test) at AventStack.ExtentReports.Model.Test.End() at AventStack.ExtentReports.Model.Report.EndTest(Test test) at AventStack.ExtentReports.Model.Report.b__19_0(Test test) at System.Collections.Generic.List1.ForEach(Action1 action) at AventStack.ExtentReports.Model.Report.CollectRunInfo() at AventStack.ExtentReports.Model.Report.AddLog(Test test, Log log) at AventStack.ExtentReports.ExtentTest.AddLog(Log evt) at AventStack.ExtentReports.ExtentTest.Log(Status status, String details, MediaEntityModelProvider provider)

Since it's an error on System.Collections.Generic.List, it could imho be solved in your AbstractStructure.cs file with ConcurrentDictionary instead of List _list, with integer TKey as array index and TValue as array value, since there's no ConcurrentList in C# yet and ConcurrentBag is unordered.

mhgames commented 7 years ago

I'm currently using locks to get rid of this message, but apparently the more tests I have, the slower the run is, since the ForEach takes longer and longer.

anshooarora commented 7 years ago

Can you please try with the attached dll. Any luck?

ExtentReports.zip

mhgames commented 7 years ago

I removed all the locks and it works, no error. Also the profiler no longer says that ExtentReports are causing performance issues. Thanks!