Skip to content

Commit

Permalink
Fix auto poll initial config load from cache (#44)
Browse files Browse the repository at this point in the history
* Fix initial config load when auto poll enabled with results from cache

* Update AutoPollingTests.swift
  • Loading branch information
z4kn4fein authored May 7, 2024
1 parent c63143e commit 502954c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ConfigCat.podspec
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ConfigCat.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
]
```
Expand Down
40 changes: 18 additions & 22 deletions Sources/ConfigCat/ConfigService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ConfigCat/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions Tests/ConfigCatTests/AutoPollingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 502954c

Please sign in to comment.