Permalink
Browse files

rip out excon for em-http-request

  • Loading branch information...
1 parent d0b3135 commit 30a0753dade876847701aae0e7d4f92809319728 @qrush qrush committed Apr 23, 2011
View
@@ -1,14 +1,15 @@
source "http://rubygems.org"
-gem "daemons", "1.1.0"
-gem "excon", "0.5.6"
-gem "json_pure", "1.4.6"
-gem "redis", "2.1.1"
+gem "em-http-request", "0.3.0"
+gem "daemons", "1.1.0"
+gem "json_pure", "1.4.6"
+gem "redis", "2.1.1"
group :development do
gem "bourne"
gem "cucumber"
gem "jeweler"
gem "rspec"
gem "timecop"
+ gem "webmock"
end
View
@@ -1,9 +1,11 @@
GEM
remote: http://rubygems.org/
specs:
+ addressable (2.2.4)
bourne (1.0)
mocha (= 0.9.8)
builder (2.1.2)
+ crack (0.1.8)
cucumber (0.9.4)
builder (~> 2.1.2)
diff-lcs (~> 1.1.2)
@@ -12,7 +14,12 @@ GEM
term-ansicolor (~> 1.0.5)
daemons (1.1.0)
diff-lcs (1.1.2)
- excon (0.5.6)
+ em-http-request (0.3.0)
+ addressable (>= 2.0.0)
+ escape_utils
+ eventmachine (>= 0.12.9)
+ escape_utils (0.2.3)
+ eventmachine (0.12.10)
gherkin (2.2.9)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
@@ -37,6 +44,9 @@ GEM
rspec-mocks (2.1.0)
term-ansicolor (1.0.5)
timecop (0.3.5)
+ webmock (1.6.1)
+ addressable (>= 2.2.2)
+ crack (>= 0.1.7)
PLATFORMS
ruby
@@ -45,9 +55,10 @@ DEPENDENCIES
bourne
cucumber
daemons (= 1.1.0)
- excon (= 0.5.6)
+ em-http-request (= 0.3.0)
jeweler
json_pure (= 1.4.6)
redis (= 2.1.1)
rspec
timecop
+ webmock
View
Binary file not shown.
View
Binary file not shown.
View
@@ -7,7 +7,7 @@
require 'stringio'
require 'thread'
-require 'excon'
+require 'em-http-request'
require 'daemons'
require 'json'
require 'redis'
View
@@ -4,17 +4,15 @@ class Client
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
- JSON::ParserError,
- Excon::Errors::SocketError]
+ JSON::ParserError]
- attr_accessor :redis, :logger, :config, :http, :monitor
+ attr_accessor :redis, :logger, :config, :monitor
def setup(config, logger = nil)
self.config = config
self.logger = logger
self.redis = connect
self.monitor = Monitor.new
- self.http = Excon.new(config.server_prefix)
log "Started Daikon v#{VERSION}"
end
@@ -39,22 +37,26 @@ def exception(error)
end
def request(method, path, options = {})
- options[:method] = method.to_s.upcase
- options[:path] = path
- options[:headers] ||= {}
- options[:headers]['Authorization'] = config.api_key
+ url = "#{config.server_prefix}#{path}"
+ options[:head] ||= {}
+ options[:head]['Authorization'] = config.api_key
- log "#{options[:method]} #{config.server_prefix}#{options[:path]}"
- http.reset
- http.request(options)
+ log "#{method} #{options[:url]}"
+
+ EventMachine.run_block do
+ http = EventMachine::HttpRequest.new(url).send(method, options)
+ http.callback do
+ log "=> #{http.response}"
+ end
+ end
end
def push(method, path, body)
json = body.to_json
request(method, path,
- :body => json,
- :headers => {"Content-Length" => json.size.to_s,
- "Content-Type" => "application/json"})
+ :body => json,
+ :head => {"Content-Length" => json.size.to_s,
+ "Content-Type" => "application/json"})
end
def rotate_monitor(start, stop)
@@ -1,9 +1,10 @@
module Daikon
class Configuration
BLANK_KEY = "1234567890"
+ URL = "https://radish.heroku.com"
FLAGS = ["-u", "-k", "-s"]
OPTIONS = ["redis_url", "api_key", "server_prefix"]
- DEFAULTS = ["redis://0.0.0.0:6379", BLANK_KEY, "https://radish.heroku.com"]
+ DEFAULTS = ["redis://0.0.0.0:6379", BLANK_KEY, URL]
attr_accessor *OPTIONS
View
@@ -39,34 +39,29 @@
end
describe Daikon::Client, "when server is down" do
- subject { Daikon::Client.new }
- let(:redis) { stub("redis instance", :info => {}) }
+ subject { Daikon::Client.new }
+ let(:redis) { stub("redis instance", :info => {}) }
before do
Redis.stubs(:connect => redis)
- http = stub("http", :reset => nil)
- http.stubs(:request).raises(Timeout::Error)
- subject.stubs(:http => http)
-
+ stub_request(:any, infos_url).to_timeout
subject.setup(Daikon::Configuration.new)
end
- it "does not commit suicide" do
+ it "does not kill the client" do
lambda {
subject.report_info
}.should_not raise_error
end
end
describe Daikon::Client, "when it returns bad json" do
- subject { Daikon::Client.new }
- let(:redis) { stub("redis instance", :info => {}) }
+ subject { Daikon::Client.new }
+ let(:redis) { stub("redis instance", :info => {}) }
before do
Redis.stubs(:connect => redis)
- http = stub("http", :request => Excon::Response.new(:body => "{'bad':'json}"), :reset => nil)
- subject.stubs(:http => http)
-
+ stub_request(:post, infos_url).to_return(:body => "{'bad':'json}")
subject.setup(Daikon::Configuration.new)
end
@@ -85,20 +80,15 @@
"Content-Type" => "application/json"
}
- http.should have_received(:reset)
- http.should have_received(:request).with(
- :method => "POST",
- :path => "/api/v1/summaries.json",
- :body => payload.to_json,
- :headers => headers)
+ WebMock.should have_requested(:post, summaries_url(server)).
+ with(:body => payload.to_json, :headers => headers)
end
end
describe Daikon::Client, "rotate monitor" do
- subject { Daikon::Client.new }
- let(:http) { stub("http", :request => Excon::Response.new, :reset => nil) }
- let(:now) { "2011-01-19T18:23:55-05:00" }
- let(:past) { "2011-01-19T18:23:54-05:00" }
+ subject { Daikon::Client.new }
+ let(:now) { "2011-01-19T18:23:55-05:00" }
+ let(:past) { "2011-01-19T18:23:54-05:00" }
let(:payload) do
data.merge("start" => past, "stop" => now)
end
@@ -110,9 +100,9 @@
end
before do
- Daikon::Monitor.stubs(:pop).yields(data)
Timecop.freeze DateTime.parse(now)
- subject.stubs(:http => http)
+ Daikon::Monitor.stubs(:pop).yields(data)
+ stub_request(:post, summaries_url(server)).to_return(:status => 200)
subject.setup(config)
subject.rotate_monitor(DateTime.parse(past), DateTime.parse(now))
end
@@ -146,23 +136,19 @@
"Content-Type" => "application/json"
}
- http.should have_received(:reset)
- http.should have_received(:request).with(
- :method => "POST",
- :path => "/api/v1/infos.json",
- :body => results.to_json,
- :headers => headers)
+ WebMock.should have_requested(:post, infos_url(server)).
+ with(:body => results.to_json, :headers => headers)
end
end
describe Daikon::Client, "report info" do
subject { Daikon::Client.new }
let(:results) { {"connected_clients"=>"1", "used_cpu_sys_childrens"=>"0.00"} }
let(:redis) { stub("redis instance", :info => results) }
- let(:http) { stub("http", :request => Excon::Response.new, :reset => nil) }
before do
- subject.stubs(:redis => redis, :http => http)
+ subject.stubs(:redis => redis)
+ stub_request(:post, infos_url(server)).to_return(:status => 200)
subject.setup(config)
subject.report_info
end
View
@@ -7,17 +7,22 @@
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
-require 'timecop'
require 'bourne'
+require 'timecop'
+require 'webmock/rspec'
require 'support/capture_helper'
require 'support/parse_helper'
+require 'support/url_helper'
+
+WebMock.disable_net_connect!
RSpec.configure do |config|
config.mock_with :mocha
config.include CaptureHelper
config.include ParseHelper
+ config.include UrlHelper
config.before do
Timecop.freeze(DateTime.now)
@@ -0,0 +1,9 @@
+module UrlHelper
+ def infos_url(host = Daikon::Configuration::URL)
+ "#{host}/api/v1/infos.json"
+ end
+
+ def summaries_url(host = Daikon::Configuration::URL)
+ "#{host}/api/v1/summaries.json"
+ end
+end

0 comments on commit 30a0753

Please sign in to comment.