It's complicated. Racial conditions are so disgusting precisely because they are so difficult to reproduce. Inside save , something like this happens:
- Validate
- Write to the database.
So, to reproduce the synchronization problem, you will need to place two save calls to overlap like this (pseudo-Rails):
a.validate # first half of a.save b.validate # first half of b.save a.write_to_db # second half of a.save b.write_to_db # second half of b.save
but you cannot open the save method and it is quite easy to script with your internal elements.
But (and this is big, but), you can completely skip validations :
Please note that save also has the ability to skip checks if passed :validate => false as an argument. This method should be used with caution.
So if you use
b.save(:validate => false)
you should only get a "write to the database" at half b save and send your data to the database without checking. This should cause a database constraint violation, and I'm sure it will raise ActiveRecord :: StatementInvalid , so I think you will need to look for an exception, not just a false return from save :
b.save(:validate => false).should raise_exception(ActiveRecord::StatementInvalid)
You can tighten this to look for a specific exception message. I have nothing convenient for testing this test, so try it in the Rails console and configure the settings accordingly.
source share