Skip to content

Commit

Permalink
refactored the tests to not use the event machine delay server which …
Browse files Browse the repository at this point in the history
…was behaving oddly
  • Loading branch information
pauldix committed Oct 16, 2009
1 parent 093449d commit 47c1865
Show file tree
Hide file tree
Showing 12 changed files with 725 additions and 927 deletions.
3 changes: 2 additions & 1 deletion lib/typhoeus/hydra.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Typhoeus
class Hydra
def initialize
def initialize(initial_pool_size = 10)
@multi = Multi.new
@easy_pool = []
initial_pool_size.times { @easy_pool << Easy.new }
@stubs = []
@memoized_requests = {}
@retrieved_from_cache = {}
Expand Down
23 changes: 19 additions & 4 deletions spec/servers/app.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
require 'rubygems'
require 'sinatra'
require 'json'

@@fail_count = 0
get '/fail/:number' do
if @@fail_count >= params[:number].to_i
"ok"
else
@@fail_count += 1
error 500, "oh noes!"
end
end

get '/fail_forever' do
error 500, "oh noes!"
end

get '/**' do
sleep params["delay"].to_i if params.has_key?("delay")
params.inspect
request.env.merge!(:body => request.body.read).to_json
end

put '/**' do
puts request.inspect
puts "**#{request.body.read}**"
request.env.merge!(:body => request.body.read).to_json
end

post '/**' do
puts request.inspect
puts "**#{request.body.read}**"
request.env.merge!(:body => request.body.read).to_json
end

delete '/**' do
puts request.inspect
puts "**#{request.body.read}**"
request.env.merge!(:body => request.body.read).to_json
end
66 changes: 0 additions & 66 deletions spec/servers/delay_fixture_server.rb

This file was deleted.

47 changes: 0 additions & 47 deletions spec/servers/delay_server.rb

This file was deleted.

51 changes: 0 additions & 51 deletions spec/servers/method_server.rb

This file was deleted.

20 changes: 1 addition & 19 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "rubygems"
require 'json'
require "spec"

# gem install redgreen for colored test output
Expand All @@ -8,22 +9,3 @@
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)

require "lib/typhoeus"

# local servers for running tests
require File.dirname(__FILE__) + "/servers/method_server.rb"

def start_method_server(port, sleep = 0)
MethodServer.sleep_time = sleep
pid = Process.fork do
EventMachine::run {
EventMachine.epoll
EventMachine::start_server("0.0.0.0", port, MethodServer)
}
end
sleep 0.2
pid
end

def stop_method_server(pid)
Process.kill("HUP", pid)
end
33 changes: 6 additions & 27 deletions spec/typhoeus/easy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Typhoeus::Easy do
before(:all) do
@pid = start_method_server(3002)
end

after(:all) do
stop_method_server(@pid)
end

describe Typhoeus::Easy do
describe "options" do
it "should allow for following redirects"
it "should allow you to set the user agent"
it "should provide a timeout in milliseconds" do
pid = start_method_server(3001, 5)
e = Typhoeus::Easy.new
e.url = "http://localhost:3001"
e.method = :get
e.timeout = 50
e.perform
puts e.response_code
puts e.total_time_taken
# this doesn't work on a mac for some reason
# e.timed_out?.should == true
stop_method_server(pid)
end
end

Expand All @@ -33,17 +22,7 @@
easy.method = :get
easy.perform
easy.response_code.should == 200
easy.response_body.should include("REQUEST_METHOD=GET")
end

it "should send a request body" do
easy = Typhoeus::Easy.new
easy.url = "http://localhost:3002"
easy.method = :get
easy.request_body = "this is a body!"
easy.perform
easy.response_code.should == 200
easy.response_body.should include("this is a body!")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"
end
end

Expand Down Expand Up @@ -80,7 +59,7 @@
easy.method = :put
easy.perform
easy.response_code.should == 200
easy.response_body.should include("REQUEST_METHOD=PUT")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "PUT"
end

it "should send a request body" do
Expand All @@ -101,7 +80,7 @@
easy.method = :post
easy.perform
easy.response_code.should == 200
easy.response_body.should include("REQUEST_METHOD=POST")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "POST"
end

it "should send a request body" do
Expand Down Expand Up @@ -132,7 +111,7 @@
easy.method = :delete
easy.perform
easy.response_code.should == 200
easy.response_body.should include("REQUEST_METHOD=DELETE")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "DELETE"
end

it "should send a request body" do
Expand Down
6 changes: 3 additions & 3 deletions spec/typhoeus/hydra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ def set(key, object, timeout = 0)
first.handled_response.body.should include("foo")
first.response.should == second.response
first.handled_response.should == second.handled_response
(Time.now - start_time).should < 1.1 # if it had run twice it would be ~ 2 seconds
(Time.now - start_time).should < 1.2 # if it had run twice it would be ~ 2 seconds
end

it "pulls GETs from cache" do
start_time = Time.now
hydra = Typhoeus::Hydra.new
start_time = Time.now
hydra.cache_getter do |request|
@cache.get(request.cache_key) rescue nil
end
Expand Down Expand Up @@ -207,6 +207,6 @@ def set(key, object, timeout = 0)

hydra.run
@responses.size.should == 3
(Time.now - start_time).should < 3.1
(Time.now - start_time).should < 3.3
end
end
16 changes: 4 additions & 12 deletions spec/typhoeus/multi_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Typhoeus::Easy do
before(:all) do
@pid = start_method_server(3002)
end

after(:all) do
stop_method_server(@pid)
end

it "should save easy handles that get added" do
multi = Typhoeus::Multi.new
easy = Typhoeus::Easy.new
Expand All @@ -30,7 +22,7 @@
multi.add(easy)
multi.perform
easy.response_code.should == 200
easy.response_body.should include("METHOD=GET")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"

e2 = Typhoeus::Easy.new
e2.url = "http://localhost:3002"
Expand All @@ -39,7 +31,7 @@
multi.perform

e2.response_code.should == 200
e2.response_body.should include("METHOD=POST")
JSON.parse(e2.response_body)["REQUEST_METHOD"].should == "POST"
end

it "should perform easy handles added after the first one runs" do
Expand All @@ -58,9 +50,9 @@

multi.perform
easy.response_code.should == 200
easy.response_body.should include("METHOD=GET")
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"
e2.response_code.should == 200
e2.response_body.should include("METHOD=POST")
JSON.parse(e2.response_body)["REQUEST_METHOD"].should == "POST"
end

# it "should do multiple gets" do
Expand Down
Loading

0 comments on commit 47c1865

Please sign in to comment.