diff --git a/Sources/FeaturevisorSDK/Instance+Refresh.swift b/Sources/FeaturevisorSDK/Instance+Refresh.swift index 29c1954..935f199 100644 --- a/Sources/FeaturevisorSDK/Instance+Refresh.swift +++ b/Sources/FeaturevisorSDK/Instance+Refresh.swift @@ -19,38 +19,35 @@ extension FeaturevisorInstance { statuses.refreshInProgress = true - Task { [weak self] in - try? await self? - .fetchDatafileContent( - from: datafileUrl, - handleDatafileFetch: handleDatafileFetch - ) { [weak self] result in - guard let self else { - return - } + try? fetchDatafileContent( + from: datafileUrl, + handleDatafileFetch: handleDatafileFetch + ) { [weak self] result in + guard let self else { + return + } - switch result { - case .success(let datafileContent): - let currentRevision = self.getRevision() - let newRevision = datafileContent.revision - let isNotSameRevision = currentRevision != newRevision + switch result { + case .success(let datafileContent): + let currentRevision = self.getRevision() + let newRevision = datafileContent.revision + let isNotSameRevision = currentRevision != newRevision - self.datafileReader = DatafileReader(datafileContent: datafileContent) - logger.info("refreshed datafile") + self.datafileReader = DatafileReader(datafileContent: datafileContent) + logger.info("refreshed datafile") - self.emitter.emit(.refresh) + self.emitter.emit(.refresh) - if isNotSameRevision { - self.emitter.emit(.update) - } + if isNotSameRevision { + self.emitter.emit(.update) + } - self.statuses.refreshInProgress = false + self.statuses.refreshInProgress = false - case .failure(let error): - self.logger.error("failed to refresh datafile", ["error": error]) - self.statuses.refreshInProgress = false - } - } + case .failure(let error): + self.logger.error("failed to refresh datafile", ["error": error]) + self.statuses.refreshInProgress = false + } } } diff --git a/Tests/FeaturevisorSDKTests/InstanceTests.swift b/Tests/FeaturevisorSDKTests/InstanceTests.swift index d28c1ca..c6bd8ec 100644 --- a/Tests/FeaturevisorSDKTests/InstanceTests.swift +++ b/Tests/FeaturevisorSDKTests/InstanceTests.swift @@ -1111,7 +1111,7 @@ class FeaturevisorInstanceTests: XCTestCase { func testHandleDatafileFetchReturnsValidResponse() { // GIVEN - let expectation = expectation(description: "datafile_error_response_expectation") + let expectation = expectation(description: "datafile_success_response_expectation") var options = InstanceOptions.default options.datafileUrl = "https://featurevisor.datafilecontent.com" options.onReady = { _ in @@ -1150,11 +1150,8 @@ class FeaturevisorInstanceTests: XCTestCase { let expectation = expectation(description: "datafile_error_response_expectation") var wasDatafileContentFetchErrorThrown = false var errorThrownDetails: String? + var options = InstanceOptions.default - options.datafileUrl = "https://featurevisor.datafilecontent.com" - options.handleDatafileFetch = { _ in - return .failure(FeaturevisorError.unparseableJSON(data: nil, errorMessage: "Error :(")) - } options.logger = createLogger { level, message, details in guard case .error = level else { return @@ -1167,6 +1164,10 @@ class FeaturevisorInstanceTests: XCTestCase { expectation.fulfill() } + options.datafileUrl = "https://featurevisor.datafilecontent.com" + options.handleDatafileFetch = { _ in + .failure(FeaturevisorError.unparseableJSON(data: nil, errorMessage: "Error :(")) + } options.datafile = DatafileContent( schemaVersion: "1", revision: "0.0.1", @@ -1177,9 +1178,10 @@ class FeaturevisorInstanceTests: XCTestCase { // WHEN let sdk = try! createInstance(options: options) + waitForExpectations(timeout: 1) // THEN - waitForExpectations(timeout: 1) + XCTAssertFalse(sdk.isReady()) XCTAssertTrue(wasDatafileContentFetchErrorThrown) XCTAssertEqual( errorThrownDetails,