From 3c8384c92a0694bb897b7f5ae89c7d857aff5efa Mon Sep 17 00:00:00 2001 From: Litianu Razvan Date: Mon, 23 Oct 2023 16:54:04 +0300 Subject: [PATCH] Bugfix FXIOS-7584 [v120] Fakespot Retry Shopping API after 500 errors 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 --- Client/Frontend/Fakespot/ShoppingProduct.swift | 7 ++++++- Tests/ClientTests/ShoppingProductTests.swift | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Client/Frontend/Fakespot/ShoppingProduct.swift b/Client/Frontend/Fakespot/ShoppingProduct.swift index 9222c7011774..c61146396b0a 100644 --- a/Client/Frontend/Fakespot/ShoppingProduct.swift +++ b/Client/Frontend/Fakespot/ShoppingProduct.swift @@ -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. /// @@ -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 diff --git a/Tests/ClientTests/ShoppingProductTests.swift b/Tests/ClientTests/ShoppingProductTests.swift index a89ee7fd66b7..585a346ef4f6 100644 --- a/Tests/ClientTests/ShoppingProductTests.swift +++ b/Tests/ClientTests/ShoppingProductTests.swift @@ -4,6 +4,7 @@ import XCTest @testable import Client +import MozillaAppServices final class ShoppingProductTests: XCTestCase { var client: TestFakespotClient! @@ -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))