Skip to content

Commit

Permalink
Improve specs stability.
Browse files Browse the repository at this point in the history
Use benchmark_unit for perf specs.
Kill daemon processes in daemonizing specs.
Add error message before running spec if missing required gem.
  • Loading branch information
macournoyer committed Jan 20, 2008
1 parent b698fe8 commit d50a45e
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
== 0.5.5 Pony release
* Improve specs stability.
* Move request body to a Tempfile if too big (> 112 MB)
* Remove useless check for max header size in Request (already done in the parser)

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require 'lib/thin'

Dir['tasks/**/*.rake'].each { |rake| load rake }

task :default => [:compile, :spec]
task :default => :spec
2 changes: 1 addition & 1 deletion lib/thin/daemonizing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.included(base)
end

def pid
File.exist?(pid_file) ? open(pid_file).read : nil
File.exist?(pid_file) ? open(pid_file).read.to_i : nil
end

# Turns the current script into a daemon process that detaches from the console.
Expand Down
1 change: 1 addition & 0 deletions spec/daemonizing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@

after do
Process.kill(9, @pid.to_i) if @pid && Process.running?(@pid.to_i)
Process.kill(9, @server.pid) if @server.pid && Process.running?(@server.pid)
end
end
6 changes: 3 additions & 3 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@
proc { R("GET / HTTP/1.1\r\n#{big_headers}\r\n") }.should raise_error(InvalidRequest)
end

it "should be faster then #{max_parsing_time = 0.2} ms" do
body = <<-EOS.chomp
it "should be faster then #{max_parsing_time = 0.0002} RubySeconds" do
body = <<-EOS.chomp.gsub("\n", "\r\n")
POST /postit HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9
Expand All @@ -226,7 +226,7 @@
hi=there&name=marc&[email protected]
EOS

proc { R(body, true) }.should be_faster_then(max_parsing_time)
proc { R(body) }.should be_faster_then(max_parsing_time)
end

it 'should be comparable to Mongrel parser' do
Expand Down
2 changes: 1 addition & 1 deletion spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
out.should include("\r\n\r\n<html></html>")
end

it "should be faster then #{max_parsing_time = 0.07} ms" do
it "should be faster then #{max_parsing_time = 0.00011} ms" do
@response.body << <<-EOS
<html><head><title>Dir listing</title></head>
<body><h1>Listing stuff</h1><ul>
Expand Down
6 changes: 3 additions & 3 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
post('/', :big => big).size.should == big.size + 4
end

it "should handle GET in less then #{get_request_time = 5} ms" do
it "should handle GET in less then #{get_request_time = 0.004} RubySecond" do
proc { get('/') }.should be_faster_then(get_request_time)
end

it "should handle POST in less then #{post_request_time = 6} ms" do
proc { post('/', :file => 'X' * 1000) }.should be_faster_then(get_request_time)
it "should handle POST in less then #{post_request_time = 0.007} RubySecond" do
proc { post('/', :file => 'X' * 1000) }.should be_faster_then(post_request_time)
end

after do
Expand Down
30 changes: 25 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'benchmark'
require 'timeout'
require 'fileutils'
require 'benchmark_unit'

include Thin

Expand Down Expand Up @@ -31,14 +32,33 @@ def initialize(max_time)
@max_time = max_time
end

def matches?(target)
@target = target
@time = Benchmark.measure { @target.call }.real * 1000
@time <= @max_time
# Base on benchmark_unit/assertions#compare_benchmarks
def matches?(proc)
@time, multiplier = 0, 1

while (@time < 0.01) do
@time = Benchmark::Unit.measure do
multiplier.times &proc
end
multiplier *= 10
end

multiplier /= 10

iterations = (Benchmark::Unit::CLOCK_TARGET / @time).to_i * multiplier
iterations = 1 if iterations < 1

total = Benchmark::Unit.measure do
iterations.times &proc
end

@time = total / iterations

@time < @max_time
end

def failure_message(less_more=:less)
"took #{@time} ms, should take #{less_more} then #{@max_time} ms"
"took <#{@time.inspect} RubySeconds>, should take #{less_more} than #{@max_time} RubySeconds."
end

def negative_failure_message
Expand Down
2 changes: 1 addition & 1 deletion tasks/gem.rake
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
end

s.files = %w(COPYING CHANGELOG README Rakefile) +
Dir.glob("{benchmark,bin,doc,example,lib,spec}/**/*") +
Dir.glob("{benchmark,bin,doc,example,lib,spec,tasks}/**/*") +
Dir.glob("ext/**/*.{h,c,rb,rl}")

if WIN
Expand Down
11 changes: 11 additions & 0 deletions tasks/spec.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ else
Spec::Rake::SpecTask.new('spec') do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
end

task :check_spec_gems do
begin
require 'spec'
require 'benchmark_unit'
rescue LoadError
abort "To run specs, install rspec and benchmark_unit gems"
end
end

task :spec => [:check_spec_gems, :compile]
end

0 comments on commit d50a45e

Please sign in to comment.