Skip to content

Commit 531c677

Browse files
committed
QueryWatcher uses Task Local context identifier
1 parent cba873a commit 531c677

16 files changed

+933
-792
lines changed

Tests/ApolloInternalTestHelpers/MockJSONRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extension JSONRequest {
55
public static func mock(operation: Operation) -> JSONRequest<Operation> {
66
return JSONRequest(
77
operation: operation,
8-
graphQLEndpoint: TestURL.mockServer.url,
8+
graphQLEndpoint: TestURL.mockServer.url
99
)
1010
}
1111
}

Tests/ApolloInternalTestHelpers/MockNetworkTransport.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,23 @@ public final class MockNetworkTransport: NetworkTransport {
1919
public func send<Query>(
2020
query: Query,
2121
cachePolicy: CachePolicy,
22-
contextIdentifier: UUID?,
2322
context: (any RequestContext)?
2423
) throws -> AsyncThrowingStream<GraphQLResult<Query.Data>, any Error> where Query: GraphQLQuery {
2524
try requestChainTransport.send(
2625
query: query,
2726
cachePolicy: cachePolicy,
28-
contextIdentifier: contextIdentifier,
2927
context: context
3028
)
3129
}
3230

3331
public func send<Mutation>(
3432
mutation: Mutation,
3533
cachePolicy: CachePolicy,
36-
contextIdentifier: UUID?,
3734
context: (any RequestContext)?
3835
) throws -> AsyncThrowingStream<GraphQLResult<Mutation.Data>, any Error> where Mutation: GraphQLMutation {
3936
try requestChainTransport.send(
4037
mutation: mutation,
4138
cachePolicy: cachePolicy,
42-
contextIdentifier: contextIdentifier,
4339
context: context
4440
)
4541
}

Tests/ApolloTests/Cache/StoreSubscriptionTests.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class StoreSubscriptionTests: XCTestCase {
1919

2020
// MARK: - Tests
2121

22-
func testSubscriberIsNotifiedOfStoreUpdate() throws {
22+
func testSubscriberIsNotifiedOfStoreUpdate() async throws {
2323
let cacheKeyChangeExpectation = XCTestExpectation(description: "Subscriber is notified of cache key change")
2424
let expectedChangeKeySet: Set<String> = ["QUERY_ROOT.__typename", "QUERY_ROOT.name"]
2525
let subscriber = SimpleSubscriber(cacheKeyChangeExpectation, expectedChangeKeySet)
2626

2727
store.subscribe(subscriber)
28-
addTeardownBlock { self.store.unsubscribe(subscriber) }
28+
addTeardownBlock { [store] in store!.unsubscribe(subscriber) }
2929

30-
store.publish(
30+
try await store.publish(
3131
records: [
3232
"QUERY_ROOT": [
3333
"__typename": "Hero",
@@ -36,7 +36,7 @@ class StoreSubscriptionTests: XCTestCase {
3636
]
3737
)
3838

39-
wait(for: [cacheKeyChangeExpectation], timeout: Self.defaultWaitTimeout)
39+
await fulfillment(of: [cacheKeyChangeExpectation], timeout: Self.defaultWaitTimeout)
4040
}
4141

4242
func testUnsubscribeRemovesSubscriberFromApolloStore() throws {
@@ -50,7 +50,7 @@ class StoreSubscriptionTests: XCTestCase {
5050
}
5151

5252
/// Fufills the provided expectation when all expected keys have been observed.
53-
internal class SimpleSubscriber: ApolloStoreSubscriber {
53+
internal actor SimpleSubscriber: ApolloStoreSubscriber {
5454
private let expectation: XCTestExpectation
5555
private var changeSet: Set<String>
5656

@@ -59,12 +59,18 @@ class StoreSubscriptionTests: XCTestCase {
5959
self.changeSet = changeSet
6060
}
6161

62-
func store(_ store: ApolloStore,
63-
didChangeKeys changedKeys: Set<CacheKey>,
64-
contextIdentifier: UUID?) {
65-
changeSet.subtract(changedKeys)
66-
if (changeSet.isEmpty) {
67-
expectation.fulfill()
62+
func isolatedDo(_ block: @escaping (isolated SimpleSubscriber) -> Void) {
63+
block(self)
64+
}
65+
66+
nonisolated func store(_ store: ApolloStore, didChangeKeys changedKeys: Set<CacheKey>) {
67+
Task {
68+
await self.isolatedDo {
69+
$0.changeSet.subtract(changedKeys)
70+
if ($0.changeSet.isEmpty) {
71+
$0.expectation.fulfill()
72+
}
73+
}
6874
}
6975
}
7076
}

0 commit comments

Comments
 (0)