@@ -227,6 +227,118 @@ final class RetrySequenceTests: XCTestCase {
227227 XCTAssertEqual ( result [ 7 ] , 128 , accuracy: 13 )
228228 }
229229
230+ func test_thatChainDelayStrategy_returnsDelaysFromFirstStrategy( ) {
231+ // given
232+ let sut = ChainDelayStrategy ( entries: [
233+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . seconds( 1 ) ) ) ,
234+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . seconds( 5 ) ) ) ,
235+ ] )
236+
237+ // then
238+ XCTAssertNotNil ( sut. delay ( forRetry: 0 ) )
239+ XCTAssertNotNil ( sut. delay ( forRetry: 1 ) )
240+ XCTAssertNotNil ( sut. delay ( forRetry: 2 ) )
241+ }
242+
243+ func test_thatChainDelayStrategy_switchesToSecondStrategy_afterFirstExhausted( ) {
244+ // given
245+ let firstDelay : UInt64 = 1_000_000_000
246+ let secondDelay : UInt64 = 5_000_000_000
247+
248+ let sut = ChainDelayStrategy ( entries: [
249+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . seconds( 1 ) ) ) ,
250+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . seconds( 5 ) ) ) ,
251+ ] )
252+
253+ // then
254+ XCTAssertEqual ( sut. delay ( forRetry: 0 ) , firstDelay)
255+ XCTAssertEqual ( sut. delay ( forRetry: 1 ) , firstDelay)
256+ XCTAssertEqual ( sut. delay ( forRetry: 2 ) , firstDelay)
257+ XCTAssertEqual ( sut. delay ( forRetry: 3 ) , secondDelay)
258+ XCTAssertEqual ( sut. delay ( forRetry: 4 ) , secondDelay)
259+ }
260+
261+ func test_thatChainDelayStrategy_returnsNil_whenAllStrategiesExhausted( ) {
262+ // given
263+ let sut = ChainDelayStrategy ( entries: [
264+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . seconds( 1 ) ) ) ,
265+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . seconds( 5 ) ) ) ,
266+ ] )
267+
268+ // then
269+ XCTAssertNil ( sut. delay ( forRetry: 5 ) )
270+ }
271+
272+ func test_thatRetrySequenceCreatesASequence_whenStrategyIsChainWithDifferentDelays( ) {
273+ // given
274+ let sequence = RetrySequence (
275+ strategy: . chain( [
276+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . nanoseconds( 1 ) ) ) ,
277+ . init( retries: 3 , strategy: ExponentialDelayStrategy (
278+ duration: . nanoseconds( 1 ) ,
279+ multiplier: 2.0 ,
280+ jitterFactor: 0.0 ,
281+ maxInterval: nil
282+ ) ) ,
283+ ] )
284+ )
285+
286+ // when
287+ let result : [ UInt64 ] = sequence. map { $0 }
288+
289+ // then
290+ XCTAssertEqual ( result, [ 1 , 1 , 1 , 1 , 2 , 4 ] )
291+ }
292+
293+ func test_thatChainStrategy_automaticallyCalculatesTotalRetries( ) {
294+ // given
295+ let entries : [ ChainDelayStrategy . Entry ] = [
296+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
297+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
298+ ]
299+
300+ // when
301+ let strategy = RetryPolicyStrategy . chain ( entries)
302+
303+ // then
304+ XCTAssertEqual ( strategy. retries, 5 )
305+ }
306+
307+ func test_thatChainStrategy_returnsCustomStrategy( ) {
308+ // given
309+ let entries : [ ChainDelayStrategy . Entry ] = [
310+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
311+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
312+ ]
313+
314+ // when
315+ let strategy = RetryPolicyStrategy . chain ( entries)
316+
317+ // then
318+ if case . custom = strategy {
319+ XCTAssertTrue ( true )
320+ } else {
321+ XCTFail ( " Expected .custom strategy " )
322+ }
323+ }
324+
325+ func test_thatRetrySequenceCreatesASequence_whenStrategyIsChain( ) {
326+ // given
327+ let sequence = RetrySequence (
328+ strategy: . chain( [
329+ . init( retries: 3 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
330+ . init( retries: 2 , strategy: ConstantDelayStrategy ( duration: . nanosecond) ) ,
331+ ] )
332+ )
333+
334+ // when
335+ let result : [ UInt64 ] = sequence. map { $0 }
336+
337+ // then
338+ XCTAssertEqual ( result. count, 5 )
339+ XCTAssertEqual ( result, [ 1 , 1 , 1 , 1 , 1 ] )
340+ }
341+
230342 // MARK: Helpers
231343
232344 private func toSeconds( _ nanos: UInt64 ) -> Double {
0 commit comments