Concurrent execution vs mocks
Created by: yaauie
On JRuby, we're seeing concurrency errors while validating message expectations when the code under test does work in threads.
There are two separate errors that show up, exemplified with a spec:
- expected count doesn't always match the number of times the message was actually received
- JRuby can throw a
ConcurrencyError
exception with messageDetected invalid array contents due to unsynchronized modifications with concurrent users
when an array is modified by one thread while it is being iterated over in another thread.
This Pull-Request is a proof-of-concept to show the issue and a potential solution.
It adds mutexes to synchronize writes to RSpec::Mocks::MessageExpectation::ImplementationDetail
's @actual_received_count
instance variable and to synchronize all access to RSpec::Mocks::Proxy
's @messages_received
array.
I am not particularly thrilled with the implementation, since it disperses the details of synchronisation across each of the classes and would be pretty easy to accidentally circumvent these mutexes.