From 878cbc7033cfe20169cf65ca038330af6292f9ef Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Fri, 8 Dec 2023 15:27:35 +0100 Subject: [PATCH] Implement `RetryPolicyStrategyTests` (#6) * Implement `RetryPolicyStrategyTests` * Refactoring `RetrySequenceTests` * Update `CHANGELOG.md` --- CHANGELOG.md | 2 + .../UnitTests/RetryPolicyStrategyTests.swift | 47 +++++++++++++++++++ .../UnitTests/RetrySequenceTests.swift | 12 +++-- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1778d..ae6ddf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift b/Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift new file mode 100644 index 0000000..9afc0f5 --- /dev/null +++ b/Tests/TyphoonTests/UnitTests/RetryPolicyStrategyTests.swift @@ -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) +} diff --git a/Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift b/Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift index 97e56fd..4c55077 100644 --- a/Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift +++ b/Tests/TyphoonTests/UnitTests/RetrySequenceTests.swift @@ -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 } @@ -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 } @@ -40,7 +40,7 @@ final class RetrySequenceTests: XCTestCase { retry: .retry, jitterFactor: .jitterFactor, maxInterval: .maxInterval, - duration: .nanoseconds(1) + duration: .nanosecond ) ) @@ -66,7 +66,7 @@ final class RetrySequenceTests: XCTestCase { retry: .retry, jitterFactor: .jitterFactor, maxInterval: nil, - duration: .nanoseconds(1) + duration: .nanosecond ) ) @@ -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) +}