Skip to content

Commit

Permalink
Add crashing unit test for AppStorageKey subscription cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrtsa committed Nov 14, 2024
1 parent 333aa22 commit bc4d685
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Tests/ComposableArchitectureTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,22 @@ final class SharedTests: XCTestCase {
}
}
}

func testPersistenceKeySubscription() async throws {
let persistenceKey: AppStorageKey<Int> = .appStorage("shared")
let changes = LockIsolated<[Int?]>([])
var subscription: Optional = persistenceKey.subscribe(initialValue: nil) { value in
changes.withValue { $0.append(value) }
}
@Dependency(\.defaultAppStorage) var userDefaults
userDefaults.set(1, forKey: "shared")
userDefaults.set(42, forKey: "shared")
subscription?.cancel()
userDefaults.set(123, forKey: "shared")
subscription = nil
XCTAssertEqual([1, 42], changes.value)
XCTAssertEqual(123, persistenceKey.load(initialValue: nil))
}
}

@globalActor actor GA: GlobalActor {
Expand Down

0 comments on commit bc4d685

Please sign in to comment.