Eventuous / eventuous

Event Sourcing library for .NET
https://eventuous.dev
Apache License 2.0
447 stars 71 forks source link

Get all parameters at once to avoid conflicts in parallel testing #211

Closed Alejandro-SB closed 1 year ago

Alejandro-SB commented 1 year ago

This PR should hopefully close #181. A testing branch was created here to check the scenario.

As #181 explains, this problem arises when tests are being executed in parallel, randomly throwing ArgumentNullException in the AllStreamSubscription constructor. After debugging this case, this was narrowed down to the SubscriptionBuilder.ResolveSubscription method. Although I still don't know exactly why, the execution goes as follows. First, the GetConstructors<TOptions> extension method is called, returning the constructors that have TOptions as a parameter. https://github.com/Eventuous/eventuous/blob/3209ab37dafee320fc563620665e0c06f6c15be1/src/Core/src/Eventuous.Subscriptions/Registrations/SubscriptionBuilder.cs#L194 This extension method returns the constructor and also the TOptions ParameterInfo in a tuple. Then, in line 216, GetParameters is called again to get this constructor parameters and map them to their correct value. https://github.com/Eventuous/eventuous/blob/3209ab37dafee320fc563620665e0c06f6c15be1/src/Core/src/Eventuous.Subscriptions/Registrations/SubscriptionBuilder.cs#L216 Finally, in line 224, a check is run to see if the parameter being mapped to its correct value is the options parameter, to get them from the IMonitorOptions. https://github.com/Eventuous/eventuous/blob/3209ab37dafee320fc563620665e0c06f6c15be1/src/Core/src/Eventuous.Subscriptions/Registrations/SubscriptionBuilder.cs#L224

For some reason, when working in parallel, although they refer to the same parameter, they are completely different instances and don't match, omitting this check and returning null. To fix it, this PR returns only one array with the parameters of the constructor, avoiding the second call. In the testing branch mentioned earlier, this behaviour can be replicated with ease. If line 220 is uncommented, test will start to fail, as now the GetParameters() method is returning different instances of the parameter. Also, a System.Diagnostics.Debugger.Launch() is set in line 252 to debug this problem.

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.