From 4f4d7247208b467c3d48587d10761c23a41b140b Mon Sep 17 00:00:00 2001 From: naruse Date: Tue, 26 Sep 2017 14:00:25 +0000 Subject: Make retries for Net::HTTP configurable [Feature #10674] by stereobooster fix https://github.com/ruby/ruby/pull/1654 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60035 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/net/http/test_http.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test/net/http') diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index d22b3dea3d..43f590839f 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1045,6 +1045,56 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase } end + class MockSocket + attr_reader :count + def initialize(success_after: nil) + @success_after = success_after + @count = 0 + end + def close + end + def closed? + end + def write(_) + end + def readline + @count += 1 + if @success_after && @success_after <= @count + "HTTP/1.1 200 OK" + else + raise Errno::ECONNRESET + end + end + def readuntil(*_) + "" + end + def read_all(_) + end + end + + def test_http_retry_success + start {|http| + socket = MockSocket.new(success_after: 10) + http.instance_variable_set(:@socket, socket) + assert_equal 0, socket.count + http.max_retries = 10 + res = http.get('/') + assert_equal 10, socket.count + assert_kind_of Net::HTTPResponse, res + assert_kind_of String, res.body + } + end + + def test_http_retry_failed + start {|http| + socket = MockSocket.new + http.instance_variable_set(:@socket, socket) + http.max_retries = 10 + assert_raise(Errno::ECONNRESET){ http.get('/') } + assert_equal 11, socket.count + } + end + def test_keep_alive_server_close def @server.run(sock) sock.close -- cgit v1.2.3