Skip to content

Commit

Permalink
feat(auto-retry): removed .immediately case from AutomaticRetry.Recon…
Browse files Browse the repository at this point in the history
…nectionPolicy
  • Loading branch information
jguz-pubnub committed Dec 14, 2023
1 parent e950e4b commit 95aac0a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
10 changes: 2 additions & 8 deletions Sources/PubNub/Networking/Request/Operators/AutomaticRetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public struct AutomaticRetry: RequestOperator, Hashable {
public static var `default` = AutomaticRetry()
/// No retry will be performed
public static var none = AutomaticRetry(retryLimit: 1)
/// Retry immediately twice on lost network connection
/// Retry on lost network connection
public static var connectionLost = AutomaticRetry(
policy: .immediately,
policy: .defaultLinear,
retryableURLErrorCodes: [.networkConnectionLost]
)
/// Exponential backoff twice when no internet connection is detected
Expand All @@ -34,8 +34,6 @@ public struct AutomaticRetry: RequestOperator, Hashable {
/// Linear reconnect every 3 seconds
public static let defaultLinear: ReconnectionPolicy = .linear(delay: 3)

/// Attempt to reconnect immediately
case immediately
/// Reconnect with an exponential backoff
case exponential(base: UInt, scale: Double, maxDelay: UInt)
/// Attempt to reconnect every X seconds
Expand All @@ -47,8 +45,6 @@ public struct AutomaticRetry: RequestOperator, Hashable {
let randomDelay = Double.random(in: 0...1)

switch self {
case .immediately:
return 0.0 + randomDelay
case let .exponential(base, scale, maxDelay):
return exponentialBackoffDelay(for: base, scale: scale, maxDelay: maxDelay, current: retryAttempt) + randomDelay
case let .linear(delay):
Expand Down Expand Up @@ -157,8 +153,6 @@ public struct AutomaticRetry: RequestOperator, Hashable {
} else {
self.policy = policy
}
case .immediately:
self.policy = policy
}

self.retryLimit = retryLimit
Expand Down
12 changes: 6 additions & 6 deletions Tests/PubNubTests/Networking/Operators/AutomaticRetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ class AutomaticRetryTests: XCTestCase {
}

func testEquatable_Init_Other() {
let immediateasePolicy = AutomaticRetry.ReconnectionPolicy.immediately
let linearPolicy = AutomaticRetry.ReconnectionPolicy.linear(delay: 3.0)
let testPolicy = AutomaticRetry(retryLimit: 2,
policy: immediateasePolicy,
policy: linearPolicy,
retryableHTTPStatusCodes: [],
retryableURLErrorCodes: [])

XCTAssertEqual(testPolicy.policy, immediateasePolicy)
XCTAssertEqual(testPolicy.policy, linearPolicy)
}

// MARK: - retry(:session:for:dueTo:completion:)
Expand Down Expand Up @@ -137,7 +137,7 @@ class AutomaticRetryTests: XCTestCase {
let testStatusCode = 500

let testPolicy = AutomaticRetry(retryLimit: 2,
policy: .immediately,
policy: .linear(delay: 3.0),
retryableHTTPStatusCodes: [testStatusCode],
retryableURLErrorCodes: [])
let testResponse = HTTPURLResponse(url: url,
Expand All @@ -153,7 +153,7 @@ class AutomaticRetryTests: XCTestCase {
let testURLErrorCode = URLError.Code.timedOut
let testError = URLError(testURLErrorCode)
let testPolicy = AutomaticRetry(retryLimit: 2,
policy: .immediately,
policy: .linear(delay: 3.0),
retryableHTTPStatusCodes: [],
retryableURLErrorCodes: [testURLErrorCode])

Expand All @@ -164,7 +164,7 @@ class AutomaticRetryTests: XCTestCase {
func testShouldRetry_False() {
let testError = URLError(.timedOut)
let testPolicy = AutomaticRetry(retryLimit: 2,
policy: .immediately,
policy: .linear(delay: 3.0),
retryableHTTPStatusCodes: [],
retryableURLErrorCodes: [])

Expand Down

0 comments on commit 95aac0a

Please sign in to comment.