Skip to content

Commit

Permalink
A series of specs for curl-multi
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Ball committed Aug 20, 2009
1 parent a736a18 commit a4fdbbd
Showing 1 changed file with 109 additions and 6 deletions.
115 changes: 109 additions & 6 deletions spec/curl-multi_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# curl-multi - Ruby bindings for the libcurl multi interface
# Copyright (C) 2007 Philotic, Inc.
# curl-multi - Ruby bindings for the libcurl multi interface # Copyright (C) 2007 Philotic, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -16,7 +15,7 @@

require File.dirname(__FILE__) + '/spec_helper.rb'

describe 'Curl::Multi' do
describe 'Curl::Multi with a single request' do
before(:all) do
puts "setting up server\n"
@port = 9192
Expand All @@ -41,14 +40,118 @@
end

it "Should be able to get one ok" do
c = Curl::Multi.new()
success = lambda do |body|
body.should eql('ok')
end
failure = lambda do |ex|
fail ex
end
c.get("#{@server}/ok", success, failure)
c.select([], []) while c.size > 0
@curl_multi.get("#{@server}/ok", success, failure)
@curl_multi.select([], []) while @curl_multi.size > 0
end

it "Should be able to handle errors" do
success = lambda do |body|
fail(body)
end
failure = lambda do |ex|
ex.should be_a Curl::HTTPError
ex.status.should eql 500
end
@curl_multi.get("#{@server}/error", success, failure)
@curl_multi.select([], []) while @curl_multi.size > 0
end

it "Should be able to handle redirects" do
success = lambda do |body|
fail(body)
end
failure = lambda do |ex|
ex.should be_a Curl::HTTPError
ex.status.should eql 302
end
@curl_multi.get("#{@server}/redirect/ok", success, failure)
@curl_multi.select([], []) while @curl_multi.size > 0
end
end
describe 'Curl::Multi with many connections' do
before(:all) do
puts "setting up multiple servers for many connection test\n"
@ports = (9100...9120)
@servers = []
@server_processes = []
@ports.each do |port|
process = Process.fork do
$stdout.reopen('/dev/null', 'w')
$stderr.reopen('/dev/null', 'w')
puts "trying to open #{port}"
CurlTestServer.run! :host => 'localhost', :port => port
end
@server_processes.push process
@servers.push "http://localhost:#{port}"
sleep(0.1)
end
#sleep(1) #give servers time to boot
end

after(:all) do
puts "\nTearing down servers"
@server_processes.each do |pid|
Process.kill('KILL', pid)
end
end

before(:each) do
@curl_multi = Curl::Multi.new
end

it "should be able to get 600 oks" do
num_oks = 0
success = lambda do |body|
body.should eql('ok')
num_oks += 1
end
failure = lambda do |ex|
fail ex
end
#start with a sleep to get everybody queued
@servers.each do |server|
@curl_multi.get("#{server}/sleep/1", lambda {}, failure)
end
30.times do
@servers.each do |server|
@curl_multi.get("#{server}/ok", success, failure)
end
end
@curl_multi.select([], []) while @curl_multi.size > 0
num_oks.should eql 600
@curl_multi.size.should eql(0)
end
it "should be able to get 300 oks and 300 fails" do
num_oks = 0
num_errors = 0
success = lambda do |body|
body.should eql('ok')
num_oks += 1
end
failure = lambda do |ex|
ex.should be_a Curl::HTTPError
ex.status.should eql 500
num_errors += 1
end
#start with a sleep to get everybody queued
@servers.each do |server|
@curl_multi.get("#{server}/sleep/1", lambda {}, lambda {})
end
15.times do
@servers.each do |server|
@curl_multi.get("#{server}/ok", success, failure)
@curl_multi.get("#{server}/error", success, failure)
end
end
@curl_multi.select([], []) while @curl_multi.size > 0
num_oks.should eql 300
num_errors.should eql 300
@curl_multi.size.should eql(0)
end
end

0 comments on commit a4fdbbd

Please sign in to comment.