Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/sunspot_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'sunspot/rails'
require 'sunspot/version'
require 'net/http'

module SunspotTest
Expand All @@ -23,13 +24,19 @@ def server

def start_sunspot_server
unless solr_running?
pid = fork do
STDERR.reopen("/dev/null")
STDOUT.reopen("/dev/null")
server.run
if Sunspot::VERSION > '2.2.7'
server.start
else
suppress_output($stderr) do
server.start
end
end

at_exit { Process.kill("TERM", pid) }
at_exit do
suppress_output($stdout) do
server.stop
end
end

wait_until_solr_starts
end
Expand All @@ -53,6 +60,14 @@ def unstub

private

def suppress_output(output)
original_output = output.clone
output.reopen('/dev/null')
yield
ensure
output.reopen(original_output)
end

def original_sunspot_session
@original_sunspot_session ||= Sunspot.session
end
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

require 'sunspot'
require 'sunspot_test'

RSpec.configure do |c|
c.raise_errors_for_deprecations!
end
28 changes: 18 additions & 10 deletions spec/sunspot_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@
end

# was getting funky issues when the test was broken up
it "forks the process, redirects stdout/stderr, runs server, sets exit hook, waits for server" do

expect(SunspotTest).to receive(:fork) do |&block|
expect(STDERR).to receive(:reopen).with("/dev/null")
expect(STDOUT).to receive(:reopen).with("/dev/null")
expect(SunspotTest.server).to receive(:run)
block.call
it 'forks the process, redirects stdout/stderr, runs server, sets exit hook, waits for server' do
expect(SunspotTest).to receive(:suppress_output).with($stderr) do |&block|
expect(SunspotTest.server).to receive(:start)
block.call($stderr)
end

expect(SunspotTest).to receive(:at_exit) do |&block|
expect(Process).to receive(:kill)
expect(SunspotTest).to receive(:suppress_output).with($stdout) do |&block|
expect(SunspotTest.server).to receive(:stop)
block.call($stdout)
end
block.call
end

Expand Down Expand Up @@ -136,7 +136,7 @@
describe ".solr_running" do
context "if solr is running" do
before do
Net::HTTP.stub(get_response: double(code: '200'))
allow(Net::HTTP).to receive_messages(get_response: double(code: '200'))
end

it "returns true" do
Expand All @@ -152,7 +152,7 @@

context "if solr is starting up" do
before do
Net::HTTP.stub(get_response:double(code: '503'))
allow(Net::HTTP).to receive_messages(get_response: double(code: '503'))
end

it "returns false" do
Expand All @@ -162,6 +162,14 @@

end

describe ".suppress_output" do
it "restors default" do
original_class = $stdout.class
SunspotTest.send(:suppress_output, $stdout) { puts('this should not show up') }
expect($stdout.class).to eq(original_class)
end
end

describe ".wait_until_solr_starts" do
context "if solr never starts" do
before do
Expand Down