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

fix(datastore): wrap failures to result when applying remote update events #3187

Merged
merged 1 commit into from
Sep 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -264,79 +264,64 @@ class ReconcileAndLocalSaveOperation: AsynchronousOperation {
return dispositions
}

func applyRemoteModelsDisposition(
storageAdapter: StorageEngineAdapter,
disposition: RemoteSyncReconciler.Disposition
) -> AnyPublisher<Result<Void, DataStoreError>, Never> {
let operation: Future<ApplyRemoteModelResult, DataStoreError>
let mutationType: MutationEvent.MutationType
switch disposition {
case .create(let remoteModel):
operation = self.save(storageAdapter: storageAdapter, remoteModel: remoteModel)
mutationType = .create
case .update(let remoteModel):
operation = self.save(storageAdapter: storageAdapter, remoteModel: remoteModel)
mutationType = .update
case .delete(let remoteModel):
operation = self.delete(storageAdapter: storageAdapter, remoteModel: remoteModel)
mutationType = .delete
}

return operation
.flatMap { applyResult in
self.saveMetadata(storageAdapter: storageAdapter, applyResult: applyResult, mutationType: mutationType)
}
.map {_ in Result.success(()) }
.catch { Just<Result<Void, DataStoreError>>(.failure($0))}
.eraseToAnyPublisher()
}

// TODO: refactor - move each the publisher constructions to its own utility method for readability of the
// `switch` and a single method that you can invoke in the `map`
func applyRemoteModelsDispositions(
_ dispositions: [RemoteSyncReconciler.Disposition]) -> Future<Void, DataStoreError> {
Future<Void, DataStoreError> { promise in
var result: Result<Void, DataStoreError> = .failure(Self.unfulfilledDataStoreError())
defer {
promise(result)
}
guard !self.isCancelled else {
self.log.info("\(#function) - cancelled, aborting")
result = .successfulVoid
return
}
guard let storageAdapter = self.storageAdapter else {
let error = DataStoreError.nilStorageAdapter()
self.notifyDropped(count: dispositions.count, error: error)
result = .failure(error)
return
}
_ dispositions: [RemoteSyncReconciler.Disposition]
) -> Future<Void, DataStoreError> {
guard !self.isCancelled else {
self.log.info("\(#function) - cancelled, aborting")
return Future { $0(.successfulVoid) }
}

guard !dispositions.isEmpty else {
result = .successfulVoid
return
}
guard let storageAdapter = self.storageAdapter else {
let error = DataStoreError.nilStorageAdapter()
self.notifyDropped(count: dispositions.count, error: error)
return Future { $0(.failure(error)) }
}

let publishers = dispositions.map { disposition ->
Publishers.FlatMap<Future<Void, DataStoreError>,
Future<ReconcileAndLocalSaveOperation.ApplyRemoteModelResult, DataStoreError>> in

switch disposition {
case .create(let remoteModel):
let publisher = self.save(storageAdapter: storageAdapter,
remoteModel: remoteModel)
.flatMap { applyResult in
self.saveMetadata(storageAdapter: storageAdapter,
applyResult: applyResult,
mutationType: .create)
}
return publisher
case .update(let remoteModel):
let publisher = self.save(storageAdapter: storageAdapter,
remoteModel: remoteModel)
.flatMap { applyResult in
self.saveMetadata(storageAdapter: storageAdapter,
applyResult: applyResult,
mutationType: .update)
}
return publisher
case .delete(let remoteModel):
let publisher = self.delete(storageAdapter: storageAdapter,
remoteModel: remoteModel)
.flatMap { applyResult in
self.saveMetadata(storageAdapter: storageAdapter,
applyResult: applyResult,
mutationType: .delete)
}
return publisher
}
}
guard !dispositions.isEmpty else {
return Future { $0(.successfulVoid) }
}

let publishers = dispositions.map {
applyRemoteModelsDisposition(storageAdapter: storageAdapter, disposition: $0)
}

return Future { promise in
Publishers.MergeMany(publishers)
.collect()
.sink(
receiveCompletion: {
if case .failure(let error) = $0 {
result = .failure(error)
}
},
receiveValue: { _ in
result = .successfulVoid
}
)
.sink { _ in
// This stream will never fail, as we wrapped error in the result type.
promise(.successfulVoid)
} receiveValue: { _ in }
.store(in: &self.cancellables)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,11 +832,7 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
waitForExpectations(timeout: 1)
}

func testApplyRemoteModels_saveFail() throws {
if skipBrokenTests {
throw XCTSkip("TODO: fix this test")
}

func testApplyRemoteModels_skipFailedOperations() throws {
let dispositions: [RemoteSyncReconciler.Disposition] = [.create(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
Expand All @@ -846,7 +842,7 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync)]
let expect = expectation(description: "should fail")
let expect = expectation(description: "should complete")
let expectedDeleteSuccess = expectation(description: "delete should be successful")
expectedDeleteSuccess.expectedFulfillmentCount = 3 // 3 delete depositions
let expectedDropped = expectation(description: "mutationEventDropped received")
Expand Down Expand Up @@ -881,12 +877,12 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
expect.fulfill()
XCTFail("Unexpected failure completion")
case .finished:
XCTFail("Unexpected successfully completion")
expect.fulfill()
}
}, receiveValue: { _ in
XCTFail("Unexpected value received")

}).store(in: &cancellables)
waitForExpectations(timeout: 1)
}
Expand Down Expand Up @@ -949,20 +945,18 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
}

func testApplyRemoteModels_deleteFail() throws {
if skipBrokenTests {
throw XCTSkip("TODO: fix this test")
}

let dispositions: [RemoteSyncReconciler.Disposition] = [.create(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync),
.delete(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync)]
let expect = expectation(description: "should fail")
let dispositions: [RemoteSyncReconciler.Disposition] = [
.create(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync),
.delete(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync)
]
let expect = expectation(description: "should success")
let expectedCreateAndUpdateSuccess = expectation(description: "create and updates should be successful")
expectedCreateAndUpdateSuccess.expectedFulfillmentCount = 6 // 3 creates and 3 updates
let expectedDropped = expectation(description: "mutationEventDropped received")
Expand Down Expand Up @@ -997,31 +991,29 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
expect.fulfill()
XCTFail("Unexpected failure completion")
case .finished:
XCTFail("Unexpected successfully completion")
expect.fulfill()
}
}, receiveValue: { _ in
XCTFail("Unexpected value received")

}).store(in: &cancellables)
waitForExpectations(timeout: 1)
}

func testApplyRemoteModels_saveMetadataFail() throws {
if skipBrokenTests {
throw XCTSkip("TODO: fix this test")
}

let dispositions: [RemoteSyncReconciler.Disposition] = [.create(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync),
.delete(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync)]
let expect = expectation(description: "should fail")
let dispositions: [RemoteSyncReconciler.Disposition] = [
.create(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync),
.delete(anyPostMutationSync),
.create(anyPostMutationSync),
.update(anyPostMutationSync),
.delete(anyPostMutationSync)
]
let expect = expectation(description: "should success")
let expectedDropped = expectation(description: "mutationEventDropped received")
expectedDropped.expectedFulfillmentCount = 9 // 1 for each of the 9 dispositions
let saveResponder = SaveUntypedModelResponder { _, completion in
Expand Down Expand Up @@ -1053,12 +1045,12 @@ class ReconcileAndLocalSaveOperationTests: XCTestCase {
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
expect.fulfill()
XCTFail("Unexpected failure completion")
case .finished:
XCTFail("Unexpected successfully completion")
expect.fulfill()
}
}, receiveValue: { _ in
XCTFail("Unexpected value received")

}).store(in: &cancellables)
waitForExpectations(timeout: 1)
}
Expand Down
Loading