Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

## Added
- Increase the test coverage of the project
- Added in Pull Request [#6](https://github.com/space-code/typhoon/pull/6).
- Implement error handling in RetryPolicyService
- Added in Pull Request [#5](https://github.com/space-code/typhoon/pull/5).
- Implement exponential backoff with jitter
Expand Down
47 changes: 47 additions & 0 deletions Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Typhoon
// Copyright © 2023 Space Code. All rights reserved.
//

import Typhoon
import XCTest

// MARK: - RetryPolicyStrategyTests

final class RetryPolicyStrategyTests: XCTestCase {
// MARK: Tests

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsConstant() {
// when
let duration = RetryPolicyStrategy.constant(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsExponential() {
// when
let duration = RetryPolicyStrategy.exponential(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}

func test_thatRetryPolicyStrategyReturnsDuration_whenTypeIsExponentialWithJitter() {
// when
let duration = RetryPolicyStrategy.exponentialWithJitter(retry: .retry, duration: .second).duration

// then
XCTAssertEqual(duration, .second)
}
}

// MARK: Constants

private extension Int {
static let retry = 5
}

private extension DispatchTimeInterval {
static let second = DispatchTimeInterval.seconds(1)
}
12 changes: 8 additions & 4 deletions Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class RetrySequenceTests: XCTestCase {

func test_thatRetrySequenceCreatesASequence_whenStrategyIsConstant() {
// given
let sequence = RetrySequence(strategy: .constant(retry: .retry, duration: .nanoseconds(1)))
let sequence = RetrySequence(strategy: .constant(retry: .retry, duration: .nanosecond))

// when
let result: [UInt64] = sequence.map { $0 }
Expand All @@ -24,7 +24,7 @@ final class RetrySequenceTests: XCTestCase {

func test_thatRetrySequenceCreatesASequence_whenStrategyIsExponential() {
// given
let sequence = RetrySequence(strategy: .exponential(retry: .retry, duration: .nanoseconds(1)))
let sequence = RetrySequence(strategy: .exponential(retry: .retry, duration: .nanosecond))

// when
let result: [UInt64] = sequence.map { $0 }
Expand All @@ -40,7 +40,7 @@ final class RetrySequenceTests: XCTestCase {
retry: .retry,
jitterFactor: .jitterFactor,
maxInterval: .maxInterval,
duration: .nanoseconds(1)
duration: .nanosecond
)
)

Expand All @@ -66,7 +66,7 @@ final class RetrySequenceTests: XCTestCase {
retry: .retry,
jitterFactor: .jitterFactor,
maxInterval: nil,
duration: .nanoseconds(1)
duration: .nanosecond
)
)

Expand Down Expand Up @@ -100,3 +100,7 @@ private extension Double {
static let multiplier = 2.0
static let jitterFactor = 0.1
}

private extension DispatchTimeInterval {
static let nanosecond = DispatchTimeInterval.nanoseconds(1)
}