Class: Middleware::RetryTest

Inherits:
Faraday::TestCase show all
Defined in:
test/middleware/retry_test.rb

Instance Method Summary collapse

Methods inherited from Faraday::TestCase

#capture_warnings, jruby?, rbx?, ssl_mode?, #test_default

Methods included from Faraday::LiveServerConfig

#live_server, #live_server=, #live_server?

Instance Method Details

#conn(*retry_args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'test/middleware/retry_test.rb', line 9

def conn(*retry_args)
  Faraday.new do |b|
    b.request :retry, *retry_args
    b.adapter :test do |stub|
      ['get', 'post'].each do |method|
        stub.send(method, '/unstable') {
          @times_called += 1
          @explode.call @times_called
        }
      end
    end
  end
end

#setupObject



5
6
7
# File 'test/middleware/retry_test.rb', line 5

def setup
  @times_called = 0
end

#test_calls_sleep_amountObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'test/middleware/retry_test.rb', line 68

def test_calls_sleep_amount
  explode_app = MiniTest::Mock.new
  explode_app.expect(:call, nil, [{:body=>nil}])
  def explode_app.call(env)
    raise Errno::ETIMEDOUT
  end

  retry_middleware = Faraday::Request::Retry.new(explode_app)
  class << retry_middleware
    attr_accessor :sleep_amount_retries

    def sleep_amount(retries)
      self.sleep_amount_retries.delete(retries)
      0
    end
  end
  retry_middleware.sleep_amount_retries = [2, 1]

  assert_raises(Errno::ETIMEDOUT) {
    retry_middleware.call({:method => :get})
  }

  assert_empty retry_middleware.sleep_amount_retries
end

#test_custom_exceptionsObject



115
116
117
118
119
120
121
# File 'test/middleware/retry_test.rb', line 115

def test_custom_exceptions
  @explode = lambda {|n| raise "boom!" }
  assert_raises(RuntimeError) {
    conn(:exceptions => StandardError).get("/unstable")
  }
  assert_equal 3, @times_called
end

#test_exponential_backoffObject



93
94
95
96
97
98
# File 'test/middleware/retry_test.rb', line 93

def test_exponential_backoff
  middleware = Faraday::Request::Retry.new(nil, :max => 5, :interval => 0.1, :backoff_factor => 2)
  assert_equal middleware.sleep_amount(5), 0.1
  assert_equal middleware.sleep_amount(4), 0.2
  assert_equal middleware.sleep_amount(3), 0.4
end

#test_handled_errorObject



29
30
31
32
33
# File 'test/middleware/retry_test.rb', line 29

def test_handled_error
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) { conn.get("/unstable") }
  assert_equal 3, @times_called
end

#test_intervalObject



59
60
61
62
63
64
65
66
# File 'test/middleware/retry_test.rb', line 59

def test_interval
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  started  = Time.now
  assert_raises(Errno::ETIMEDOUT) {
    conn(:max => 2, :interval => 0.1).get("/unstable")
  }
  assert_in_delta 0.2, Time.now - started, 0.04
end

#test_legacy_max_negative_retriesObject



41
42
43
44
45
# File 'test/middleware/retry_test.rb', line 41

def test_legacy_max_negative_retries
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) { conn(-9).get("/unstable") }
  assert_equal 1, @times_called
end

#test_legacy_max_retriesObject



35
36
37
38
39
# File 'test/middleware/retry_test.rb', line 35

def test_legacy_max_retries
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) { conn(1).get("/unstable") }
  assert_equal 2, @times_called
end

#test_new_max_negative_retriesObject



53
54
55
56
57
# File 'test/middleware/retry_test.rb', line 53

def test_new_max_negative_retries
  @explode = lambda { |n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) { conn(:max => -9).get("/unstable") }
  assert_equal 1, @times_called
end

#test_new_max_retriesObject



47
48
49
50
51
# File 'test/middleware/retry_test.rb', line 47

def test_new_max_retries
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) { conn(:max => 3).get("/unstable") }
  assert_equal 4, @times_called
end

#test_random_additional_interval_amountObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'test/middleware/retry_test.rb', line 100

def test_random_additional_interval_amount
  middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 1.0)
  sleep_amount = middleware.sleep_amount(2)
  assert_operator sleep_amount, :>=, 0.1
  assert_operator sleep_amount, :<=, 0.2
  middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.5)
  sleep_amount = middleware.sleep_amount(2)
  assert_operator sleep_amount, :>=, 0.1
  assert_operator sleep_amount, :<=, 0.15
  middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.25)
  sleep_amount = middleware.sleep_amount(2)
  assert_operator sleep_amount, :>=, 0.1
  assert_operator sleep_amount, :<=, 0.125
end

#test_should_call_retry_if_for_empty_method_listObject



167
168
169
170
171
172
173
174
# File 'test/middleware/retry_test.rb', line 167

def test_should_call_retry_if_for_empty_method_list
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  check = lambda { |env,exception| @times_called < 2 }
  assert_raises(Errno::ETIMEDOUT) {
    conn(:retry_if => check, :methods => []).get("/unstable")
  }
  assert_equal 2, @times_called
end

#test_should_not_call_retry_if_for_idempotent_methods_if_methods_unspecifiedObject



141
142
143
144
145
146
147
148
# File 'test/middleware/retry_test.rb', line 141

def test_should_not_call_retry_if_for_idempotent_methods_if_methods_unspecified
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  check = lambda { |env,exception| raise "this should have never been called" }
  assert_raises(Errno::ETIMEDOUT) {
    conn(:retry_if => check).get("/unstable")
  }
  assert_equal 3, @times_called
end

#test_should_not_call_retry_if_for_specified_methodsObject



158
159
160
161
162
163
164
165
# File 'test/middleware/retry_test.rb', line 158

def test_should_not_call_retry_if_for_specified_methods
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  check = lambda { |env,exception| raise "this should have never been called" }
  assert_raises(Errno::ETIMEDOUT) {
    conn(:retry_if => check, :methods => [:post]).post("/unstable")
  }
  assert_equal 3, @times_called
end

#test_should_not_retry_for_non_idempotent_method_if_methods_unspecifiedObject



150
151
152
153
154
155
156
# File 'test/middleware/retry_test.rb', line 150

def test_should_not_retry_for_non_idempotent_method_if_methods_unspecified
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  assert_raises(Errno::ETIMEDOUT) {
    conn.post("/unstable")
  }
  assert_equal 1, @times_called
end

#test_should_stop_retrying_if_block_returns_false_checking_envObject



123
124
125
126
127
128
129
130
# File 'test/middleware/retry_test.rb', line 123

def test_should_stop_retrying_if_block_returns_false_checking_env
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  check = lambda { |env,exception| env[:method] != :post }
  assert_raises(Errno::ETIMEDOUT) {
    conn(:retry_if => check).post("/unstable")
  }
  assert_equal 1, @times_called
end

#test_should_stop_retrying_if_block_returns_false_checking_exceptionObject



132
133
134
135
136
137
138
139
# File 'test/middleware/retry_test.rb', line 132

def test_should_stop_retrying_if_block_returns_false_checking_exception
  @explode = lambda {|n| raise Errno::ETIMEDOUT }
  check = lambda { |env,exception| !exception.kind_of?(Errno::ETIMEDOUT) }
  assert_raises(Errno::ETIMEDOUT) {
    conn(:retry_if => check).post("/unstable")
  }
  assert_equal 1, @times_called
end

#test_unhandled_errorObject



23
24
25
26
27
# File 'test/middleware/retry_test.rb', line 23

def test_unhandled_error
  @explode = lambda {|n| raise "boom!" }
  assert_raises(RuntimeError) { conn.get("/unstable") }
  assert_equal 1, @times_called
end