Skip to content

Commit

Permalink
Allow a configurable port number for starting servers during tests, a…
Browse files Browse the repository at this point in the history
…nd by default use a port other than the default for non-test usage. Also remove some of the repetition in test_helper.rb.
  • Loading branch information
radsaq committed Aug 10, 2011
1 parent 1bdfbe0 commit 9ce8a7d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
28 changes: 14 additions & 14 deletions lib/goliath/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def self.included(mod)
end

# Launches an instance of a given API server. The server
# will launch on the default settings of localhost port 9000.
# will launch on the specified port.
#
# @param api [Class] The API class to launch
# @param port [Integer] The port to run the server on
# @param options [Hash] The options hash to provide to the server
# @return [Goliath::Server] The executed server
def server(api, port = 9000, options = {}, &blk)
def server(api, port, options = {}, &blk)
op = OptionParser.new

s = Goliath::Server.new
Expand All @@ -44,7 +44,7 @@ def server(api, port = 9000, options = {}, &blk)
s.app = Goliath::Rack::Builder.build(api, s.api)
s.api.options_parser(op, options)
s.options = options
s.port = port
s.port = @test_server_port = port
s.start(&blk)
s
end
Expand All @@ -64,7 +64,7 @@ def stop
# @param blk [Proc] The code to execute after the server is launched.
# @note This will not return until stop is called.
def with_api(api, options = {}, &blk)
server(api, 9000, options, &blk)
server(api, options.delete(:port) || 9900, options, &blk)
end

# Helper method to setup common callbacks for various request methods.
Expand All @@ -90,8 +90,7 @@ def hookup_request_callbacks(req, errback, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def head_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").head(request_data)
req = test_request(request_data).head(request_data)
hookup_request_callbacks(req, errback, &blk)
end

Expand All @@ -101,8 +100,7 @@ def head_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def get_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").get(request_data)
req = test_request(request_data).get(request_data)
hookup_request_callbacks(req, errback, &blk)
end

Expand All @@ -112,8 +110,7 @@ def get_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def post_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").post(request_data)
req = test_request(request_data).post(request_data)
hookup_request_callbacks(req, errback, &blk)
end

Expand All @@ -123,8 +120,7 @@ def post_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def put_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").put(request_data)
req = test_request(request_data).put(request_data)
hookup_request_callbacks(req, errback, &blk)
end

Expand All @@ -134,9 +130,13 @@ def put_request(request_data = {}, errback = nil, &blk)
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def delete_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").delete(request_data)
req = test_request(request_data).delete(request_data)
hookup_request_callbacks(req, errback, &blk)
end

def test_request(request_data)
path = request_data.delete(:path) || ''
EM::HttpRequest.new("http://localhost:#{@test_server_port}#{path}")
end
end
end
4 changes: 2 additions & 2 deletions spec/integration/keepalive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

describe 'HTTP Keep-Alive support' do
it 'serves multiple requests via single connection' do
with_api(Echo) do
conn = EM::HttpRequest.new('http://localhost:9000')
with_api(Echo, :port => 9901) do
conn = EM::HttpRequest.new('http://localhost:9901')
r1 = conn.get(:query => {:echo => 'test'}, :keepalive => true)

r1.errback { fail }
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/pipelining_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def response(env)

describe 'HTTP Pipelining support' do
it 'serves multiple requests via single connection' do
with_api(Interleaving) do
with_api(Interleaving, :port => 9901) do
start = Time.now.to_f
res = []

conn = EM::HttpRequest.new('http://localhost:9000')
conn = EM::HttpRequest.new('http://localhost:9901')
r1 = conn.aget :query => {:delay => 0.3}, :keepalive => true
r2 = conn.aget :query => {:delay => 0.2}

Expand Down

0 comments on commit 9ce8a7d

Please sign in to comment.