Skip to content
Browse files

Remove a parameter that was always the same

This also fixes tests for the request benchmarking that can fail depending on
whether or not the HTTP client, test framework, or Ruby version themselves
call Time.now, since those callers can mess up the number of calls to Time.now
I was stubbing.
  • Loading branch information...
1 parent 649484a commit 9a9d08b4333fc830b3de00942662e165c973a1af @chrisk committed Nov 10, 2011
View
8 lib/samuel/diary.rb
@@ -2,19 +2,23 @@ module Samuel
module Diary
extend self
- def record_request(http, request, time_requested)
+ def record_request(http, request, time_requested = current_time)
@requests ||= []
@requests.push({:request => request, :time_requested => time_requested})
end
- def record_response(http, request, response, time_responded)
+ def record_response(http, request, response, time_responded = current_time)
time_requested = @requests.detect { |r| r[:request] == request }[:time_requested]
@requests.reject! { |r| r[:request] == request }
log_request_and_response(http, request, response, time_requested, time_responded)
end
private
+ def current_time
+ Time.now
+ end
+
def log_request_and_response(http, request, response, time_started, time_ended)
log_entry_class = case http.class.to_s
when "Net::HTTP" then LogEntries::NetHttp
View
8 lib/samuel/driver_patches/http_client.rb
@@ -19,7 +19,7 @@ def do_get_block_with_samuel(req, proxy, conn, &block)
begin
do_get_block_without_samuel(req, proxy, conn, &block)
rescue Exception => e
- Samuel::Diary.record_response(self, req, e, Time.now)
+ Samuel::Diary.record_response(self, req, e)
raise
end
end
@@ -28,7 +28,7 @@ def do_get_stream_with_samuel(req, proxy, conn)
begin
do_get_stream_without_samuel(req, proxy, conn)
rescue Exception => e
- Samuel::Diary.record_response(self, req, e, Time.now)
+ Samuel::Diary.record_response(self, req, e)
raise
end
end
@@ -39,11 +39,11 @@ def initialize(http_client_instance)
end
def filter_request(request)
- Samuel::Diary.record_request(@http_client_instance, request, Time.now)
+ Samuel::Diary.record_request(@http_client_instance, request)
end
def filter_response(request, response)
- Samuel::Diary.record_response(@http_client_instance, request, response, Time.now)
+ Samuel::Diary.record_response(@http_client_instance, request, response)
nil # this returns command symbols like :retry, etc.
end
end
View
9 lib/samuel/driver_patches/net_http.rb
@@ -11,16 +11,15 @@ def self.included(klass)
end
def request_with_samuel(request, body = nil, &block)
- Samuel::Diary.record_request(self, request, Time.now)
+ Samuel::Diary.record_request(self, request)
response, exception_raised = nil, false
begin
response = request_without_samuel(request, body, &block)
rescue Exception => response
exception_raised = true
end
-
- Samuel::Diary.record_response(self, request, response, Time.now)
+ Samuel::Diary.record_response(self, request, response)
raise response if exception_raised
response
@@ -32,8 +31,8 @@ def connect_with_samuel
fake_request = Object.new
def fake_request.path; ""; end
def fake_request.method; "CONNECT"; end
- Samuel::Diary.record_request(self, fake_request, Time.now)
- Samuel::Diary.record_response(self, fake_request, response, Time.now)
+ Samuel::Diary.record_request(self, fake_request)
+ Samuel::Diary.record_response(self, fake_request, response)
raise
end
end
View
2 test/http_client_test.rb
@@ -10,7 +10,7 @@ class HttpClientTest < Test::Unit::TestCase
context "to GET http://localhost:8000/, responding with a 200 in 53ms" do
setup do
now = Time.now
- Time.stubs(:now).returns(now, now + 0.053)
+ Samuel::Diary.stubs(:current_time).returns(now, now + 0.053)
HTTPClient.get("http://localhost:8000/")
end
View
2 test/net_http_test.rb
@@ -12,7 +12,7 @@ class RequestTest < Test::Unit::TestCase
setup do
FakeWeb.register_uri(:get, "http://example.com/test", :status => [200, "OK"])
now = Time.now
- Time.stubs(:now).returns(now, now + 0.053)
+ Samuel::Diary.stubs(:current_time).returns(now, now + 0.053)
open "http://example.com/test"
end

0 comments on commit 9a9d08b

Please sign in to comment.
Something went wrong with that request. Please try again.