From da6becadfc43e8b62e58b60a59629e221051394b Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Wed, 27 Mar 2024 17:02:17 +0100 Subject: [PATCH] Fixing a cache read issue (#42) * Fixing a cache read issue * Update CacheTest.swift --- ConfigCat.podspec | 2 +- ConfigCat.xcconfig | 2 +- README.md | 2 +- Sources/ConfigCat/ConfigService.swift | 2 +- Sources/ConfigCat/Utils.swift | 2 +- Tests/ConfigCatTests/CacheTest.swift | 25 +++++++++++++++++++++++++ 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ConfigCat.podspec b/ConfigCat.podspec index 4adb830..fae534e 100755 --- a/ConfigCat.podspec +++ b/ConfigCat.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = "ConfigCat" - spec.version = "11.0.0" + spec.version = "11.0.1" spec.summary = "ConfigCat Swift SDK" spec.swift_version = "5.0" diff --git a/ConfigCat.xcconfig b/ConfigCat.xcconfig index 7b83b1d..b74f0c8 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.0 +MARKETING_VERSION = 11.0.1 diff --git a/README.md b/README.md index 08489fb..ef03158 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.0") + .package(url: "https://github.com/configcat/swift-sdk", from: "11.0.1") ] ``` diff --git a/Sources/ConfigCat/ConfigService.swift b/Sources/ConfigCat/ConfigService.swift index 94ba104..4438579 100644 --- a/Sources/ConfigCat/ConfigService.swift +++ b/Sources/ConfigCat/ConfigService.swift @@ -193,7 +193,7 @@ class ConfigService { defer { mutex.unlock() } // Sync up with the cache and use it when it's not expired. let entry = readCache() - if cachedEntry.isEmpty && entry != cachedEntry { + if !entry.isEmpty && entry != cachedEntry { cachedEntry = entry hooks.invokeOnConfigChanged(config: entry.config) } diff --git a/Sources/ConfigCat/Utils.swift b/Sources/ConfigCat/Utils.swift index 0ddf848..a61c002 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.0" + static let version: String = "11.0.1" 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/CacheTest.swift b/Tests/ConfigCatTests/CacheTest.swift index 3e28f85..44d8b48 100644 --- a/Tests/ConfigCatTests/CacheTest.swift +++ b/Tests/ConfigCatTests/CacheTest.swift @@ -25,4 +25,29 @@ class CacheTests: XCTestCase { XCTAssertEqual(testJson, fromCache.configJson) XCTAssertEqual("test-etag", fromCache.eTag) } + + #if compiler(>=5.5) && canImport(_Concurrency) + @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) + func testCacheTTLRespectsExternalCache() async { + let testJson = "{\"f\":{\"testKey\":{\"t\":1,\"v\":{\"s\":\"%@\"}}}}" + let engine = MockEngine() + engine.enqueueResponse(response: Response(body: String(format: testJson, "remote"), statusCode: 200)) + + let entry = try! ConfigEntry.fromConfigJson(json: String(format: testJson, "local"), eTag: "test-etag", fetchTime: Date()).get() + let cache = SingleValueCache(initValue: entry.serialize()) + let client = ConfigCatClient(sdkKey: randomSdkKey(), pollingMode: PollingModes.lazyLoad(cacheRefreshIntervalInSeconds: 1), logger: NoLogger(), httpEngine: engine, configCache: cache) + var val = await client.getValue(for: "testKey", defaultValue: "") + + XCTAssertEqual("local", val) + XCTAssertEqual(0, engine.requests.count) + + let entry2 = try! ConfigEntry.fromConfigJson(json: String(format: testJson, "local2"), eTag: "test-etag2", fetchTime: Date()).get() + try! cache.write(for: "", value: entry2.serialize()) + + val = await client.getValue(for: "testKey", defaultValue: "") + + XCTAssertEqual("local2", val) + XCTAssertEqual(0, engine.requests.count) + } + #endif }