Skip to content

Commit

Permalink
Support RSpec 3
Browse files Browse the repository at this point in the history
  • Loading branch information
junaruga authored and ioquatix committed Aug 4, 2020
1 parent c0e1266 commit bfb2969
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ end

group :test do
gem "rake", '< 11.0'
gem "rspec", "~> 1.2.9"
gem "rspec", "~> 3.5"
end
12 changes: 6 additions & 6 deletions spec/backends/swiftiply_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Backends::SwiftiplyClient do
before do
@backend = Backends::SwiftiplyClient.new('0.0.0.0', 3333)
@backend.server = mock('server').as_null_object
@backend.server = double('server').as_null_object
end

it "should connect" do
Expand All @@ -26,7 +26,7 @@
before do
@connection = SwiftiplyConnection.new(nil)
@connection.backend = Backends::SwiftiplyClient.new('0.0.0.0', 3333)
@connection.backend.server = mock('server').as_null_object
@connection.backend.server = double('server').as_null_object
end

it do
Expand All @@ -39,8 +39,8 @@
end

it "should reconnect on unbind" do
@connection.backend.stub!(:running?).and_return(true)
@connection.stub!(:rand).and_return(0) # Make sure we don't wait
allow(@connection.backend).to receive(:running?) { true }
allow(@connection).to receive(:rand) { 0 } # Make sure we don't wait

@connection.should_receive(:reconnect).with('0.0.0.0', 3333)

Expand All @@ -51,7 +51,7 @@
end

it "should not reconnect when not running" do
@connection.backend.stub!(:running?).and_return(false)
allow(@connection.backend).to receive(:running?) { false }
EventMachine.should_not_receive(:add_timer)
@connection.unbind
end
Expand All @@ -63,4 +63,4 @@
it "should generate swiftiply_handshake based on key" do
@connection.send(:swiftiply_handshake, 'key').should == 'swiftclient000000000d0503key'
end
end
end
4 changes: 2 additions & 2 deletions spec/backends/unix_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

it "should remove socket file on close" do
@backend.close
File.exist?('/tmp/thin-test.sock').should be_false
File.exist?('/tmp/thin-test.sock').should be_falsey
end
end

Expand All @@ -34,4 +34,4 @@
it "should return 127.0.0.1 as remote_address" do
@connection.remote_address.should == '127.0.0.1'
end
end
end
30 changes: 16 additions & 14 deletions spec/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

describe Connection do
before do
EventMachine.stub(:send_data)
@connection = Connection.new(mock('EM').as_null_object)
allow(EventMachine).to receive(:send_data)
@connection = Connection.new(double('EM').as_null_object)
@connection.post_init
@connection.backend = mock("backend", :ssl? => false)
@connection.backend = double("backend", :ssl? => false)
@connection.app = proc do |env|
[200, {}, ['body']]
end
Expand All @@ -17,14 +17,14 @@
end

it "should make a valid response on bad request" do
@connection.request.stub!(:parse).and_raise(InvalidRequest)
allow(@connection.request).to receive(:parse).and_raise(InvalidRequest)
@connection.should_receive(:post_process).with(Response::BAD_REQUEST)
@connection.receive_data('')
end

it "should close connection on InvalidRequest error in receive_data" do
@connection.request.stub!(:parse).and_raise(InvalidRequest)
@connection.response.stub!(:persistent?).and_return(false)
allow(@connection.request).to receive(:parse).and_raise(InvalidRequest)
allow(@connection.response).to receive(:persistent?) { false }
@connection.can_persist!
@connection.should_receive(:terminate_request)
@connection.receive_data('')
Expand All @@ -42,7 +42,7 @@

it "should rescue error in process" do
@connection.app.should_receive(:call).and_raise(StandardError)
@connection.response.stub!(:persistent?).and_return(false)
allow(@connection.response).to receive(:persistent?) { false }
@connection.should_receive(:terminate_request)
@connection.process
end
Expand All @@ -55,7 +55,7 @@

it "should not close persistent connection on error" do
@connection.app.should_receive(:call).and_raise(StandardError)
@connection.response.stub!(:persistent?).and_return(true)
allow(@connection.response).to receive(:persistent?) { true }
@connection.can_persist!
@connection.should_receive(:teminate_request).never
@connection.process
Expand All @@ -68,27 +68,29 @@

it "should not return HTTP_X_FORWARDED_FOR as remote_address" do
@connection.request.env['HTTP_X_FORWARDED_FOR'] = '1.2.3.4'
@connection.stub!(:socket_address).and_return("127.0.0.1")
allow(@connection).to receive(:socket_address) { "127.0.0.1" }
@connection.remote_address.should == "127.0.0.1"
end

it "should return nil on error retreiving remote_address" do
@connection.stub!(:get_peername).and_raise(RuntimeError)
allow(@connection).to receive(:get_peername).and_raise(RuntimeError)
@connection.remote_address.should be_nil
end

it "should return nil on nil get_peername" do
@connection.stub!(:get_peername).and_return(nil)
allow(@connection).to receive(:get_peername) { nil }
@connection.remote_address.should be_nil
end

it "should return nil on empty get_peername" do
@connection.stub!(:get_peername).and_return('')
allow(@connection).to receive(:get_peername) { '' }
@connection.remote_address.should be_nil
end

it "should return remote_address" do
@connection.stub!(:get_peername).and_return(Socket.pack_sockaddr_in(3000, '127.0.0.1'))
allow(@connection).to receive(:get_peername) do
Socket.pack_sockaddr_in(3000, '127.0.0.1')
end
@connection.remote_address.should == '127.0.0.1'
end

Expand All @@ -97,7 +99,7 @@
end

it "should be persistent when response is and allowed" do
@connection.response.stub!(:persistent?).and_return(true)
allow(@connection.response).to receive(:persistent?) { true }
@connection.can_persist!
@connection.should be_persistent
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

Server.should_receive(:new).with('0.0.0.0', 3000, @controller.options).and_return(@server)
@server.should_receive(:config)
Rack::Adapter::Rails.stub!(:new).and_return(@adapter)
allow(Rack::Adapter::Rails).to receive(:new) { @adapter }
end

it "should configure server" do
Expand Down Expand Up @@ -87,7 +87,7 @@
@controller.options[:threaded] = true
@controller.start

@server.threaded.should be_true
@server.threaded.should be_truthy
end

it "should set RACK_ENV" do
Expand All @@ -103,7 +103,7 @@
describe Controller do
before do
@controller = Controller.new(:pid => 'thin.pid', :timeout => 10)
@controller.stub!(:wait_for_file)
allow(@controller).to receive(:wait_for_file)
end

it "should stop" do
Expand All @@ -126,4 +126,4 @@

File.delete('test.yml')
end
end
end
8 changes: 4 additions & 4 deletions spec/controllers/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

before do
Thin.stub!(:linux?).and_return(true)
allow(Thin).to receive(:linux?) { true }
FileUtils.mkdir_p 'tmp/sandbox'

@service = Service.new(:all => 'spec/configs')
Expand All @@ -27,7 +27,7 @@
it "should create /etc/init.d/thin file when calling install" do
@service.install

File.exist?(Service::INITD_PATH).should be_true
File.exist?(Service::INITD_PATH).should be_truthy
script_name = File.directory?('/etc/rc.d') ?
'/etc/rc.d/thin' : '/etc/init.d/thin'
File.read(Service::INITD_PATH).should include('CONFIG_PATH=tmp/sandbox/etc/thin',
Expand All @@ -38,7 +38,7 @@
it "should create /etc/thin dir when calling install" do
@service.install

File.directory?(Service::DEFAULT_CONFIG_PATH).should be_true
File.directory?(Service::DEFAULT_CONFIG_PATH).should be_truthy
end

it "should include specified path in /etc/init.d/thin script" do
Expand All @@ -50,4 +50,4 @@
after do
FileUtils.rm_rf 'tmp/sandbox'
end
end
end
16 changes: 8 additions & 8 deletions spec/daemonizing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def name

sleep 1
Process.wait(@pid)
File.exist?(@server.pid_file).should be_true
File.exist?(@server.pid_file).should be_truthy
@pid = @server.pid

proc { sleep 0.1 while File.exist?(@server.pid_file) }.should take_less_then(20)
Expand Down Expand Up @@ -77,7 +77,7 @@ def name
end

it 'should kill process in pid file' do
File.exist?(@server.pid_file).should be_false
File.exist?(@server.pid_file).should be_falsey

@pid = fork do
@server.daemonize
Expand All @@ -94,7 +94,7 @@ def name

Process.wait(@pid)

File.exist?(@server.pid_file).should be_false
File.exist?(@server.pid_file).should be_falsey
end

it 'should force kill process in pid file' do
Expand All @@ -113,7 +113,7 @@ def name

Process.wait(@pid)

File.exist?(@server.pid_file).should be_false
File.exist?(@server.pid_file).should be_falsey
end

it 'should send kill signal if timeout' do
Expand All @@ -131,8 +131,8 @@ def name

Process.wait(@pid)

File.exist?(@server.pid_file).should be_false
Process.running?(@pid).should be_false
File.exist?(@server.pid_file).should be_falsey
Process.running?(@pid).should be_falsey
end

it "should restart" do
Expand Down Expand Up @@ -174,7 +174,7 @@ def name

proc { @server.daemonize }.should raise_error(PidFileExist)

File.exist?(@server.pid_file).should be_true
File.exist?(@server.pid_file).should be_truthy
end

it "should raise if no pid file" do
Expand All @@ -189,7 +189,7 @@ def name

@server.send(:remove_stale_pid_file)

File.exist?(@server.pid_file).should be_false
File.exist?(@server.pid_file).should be_falsey
end

after do
Expand Down
30 changes: 19 additions & 11 deletions spec/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestLogging
describe "when setting a custom logger" do

it "should not accept a logger object that is not sane" do
expect { Logging.logger = "" }.to raise_error
expect { Logging.logger = "" }.to raise_error(ArgumentError)
end

it "should accept a legit custom logger object" do
Expand Down Expand Up @@ -54,7 +54,7 @@ class TestLogging

str = nil
expect { str = @readpipe.read_nonblock(512) }.to_not raise_error
str.should_not be_nil
expect(str).not_to be_nil
end

#
Expand All @@ -63,7 +63,9 @@ class TestLogging
Logging.debug = false
@object.log_debug("hiya")

expect { @readpipe.read_nonblock(512) }.to raise_error
expect do
@readpipe.read_nonblock(512)
end.to raise_error(IO::EAGAINWaitReadable)
end

#
Expand All @@ -78,19 +80,25 @@ class TestLogging
it "should not log messages if silenced via module method" do
Logging.silent = true
@object.log_info("hola")
expect { @readpipe.read_nonblock(512) }.to raise_error()
expect do
@readpipe.read_nonblock(512)
end.to raise_error(IO::EAGAINWaitReadable)
end

it "should not log anything if silenced via module methods" do
Logging.silent = true
Logging.log_msg("hi")
expect { @readpipe.read_nonblock(512) }.to raise_error()
expect do
@readpipe.read_nonblock(512)
end.to raise_error(IO::EAGAINWaitReadable)
end

it "should not log anything if silenced via instance methods" do
@object.silent = true
@object.log_info("hello")
expect { @readpipe.read_nonblock(512) }.to raise_error()
expect do
@readpipe.read_nonblock(512)
end.to raise_error(IO::EAGAINWaitReadable)
end

end # Logging tests (with custom logger)
Expand All @@ -103,16 +111,16 @@ class TestLogging
@object.log_debug("Hey")
end

out.include?("Hey").should be_true
out.include?("DEBUG").should be_true
out.include?("Hey").should be_truthy
out.include?("DEBUG").should be_truthy
end

it "should be usable (at the module level) for logging" do
out = with_redirected_stdout do
Logging.log_msg("Hey")
end

out.include?("Hey").should be_true
out.include?("Hey").should be_truthy
end

end
Expand Down Expand Up @@ -147,7 +155,7 @@ class TestLogging
@object.trace("Hey")
end

out.include?("Hey").should be_true
out.include?("Hey").should be_truthy
end

it "should be usable (at the module level) for logging" do
Expand All @@ -156,7 +164,7 @@ class TestLogging
Logging.trace_msg("hey")
end

out.include?("hey").should be_true
out.include?("hey").should be_truthy
end

end # tracer tests (no custom logger)
Expand Down
Loading

0 comments on commit bfb2969

Please sign in to comment.