@@ -116,4 +116,76 @@ def runtime
116116 end
117117 end
118118 end
119+
120+ context "with instrumentation on retry" do
121+ let ( :instrumentation ) { double ( "Instrumentation" ) }
122+ let ( :middleware ) do
123+ ZendeskAPI ::Middleware ::Request ::Retry . new ( client . connection . builder . app )
124+ end
125+
126+ before do
127+ allow ( instrumentation ) . to receive ( :instrument )
128+ client . config . instrumentation = instrumentation
129+ # Inject instrumentation into middleware instance
130+ allow_any_instance_of ( ZendeskAPI ::Middleware ::Request ::Retry ) . to receive ( :instrumentation ) . and_return ( instrumentation )
131+ stub_request ( :get , %r{instrumented} ) . to_return ( status : 429 , headers : { retry_after : 1 } ) . to_return ( status : 200 )
132+ end
133+
134+ it "calls instrumentation on retry" do
135+ expect ( instrumentation ) . to receive ( :instrument ) . with (
136+ "zendesk.retry" ,
137+ hash_including ( attempt : 1 , endpoint : anything , method : anything , reason : anything , delay : anything )
138+ ) . at_least ( :once )
139+ client . connection . get ( "instrumented" )
140+ end
141+
142+ it "calls instrumentation on second retry attempt" do
143+ # Override stub for this test to force two rate limit responses before success
144+ stub_request ( :get , %r{instrumented_twice} )
145+ . to_return ( status : 429 , headers : { retry_after : 0 } )
146+ . to_return ( status : 429 , headers : { retry_after : 0 } )
147+ . to_return ( status : 200 )
148+
149+ # Expect instrumentation for attempt 1 and attempt 2
150+ expect ( instrumentation ) . to receive ( :instrument ) . with (
151+ "zendesk.retry" ,
152+ hash_including ( attempt : 1 , endpoint : anything , method : anything , reason : "rate_limited" , delay : anything )
153+ ) . at_least ( :once )
154+ expect ( instrumentation ) . to receive ( :instrument ) . with (
155+ "zendesk.retry" ,
156+ hash_including ( attempt : 2 , endpoint : anything , method : anything , reason : "rate_limited" , delay : anything )
157+ ) . at_least ( :once )
158+
159+ client . connection . get ( "instrumented_twice" )
160+ end
161+
162+ it "calls instrumentation with first attempt server error, and rate limited on second retry attempt" do
163+ # Override stub for this test to force two rate limit responses before success
164+ stub_request ( :get , %r{instrumented_twice} )
165+ . to_return ( status : 503 , headers : { retry_after : 0 } )
166+ . to_return ( status : 429 , headers : { retry_after : 0 } )
167+ . to_return ( status : 200 )
168+
169+ # Expect instrumentation for attempt 1 and attempt 2
170+ expect ( instrumentation ) . to receive ( :instrument ) . with (
171+ "zendesk.retry" ,
172+ hash_including ( attempt : 1 , endpoint : anything , method : anything , reason : "server_error" , delay : anything )
173+ ) . at_least ( :once )
174+ expect ( instrumentation ) . to receive ( :instrument ) . with (
175+ "zendesk.retry" ,
176+ hash_including ( attempt : 2 , endpoint : anything , method : anything , reason : "rate_limited" , delay : anything )
177+ ) . at_least ( :once )
178+
179+ client . connection . get ( "instrumented_twice" )
180+ end
181+
182+ it "does not call instrumentation when no retry occurs" do
183+ stub_request ( :get , %r{no_retry} ) . to_return ( status : 200 )
184+ expect ( instrumentation ) . not_to receive ( :instrument ) . with (
185+ "zendesk.retry" ,
186+ hash_including ( :attempt , :endpoint , :method , :reason , :delay )
187+ )
188+ client . connection . get ( "no_retry" )
189+ end
190+ end
119191end
0 commit comments