RubyMoney/eu_central_bank — pull request #22 by saurabhnanda (2014-01-04 04:47:36+00:00)
Title: Thread safety of EuCentralBank.update_rates & EuCentralBank.exchange

New test + fixtures to ensure that update_rates is atomic in nature. The test spawns 2 threads, one thread keeps updating the @bank object with odd rates and the other with even rates. At no point in time should @bank.rates have, both, odd AND even rates, i.e. either the rates should be all odd, or they should be all even.

Changed EuCentralBank.update_rates to update rates atomically. It creates a temporary bank object, adds rates to it one-by-one and then uses it to replace the rates on `self` using import_rates. We need to do this because import_rates holds a lock on @mutex while ALL rates are being updated. This ensures that rate updation is always atomic in nature -- at no point in time will some other thread see an inconsistent view of the rates, i.e. few rates from this update & few rates from the previous update.

Added another test to check that EuCentralBank.exchange reads the two rates atomically. However, this test is failing, because there is no way to read multiple rates in thread-safe manner without resorting to a by-pass of `VariableExchange.get_rate` in favour of reading directly via `@rates[rate_key_for(...)]`