summaryrefslogtreecommitdiff
path: root/test/net/error_handling_test.rb
blob: 2f963d610f8899177453d27a971836cb7f01209e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# frozen_string_literal: true

require 'test_helper'

class ErrorHandlingTest < Minitest::Test
  def setup
    @client = Net::Hippie::Client.new
    @uri = URI.parse('https://example.com/test')
  end

  def test_handles_eof_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(EOFError)
    
    assert_raises EOFError do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_connection_refused
    WebMock.stub_request(:get, @uri.to_s).to_raise(Errno::ECONNREFUSED)
    
    assert_raises Errno::ECONNREFUSED do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_connection_reset
    WebMock.stub_request(:get, @uri.to_s).to_raise(Errno::ECONNRESET)
    
    assert_raises Errno::ECONNRESET do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_host_unreachable
    WebMock.stub_request(:get, @uri.to_s).to_raise(Errno::EHOSTUNREACH)
    
    assert_raises Errno::EHOSTUNREACH do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_invalid_argument
    WebMock.stub_request(:get, @uri.to_s).to_raise(Errno::EINVAL)
    
    assert_raises Errno::EINVAL do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_net_open_timeout
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::OpenTimeout)
    
    assert_raises Net::OpenTimeout do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_net_protocol_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::ProtocolError)
    
    assert_raises Net::ProtocolError do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_net_read_timeout
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::ReadTimeout)
    
    assert_raises Net::ReadTimeout do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_openssl_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(OpenSSL::OpenSSLError)
    
    assert_raises OpenSSL::OpenSSLError do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_ssl_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(OpenSSL::SSL::SSLError)
    
    assert_raises OpenSSL::SSL::SSLError do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_socket_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(SocketError)
    
    assert_raises SocketError do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_handles_timeout_error
    WebMock.stub_request(:get, @uri.to_s).to_raise(Timeout::Error)
    
    assert_raises Timeout::Error do
      @client.with_retry(retries: 0) { |client| client.get(@uri) }
    end
  end

  def test_retry_with_exponential_backoff
    call_count = 0
    WebMock.stub_request(:get, @uri.to_s).to_return do
      call_count += 1
      if call_count < 3
        raise Net::ReadTimeout
      else
        { status: 200, body: 'success' }
      end
    end

    start_time = Time.now
    response = @client.with_retry(retries: 3) { |client| client.get(@uri) }
    end_time = Time.now

    assert_equal Net::HTTPOK, response.class
    assert_equal 'success', response.body
    assert_equal 3, call_count
    # Should have some delay due to exponential backoff
    assert_operator end_time - start_time, :>, 0.3
  end

  def test_retry_eventually_fails_after_max_retries
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::ReadTimeout)
    
    start_time = Time.now
    
    assert_raises Net::ReadTimeout do
      @client.with_retry(retries: 2) { |client| client.get(@uri) }
    end
    
    end_time = Time.now
    # Should have attempted 3 times (initial + 2 retries) with delays
    assert_operator end_time - start_time, :>, 0.3
  end

  def test_retry_with_nil_retries
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::ReadTimeout)
    
    assert_raises Net::ReadTimeout do
      @client.with_retry(retries: nil) { |client| client.get(@uri) }
    end
  end

  def test_retry_with_negative_retries
    WebMock.stub_request(:get, @uri.to_s).to_raise(Net::ReadTimeout)
    
    assert_raises Net::ReadTimeout do
      @client.with_retry(retries: -1) { |client| client.get(@uri) }
    end
  end

  def test_connection_errors_constant_includes_all_expected_errors
    expected_errors = [
      EOFError,
      Errno::ECONNREFUSED,
      Errno::ECONNRESET,
      Errno::ECONNRESET,  # Listed twice in original
      Errno::EHOSTUNREACH,
      Errno::EINVAL,
      Net::OpenTimeout,
      Net::ProtocolError,
      Net::ReadTimeout,
      OpenSSL::OpenSSLError,
      OpenSSL::SSL::SSLError,
      SocketError,
      Timeout::Error
    ]
    
    expected_errors.each do |error_class|
      assert_includes Net::Hippie::CONNECTION_ERRORS, error_class
    end
  end
end