diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-09-17 17:04:27 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-09-17 17:04:37 +0100 |
commit | 09d6cc3313b9ec1f495ff09c74fe91e1b667f8b9 (patch) | |
tree | 713e2b7695776dd2679bf1a8ac74753a137520db | |
parent | 96b5229241f09f7d35c306d457f36ab2ffdad3b5 (diff) |
Add a failing spec for handling a race condition
The spec uses a hook method to simulate the insertion of an
acts_as_xapian_job in another process for the model.
Credit to: http://stackoverflow.com/questions/2017587/simulating-race-conditions-in-rspec-unit-tests
Conflicts:
vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
-rw-r--r-- | spec/models/xapian_spec.rb | 31 | ||||
-rw-r--r-- | vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb | 6 |
2 files changed, 37 insertions, 0 deletions
diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 7aab9cdc6..c7c21e3a0 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -400,3 +400,34 @@ describe ActsAsXapian::Search, "#words_to_highlight" do end end + +describe InfoRequestEvent, " when faced with a race condition during xapian_mark_needs_index" do + + before(:each) do + load_raw_emails_data + get_fixtures_xapian_index + # Use the before create job hook to simulate a race condition with another process + # by creating an acts_as_xapian_job record for the same model + class InfoRequestEvent + def xapian_before_create_job_hook(action, model, model_id) + ActsAsXapian::ActsAsXapianJob.create!(:model => model, + :model_id => model_id, + :action => action) + end + end + end + + after(:each) do + # Reset the before create job hook + class InfoRequestEvent + def xapian_before_create_job_hook(action, model, model_id) + end + end + end + + it 'should not raise an error but should fail silently' do + ir = info_requests(:naughty_chicken_request) + ir.reindex_request_events + end + +end diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb index e3659b629..a4447371e 100644 --- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb +++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb @@ -929,11 +929,17 @@ module ActsAsXapian def xapian_create_job(action, model, model_id) ActiveRecord::Base.transaction do ActsAsXapianJob.delete_all([ "model = ? and model_id = ?", model, model_id]) + xapian_before_create_job_hook(action, model, model_id) ActsAsXapianJob.create!(:model => model, :model_id => model_id, :action => action) end end + + # A hook method that can be used in tests to simulate e.g. an external process inserting a record + def xapian_before_create_job_hook(action, model, model_id) + end + end ###################################################################### |