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
13 changes: 12 additions & 1 deletion Sources/Networking/RemoteConfigBlobDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ final class URLSessionRemoteConfigBlobDownloader: RemoteConfigBlobDownloaderType
case unexpectedStatusCode(Int)
}

static let timeout: TimeInterval = 5

private let session: URLSession

init(session: URLSession = .shared) {
init(session: URLSession = URLSession(
configuration: URLSessionRemoteConfigBlobDownloader.defaultSessionConfiguration()
)) {
self.session = session
}

static func defaultSessionConfiguration() -> URLSessionConfiguration {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = Self.timeout
configuration.timeoutIntervalForResource = Self.timeout
return configuration
}

func data(from url: URL) async throws -> Data {
return try await withCheckedThrowingContinuation { continuation in
let task = self.session.dataTask(with: url) { data, response, error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Foundation
import Nimble
import OHHTTPStubs
import OHHTTPStubsSwift
@testable import RevenueCat
import XCTest

Expand All @@ -31,6 +33,7 @@ final class RemoteConfigBlobFetcherTests: TestCase {
}

override func tearDownWithError() throws {
HTTPStubs.removeAllStubs()
self.downloader.cancelAll()
self.fetcher = nil
self.sourceProvider = nil
Expand Down Expand Up @@ -217,6 +220,39 @@ final class RemoteConfigBlobFetcherTests: TestCase {
expect(self.blobStore.invokedWriteParameters?.data) == payload
}

func testRealDownloaderUsesFiveSecondTimeoutAndRetriesNextSourceAfterTimeout() async {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually verify the timeout, but that is rather tricky here (we could assert that the user config uses a 5s timeout here, but I'm not sure if that adds much value here? Happy to add it though).

But I realized we don't have a timeout test case at all, so felt like adding this one

let payload = "timeout fallback payload".asData
let ref = Self.ref(for: payload)
let primaryURL = Self.templateURL.replacingOccurrences(of: Self.placeholder, with: ref)
let backupURL = Self.backupTemplateURL.replacingOccurrences(of: Self.placeholder, with: ref)

stub(condition: isAbsoluteURLString(primaryURL)) { _ in
return HTTPStubsResponse(error: URLError(.timedOut))
}
stub(condition: isAbsoluteURLString(backupURL)) { _ in
return HTTPStubsResponse(
data: payload,
statusCode: Int32(HTTPStatusCode.success.rawValue),
headers: nil
)
}

self.sourceProvider = Self.sourceProvider(urls: [
Self.templateURL,
Self.backupTemplateURL
])
self.fetcher = RemoteConfigBlobFetcher(
blobStore: self.blobStore,
sourceProvider: self.sourceProvider,
downloader: URLSessionRemoteConfigBlobDownloader()
)

let result = await self.fetcher.ensureDownloaded(ref: ref)

expect(result) == true
expect(self.blobStore.invokedWriteParameters?.data) == payload
}

func testNotFoundDoesNotMarkSourceUnhealthyOrRetryNextSource() async {
let missingRef = Self.ref(for: "missing blob".asData)

Expand Down