Skip to content

Commit

Permalink
Bugfix FXIOS-7584 [v120] Fakespot Retry Shopping API after 500 errors…
Browse files Browse the repository at this point in the history
… doesn't work (#16910)

* FXIOS-7584 #16883 Fakespot Retry Shopping API after 500 errors doesn't work

* check for both relay and 500

* Update tests
  • Loading branch information
razvanlitianu authored Oct 23, 2023
1 parent 9636b63 commit 3c8384c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Client/Frontend/Fakespot/ShoppingProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Foundation
import MozillaAppServices

/// Represents a parsed product for a URL.
///
Expand Down Expand Up @@ -97,7 +98,11 @@ class ShoppingProduct: FeatureFlaggable {
// If 500 error occurs during the attempt, we use 'continue'
// to go back to the beginning of the loop and try again.
// This means we will retry the 'fetch(_ type:, url:, requestBody:)' operation.
if (error as NSError).code == 500 {
if case OhttpError.RelayFailed = error {
let backOff = retryTimeout * Int(pow(2, Double(failCount - 1)))
try? await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64(backOff))
continue
} else if (error as NSError).code == 500 {
let backOff = retryTimeout * Int(pow(2, Double(failCount - 1)))
try? await Task.sleep(nanoseconds: NSEC_PER_MSEC * UInt64(backOff))
continue
Expand Down
11 changes: 11 additions & 0 deletions Tests/ClientTests/ShoppingProductTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import XCTest
@testable import Client
import MozillaAppServices

final class ShoppingProductTests: XCTestCase {
var client: TestFakespotClient!
Expand Down Expand Up @@ -146,6 +147,16 @@ final class ShoppingProductTests: XCTestCase {
XCTAssertEqual(client.fetchProductAnalysisDataCallCount, expected)
}

func testFetchingProductAnalysisData_WithThrowingRelayError_RetriesClientAPI() async {
let url = URL(string: "https://www.amazon.com/Under-Armour-Charged-Assert-Running/dp/B087T8Q2C4")!
let client = ThrowingFakeSpotClient(error: OhttpError.RelayFailed(message: "Relay error"))
let sut = ShoppingProduct(url: url, client: client)
_ = try? await sut.fetchProductAnalysisData(maxRetries: 3)

let expected = 4 // 3 + the original API call
XCTAssertEqual(client.fetchProductAnalysisDataCallCount, expected)
}

func testFetchingProductAnalysisData_WithAnyThrowing_DoesNotRetryClientAPI() async {
let url = URL(string: "https://www.amazon.com/Under-Armour-Charged-Assert-Running/dp/B087T8Q2C4")!
let client = ThrowingFakeSpotClient(error: NSError(domain: "Any Error", code: 404, userInfo: nil))
Expand Down

0 comments on commit 3c8384c

Please sign in to comment.