Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache load errors cause deadlock in write transaction #365

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/Apollo/DataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public final class DataLoader<Key: Hashable, Value> {

let keys = loads.map { $0.key }

self.batchLoad(keys).andThen { values in
self.batchLoad(keys).catch { error in
loads.forEach { $0.reject(error) }
}.andThen { values in
for (load, value) in zip(loads, values) {
load.fulfill(value)
}
Expand Down
30 changes: 30 additions & 0 deletions Tests/ApolloCacheDependentTests/LoadQueryFromStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,36 @@ class LoadQueryFromStoreTests: XCTestCase {
}
}


func testLoadingWithBadCacheSerialization() throws {
let initialRecords: RecordSet = [
"QUERY_ROOT": ["hero": Reference(key: "2001")],
"2001": [
"name": "R2-D2",
"__typename": "Droid",
"friends": [
Reference(key: "1000"),
Reference(key: "1002"),
Reference(key: "1003")
]
],
"1000": ["__typename": "Human", "name": ["dictionary": "badValues", "nested bad val": ["subdictionary": "some value"] ]
],
"1002": ["__typename": "Human", "name": "Han Solo"],
"1003": ["__typename": "Human", "name": "Leia Organa"],
]

withCache(initialRecords: initialRecords) { (cache) in
store = ApolloStore(cache: cache)

let query = HeroAndFriendsNamesQuery()
load(query: query) { (result, error) in
XCTAssertNil(result)
XCTAssertNotNil(error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating this to

XCTAssertTrue(error is GraphQLResultError)
XCTAssertTrue((error as! GraphQLResultError).underlying is JSONDecodingError)

provides a bit more confidence about what is happening here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use guard let instead of force unwrapping because force unwrapping crashes the whole test suite instead of just failing the test, but I agree here

}
}
}

// MARK: - Helpers

private func load<Query: GraphQLQuery>(query: Query, resultHandler: @escaping OperationResultHandler<Query>) {
Expand Down