From 502954c97a9947d820f3cabd00b41229e1d0588c Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Tue, 7 May 2024 13:26:26 +0200 Subject: [PATCH] Fix auto poll initial config load from cache (#44) * Fix initial config load when auto poll enabled with results from cache * Update AutoPollingTests.swift --- ConfigCat.podspec | 2 +- ConfigCat.xcconfig | 2 +- README.md | 2 +- Sources/ConfigCat/ConfigService.swift | 40 ++++++++++----------- Sources/ConfigCat/Utils.swift | 2 +- Tests/ConfigCatTests/AutoPollingTests.swift | 20 +++++++++++ 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/ConfigCat.podspec b/ConfigCat.podspec index b42268f..3aef8fc 100755 --- a/ConfigCat.podspec +++ b/ConfigCat.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = "ConfigCat" - spec.version = "11.0.2" + spec.version = "11.0.3" spec.summary = "ConfigCat Swift SDK" spec.swift_version = "5.0" diff --git a/ConfigCat.xcconfig b/ConfigCat.xcconfig index ffe01b6..e59d619 100644 --- a/ConfigCat.xcconfig +++ b/ConfigCat.xcconfig @@ -38,4 +38,4 @@ SUPPORTED_PLATFORMS = macosx iphoneos iphonesimulator watchos watchsimulator app SWIFT_VERSION = 5.0 // ConfigCat SDK version -MARKETING_VERSION = 11.0.2 +MARKETING_VERSION = 11.0.3 diff --git a/README.md b/README.md index 4edd47b..9642693 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ The following device platform versions are supported: ``` swift dependencies: [ - .package(url: "https://github.com/configcat/swift-sdk", from: "11.0.2") + .package(url: "https://github.com/configcat/swift-sdk", from: "11.0.3") ] ``` diff --git a/Sources/ConfigCat/ConfigService.swift b/Sources/ConfigCat/ConfigService.swift index 4438579..730767e 100644 --- a/Sources/ConfigCat/ConfigService.swift +++ b/Sources/ConfigCat/ConfigService.swift @@ -99,28 +99,24 @@ class ConfigService { } func settings(completion: @escaping (SettingsResult) -> Void) { - switch pollingMode { - case let lazyMode as LazyLoadingMode: - fetchIfOlder(threshold: Date().subtract(seconds: lazyMode.cacheRefreshIntervalInSeconds)!) { result in - switch result { - case .success(let entry): completion(!entry.isEmpty - ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) - : .empty) - case .failure(_, let entry): completion(!entry.isEmpty - ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) - : .empty) - } - } - default: - fetchIfOlder(threshold: .distantPast, preferCache: initialized) { result in - switch result { - case .success(let entry): completion(!entry.isEmpty - ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) - : .empty) - case .failure(_, let entry): completion(!entry.isEmpty - ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) - : .empty) - } + var threshold = Date.distantPast + var preferCache = initialized + if let lazyMode = pollingMode as? LazyLoadingMode { + threshold = Date().subtract(seconds: lazyMode.cacheRefreshIntervalInSeconds)! + preferCache = false + } + else if let autoPoll = pollingMode as? AutoPollingMode, !initialized { + threshold = Date().subtract(seconds: autoPoll.autoPollIntervalInSeconds)! + } + + fetchIfOlder(threshold: threshold, preferCache: preferCache) { result in + switch result { + case .success(let entry): completion(!entry.isEmpty + ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) + : .empty) + case .failure(_, let entry): completion(!entry.isEmpty + ? SettingsResult(settings: entry.config.settings, fetchTime: entry.fetchTime) + : .empty) } } } diff --git a/Sources/ConfigCat/Utils.swift b/Sources/ConfigCat/Utils.swift index f864701..f4d2079 100644 --- a/Sources/ConfigCat/Utils.swift +++ b/Sources/ConfigCat/Utils.swift @@ -47,7 +47,7 @@ extension Equatable { } class Constants { - static let version: String = "11.0.2" + static let version: String = "11.0.3" static let configJsonName: String = "config_v6.json" static let configJsonCacheVersion: String = "v2" static let globalBaseUrl: String = "https://cdn-global.configcat.com" diff --git a/Tests/ConfigCatTests/AutoPollingTests.swift b/Tests/ConfigCatTests/AutoPollingTests.swift index 30899a7..9fec2b1 100755 --- a/Tests/ConfigCatTests/AutoPollingTests.swift +++ b/Tests/ConfigCatTests/AutoPollingTests.swift @@ -210,6 +210,26 @@ class AutoPollingTests: XCTestCase { XCTAssertEqual(1, engine.requests.count) } + + func testPollsWhenCacheExpired() { + let engine = MockEngine() + engine.enqueueResponse(response: Response(body: String(format: testJsonFormat, "test1"), statusCode: 200)) + + let initValue = String(format: testJsonFormat, "test").asEntryString(date: Date().subtract(seconds: 5)!) + let cache = SingleValueCache(initValue: initValue) + let mode = PollingModes.autoPoll(autoPollIntervalInSeconds: 2) + let fetcher = ConfigFetcher(httpEngine: engine, logger: InternalLogger.noLogger, sdkKey: "", mode: mode.identifier, dataGovernance: .global) + let service = ConfigService(log: InternalLogger.noLogger, fetcher: fetcher, cache: cache, pollingMode: mode, hooks: Hooks(), sdkKey: "", offline: false) + + let expectation1 = expectation(description: "wait for settings") + service.settings { settingsResult in + XCTAssertEqual("test1", settingsResult.settings["fakeKey"]?.value.stringValue) + expectation1.fulfill() + } + wait(for: [expectation1], timeout: 5) + + XCTAssertEqual(1, engine.requests.count) + } func testOnlineOffline() { let engine = MockEngine()