Lets set the stage for this question.
Here is our model:
class Deal < ActiveRecord::Base
belongs_to :retailer
has_many :content_locations, as: :locatable
has_many :stores, through: :content_locations
searchable do
text :title
text :store_name
text :store_sort_name
text :description
end
scope :by_keyword, ->(keyword, limit, offset) {
search {
fulltext keyword
paginate offset: offset, per_page: limit
}.results
}
end
Here is the section of our rspec test that hits SOLR:
describe 'searchable' do
before do
DatabaseCleaner.clean
end
target = 'foo'
let!(:deal_with_title) { create :deal, title: target }
let!(:deal_with_description) { create :deal, description: target }
let!(:deal_with_store_name) { create :deal, store_name: target }
let!(:deal_with_store_sort_name) { create :deal, store_sort_name: target }
let!(:deal_with_nothing) {
create :deal, title: 'bar', description: 'bar', store_name: 'bar', store_sort_name: 'bar'
}
it 'includes matches' do
sleep 1
results = Deal.by_keyword(target, 25, 0)
expect(results).to include(deal_with_title)
expect(results).to include(deal_with_description)
expect(results).to include(deal_with_store_name)
expect(results).to include(deal_with_store_sort_name)
expect(results).to_not include(deal_with_nothing)
end
end
What we are seeing is that our test fails randomly. I can execute this test over and over successfully for 1-3xs but the 4th will fail, or it will fail 4xs and the 5th will fail. The pattern is completely random. As you see we tried adding a sleep to this setup and that doesn't do anything but slow it down.
Any tips would be greatly appreciated here. This has been driving us nuts!