-
-
Notifications
You must be signed in to change notification settings - Fork 540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event monitoring is not working properly for asyncronous tests #477
Comments
Thanks for reporting. |
I don't know if it is the same issue but when 2 tasks fire events in a class under test then sometimes MonitorEvents is not catching both. So either in EventMonitor or EventRecordersMap there should be some locking. |
@informatorius I believe it is the same problem. The current API doesn't simply not work with |
I can reproduce the issue and can fix it with lock(object). |
You can't really solve it like that. You might even cause deadlocks. |
My issue is with the MonitorEvents() method called from different threads and I get this exception: I can fix it be using lock(object) at the MonitorEvents() method directly and in the EventRecordersMap.Add method. |
The problem is caused by the use of |
OK then I may have a different issue and proceed in a new issue. |
Please consider the following test:
Sometimes, this test fails by an InvalidOperationException with description "Object <{0}> is not being monitored for events or has already been garbage collected. Use the MonitorEvents() extension method to start monitoring events."
The reason is the
ThreadStatic
-Attribute which is used for decorating the EventRecordersMap in the EventMonitor class.Typically, UnitTests does not have a synchronization context so the code after await will be executed on any idle thread which is not the same thread as the thread that has been started the test. As a result, a new (empty) EventRecordersMap will be created and used when calling sut.ShouldRaisePropertyChangeFor(...).
The text was updated successfully, but these errors were encountered: