Skip to content

Commit a0263ef

Browse files
committed
chore: update cache migration tests to use new cache key format for identified & anonymous configs
1 parent 9202cf4 commit a0263ef

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

DevCycle/Models/Cache.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class CacheService: CacheServiceProtocol {
4242
init(configCacheTTL: Int = DEFAULT_CONFIG_CACHE_TTL) {
4343
self.configCacheTTL = configCacheTTL
4444
migrateLegacyCache()
45-
clearDeprecatedCachedConfigs()
4645
}
4746

4847
func setAnonUserId(anonUserId: String) {
@@ -133,7 +132,7 @@ class CacheService: CacheServiceProtocol {
133132
return baseKey
134133
}
135134

136-
private func clearDeprecatedCachedConfigs() {
135+
private func cleanupDeprecatedCachedConfigs() {
137136
let deprecatedKeys: [String] = defaults.dictionaryRepresentation().keys.compactMap { key in
138137
// Only include keys that contain one of these patterns
139138
guard key.contains(CacheKeys.identifiedConfig) || key.contains(CacheKeys.anonymousConfig) else {
@@ -163,6 +162,9 @@ class CacheService: CacheServiceProtocol {
163162

164163
// Clean up legacy config cache
165164
cleanupLegacyConfigCache()
165+
166+
// Clean up config cache from other SDK versions
167+
cleanupDeprecatedCachedConfigs()
166168
}
167169

168170
private func cleanupLegacyUserCache() {

DevCycleTests/Models/DevCycleUserTest.swift

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,17 @@ class DevCycleUserTest: XCTestCase {
240240

241241
let identifiedUserId = "identified_user_123"
242242
let anonymousUserId = "anon_user_456"
243+
let versionPrefix = "VERSION_\(PlatformDetails().sdkVersion)"
243244
let configData = "{\"variables\": {\"test\": \"value\"}}".data(using: .utf8)
244245
let fetchDate = Int(Date().timeIntervalSince1970)
245246

246-
defaults.set(configData, forKey: "IDENTIFIED_CONFIG")
247-
defaults.set(identifiedUserId, forKey: "IDENTIFIED_CONFIG.USER_ID")
248-
defaults.set(fetchDate, forKey: "IDENTIFIED_CONFIG.FETCH_DATE")
247+
defaults.set(configData, forKey: "\(versionPrefix).IDENTIFIED_CONFIG")
248+
defaults.set(identifiedUserId, forKey: "\(versionPrefix).IDENTIFIED_CONFIG.USER_ID")
249+
defaults.set(fetchDate, forKey: "\(versionPrefix).IDENTIFIED_CONFIG.FETCH_DATE")
249250

250-
defaults.set(configData, forKey: "ANONYMOUS_CONFIG")
251-
defaults.set(anonymousUserId, forKey: "ANONYMOUS_CONFIG.USER_ID")
252-
defaults.set(fetchDate, forKey: "ANONYMOUS_CONFIG.FETCH_DATE")
251+
defaults.set(configData, forKey: "\(versionPrefix).ANONYMOUS_CONFIG")
252+
defaults.set(anonymousUserId, forKey: "\(versionPrefix).ANONYMOUS_CONFIG.USER_ID")
253+
defaults.set(fetchDate, forKey: "\(versionPrefix).ANONYMOUS_CONFIG.FETCH_DATE")
253254

254255
cacheService.migrateLegacyCache()
255256

@@ -274,25 +275,25 @@ class DevCycleUserTest: XCTestCase {
274275
"Legacy anonymous fetch date should be removed")
275276

276277
XCTAssertEqual(
277-
defaults.object(forKey: "IDENTIFIED_CONFIG_\(identifiedUserId)") as? Data,
278+
defaults.object(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(identifiedUserId)") as? Data,
278279
configData,
279280
"New identified config data should match original")
280281
XCTAssertNotNil(
281-
defaults.object(forKey: "IDENTIFIED_CONFIG_\(identifiedUserId).EXPIRY_DATE"),
282+
defaults.object(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(identifiedUserId).EXPIRY_DATE"),
282283
"New identified expiry date should be set")
283284

284285
XCTAssertEqual(
285-
defaults.object(forKey: "ANONYMOUS_CONFIG_\(anonymousUserId)") as? Data,
286+
defaults.object(forKey: "\(versionPrefix).ANONYMOUS_CONFIG_\(anonymousUserId)") as? Data,
286287
configData,
287288
"New anonymous config data should match original")
288289
XCTAssertNotNil(
289-
defaults.object(forKey: "ANONYMOUS_CONFIG_\(anonymousUserId).EXPIRY_DATE"),
290+
defaults.object(forKey: "\(versionPrefix).ANONYMOUS_CONFIG_\(anonymousUserId).EXPIRY_DATE"),
290291
"New anonymous expiry date should be set")
291292

292-
defaults.removeObject(forKey: "IDENTIFIED_CONFIG_\(identifiedUserId)")
293-
defaults.removeObject(forKey: "IDENTIFIED_CONFIG_\(identifiedUserId).EXPIRY_DATE")
294-
defaults.removeObject(forKey: "ANONYMOUS_CONFIG_\(anonymousUserId)")
295-
defaults.removeObject(forKey: "ANONYMOUS_CONFIG_\(anonymousUserId).EXPIRY_DATE")
293+
defaults.removeObject(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(identifiedUserId)")
294+
defaults.removeObject(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(identifiedUserId).EXPIRY_DATE")
295+
defaults.removeObject(forKey: "\(versionPrefix).ANONYMOUS_CONFIG_\(anonymousUserId)")
296+
defaults.removeObject(forKey: "\(versionPrefix).ANONYMOUS_CONFIG_\(anonymousUserId).EXPIRY_DATE")
296297
}
297298

298299
func testLegacyCacheMigrationSkipsWhenNoData() {
@@ -310,6 +311,7 @@ class DevCycleUserTest: XCTestCase {
310311
let defaults = UserDefaults.standard
311312

312313
let userId = "test_user_123"
314+
let versionPrefix = "VERSION_\(PlatformDetails().sdkVersion)"
313315
let legacyConfigData = "{\"variables\": {\"legacy\": \"oldValue\"}}".data(using: .utf8)
314316
let newConfigData = "{\"variables\": {\"new\": \"newValue\"}}".data(using: .utf8)
315317
let legacyFetchDate = Int(Date().timeIntervalSince1970) - 3600 // 1 hour ago
@@ -319,9 +321,9 @@ class DevCycleUserTest: XCTestCase {
319321
defaults.set(userId, forKey: "IDENTIFIED_CONFIG.USER_ID")
320322
defaults.set(legacyFetchDate, forKey: "IDENTIFIED_CONFIG.FETCH_DATE")
321323

322-
defaults.set(newConfigData, forKey: "IDENTIFIED_CONFIG_\(userId)")
323-
defaults.set(userId, forKey: "IDENTIFIED_CONFIG_\(userId).USER_ID")
324-
defaults.set(newExpiryDate, forKey: "IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE")
324+
defaults.set(newConfigData, forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId)")
325+
defaults.set(userId, forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId).USER_ID")
326+
defaults.set(newExpiryDate, forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE")
325327

326328
cacheService.migrateLegacyCache()
327329

@@ -336,17 +338,17 @@ class DevCycleUserTest: XCTestCase {
336338
"Legacy fetch date should be removed when new cache exists")
337339

338340
XCTAssertEqual(
339-
defaults.object(forKey: "IDENTIFIED_CONFIG_\(userId)") as? Data,
341+
defaults.object(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId)") as? Data,
340342
newConfigData,
341343
"New config data should remain unchanged")
342344
XCTAssertEqual(
343-
defaults.integer(forKey: "IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE"),
345+
defaults.integer(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE"),
344346
newExpiryDate,
345347
"New expiry date should remain unchanged")
346348

347-
defaults.removeObject(forKey: "IDENTIFIED_CONFIG_\(userId)")
348-
defaults.removeObject(forKey: "IDENTIFIED_CONFIG_\(userId).USER_ID")
349-
defaults.removeObject(forKey: "IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE")
349+
defaults.removeObject(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId)")
350+
defaults.removeObject(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId).USER_ID")
351+
defaults.removeObject(forKey: "\(versionPrefix).IDENTIFIED_CONFIG_\(userId).EXPIRY_DATE")
350352
}
351353

352354
func testLegacyUserCacheCleanup() {

0 commit comments

Comments
 (0)