Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created April 16, 2012 12:03
Show Gist options
  • Save joakimk/2398241 to your computer and use it in GitHub Desktop.
Save joakimk/2398241 to your computer and use it in GitHub Desktop.
Our sphinx testing config, the result of many iterations to make it fast and reliable.
# Use this with https://gist.github.com/2397889 to make it much more stable.
# Usage:
# 1) Put this in spec/support/rspec_sphinx.rb
# 2) In your spec_helper.rb: "RSpec::ThinkingSphinx.setup(config)"
# Expects a spec_helper like:
#
# require 'database_cleaner'
#
# RSpec.configure do |config|
# config.use_transactional_fixtures = false
#
# config.before(:each) do
# DatabaseCleaner.strategy = @db_cleaner_strategy || :transaction
# DatabaseCleaner.start
# end
#
# config.after(:each) do
# DatabaseCleaner.clean
# end
# end
require 'thinking_sphinx/test'
class RSpec::ThinkingSphinx
@started = false
def self.setup(config)
ThinkingSphinx::Test.create_indexes_folder
# Need to start sphinx after spork has forked it in dev, so doing it in a before block
config.before(:each, :sphinx) do
@db_cleaner_strategy = :truncation
unless @started
ThinkingSphinx::Test.start
@started = true
Kernel.at_exit do
# start_with_autostop is very slow for some reason
ThinkingSphinx::Test.config.controller.stop
end
end
end
end
end
# Usage, in spec:
# Model.create ...
# index_sphinx
# query_sphinx ...
def index_sphinx
ThinkingSphinx::Test.index
t = Time.now
path = "#{Rails.root}/log/searchd.#{ENV['TEST_ENV_NUMBER'].to_i}.log"
loop do
if File.exists?(path)
break if File.readlines(path).last.include?("rotating finished")
end
sleep 0.025
if Time.now - t > 0.5
puts("WTF: indexing not finished in 0.5 seconds? Let's assume it worked anyway.") if File.exists?(path)
break
end
end
end
@joakimk
Copy link
Author

joakimk commented Apr 16, 2012

Would very much like a better way to talk to the indexer process other than reading the log file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment