module SunspotTest

Attributes

server[W]
solr_startup_timeout[W]

Public Class Methods

server() click to toggle source
# File lib/sunspot_test.rb, line 20
def server
  @server ||= Sunspot::Rails::Server.new
end
setup_solr() click to toggle source
# File lib/sunspot_test.rb, line 15
def setup_solr
  unstub
  start_sunspot_server
end
solr_startup_timeout() click to toggle source
# File lib/sunspot_test.rb, line 11
def solr_startup_timeout
  @solr_startup_timeout || 15
end
start_sunspot_server() click to toggle source
# File lib/sunspot_test.rb, line 24
def start_sunspot_server
  unless solr_running?
    pid = fork do
      STDERR.reopen("/dev/null")
      STDOUT.reopen("/dev/null")
      server.run
    end

    at_exit { Process.kill("TERM", pid) }

    wait_until_solr_starts
  end
end
stub() click to toggle source

Stubs Sunspot calls to Solr server

# File lib/sunspot_test.rb, line 39
def stub
  unless @session_stubbed
    Sunspot.session = Sunspot::Rails::StubSessionProxy.new(original_sunspot_session)
    @session_stubbed = true
  end
end
unstub() click to toggle source

Resets Sunspot to call Solr server, opposite of stub

# File lib/sunspot_test.rb, line 47
def unstub
  if @session_stubbed
    Sunspot.session = original_sunspot_session
    @session_stubbed = false
  end
end

Private Class Methods

original_sunspot_session() click to toggle source
# File lib/sunspot_test.rb, line 56
def original_sunspot_session
  @original_sunspot_session ||= Sunspot.session
end
solr_running?() click to toggle source
# File lib/sunspot_test.rb, line 68
def solr_running?
  begin
    solr_ping_uri = URI.parse("#{Sunspot.session.config.solr.url}/ping")
    res = Net::HTTP.get_response(solr_ping_uri)
    # Solr will return 503 codes when it's starting up
    res.code != '503'
  rescue
    false # Solr Not Running
  end
end
wait_until_solr_starts() click to toggle source
# File lib/sunspot_test.rb, line 60
def wait_until_solr_starts
  (solr_startup_timeout * 10).times do
    break if solr_running?
    sleep(0.1)
  end
  raise TimeOutError, "Solr failed to start after #{solr_startup_timeout} seconds" unless solr_running?
end