-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Clear inProcess state on startup of outgoing mutation queue
- Loading branch information
Showing
10 changed files
with
199 additions
and
31 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
.../AWSDataStoreCategoryPlugin/Sync/MutationSync/MutationEvent/MutationEventClearState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import Foundation | ||
|
||
final class MutationEventClearState { | ||
|
||
let storageAdapter: StorageEngineAdapter | ||
init(storageAdapter: StorageEngineAdapter) { | ||
self.storageAdapter = storageAdapter | ||
} | ||
|
||
func clearStateOutgoingMutations(completion: @escaping BasicClosure) { | ||
let fields = MutationEvent.keys | ||
let predicate = fields.inProcess == true | ||
let orderClause = """ | ||
ORDER BY \(MutationEvent.keys.createdAt.stringValue) ASC | ||
""" | ||
|
||
storageAdapter.query(MutationEvent.self, | ||
predicate: predicate, | ||
paginationInput: nil, | ||
additionalStatements: orderClause) { result in | ||
switch result { | ||
case .failure(let dataStoreError): | ||
log.error("Failed on clearStateOutgoingMutations: \(dataStoreError)") | ||
case .success(let mutationEvents): | ||
if !mutationEvents.isEmpty { | ||
updateMutationsState(mutationEvents: mutationEvents, | ||
completion: completion) | ||
} else { | ||
completion() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func updateMutationsState(mutationEvents: [MutationEvent], completion: @escaping BasicClosure) { | ||
var numMutationEventsUpdated = 0 | ||
for mutationEvent in mutationEvents { | ||
var inProcessEvent = mutationEvent | ||
inProcessEvent.inProcess = false | ||
storageAdapter.save(inProcessEvent, condition: nil, completion: { result in | ||
switch result { | ||
case .success: | ||
numMutationEventsUpdated += 1 | ||
if numMutationEventsUpdated >= mutationEvents.count { | ||
completion() | ||
} | ||
case .failure(let error): | ||
self.log.error("Failed to update mutationEvent:\(error)") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension MutationEventClearState: DefaultLogger { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ore/AWSDataStoreCategoryPluginTests/Sync/MutationQueue/MutationEventClearStateTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import Amplify | ||
@testable import AmplifyTestCommon | ||
@testable import AWSPluginsCore | ||
@testable import AWSDataStoreCategoryPlugin | ||
|
||
class MutationEventClearStateTests: XCTestCase { | ||
var mockStorageAdapter: MockSQLiteStorageEngineAdapter! | ||
var mutationEventClearState: MutationEventClearState! | ||
|
||
override func setUp() { | ||
mockStorageAdapter = MockSQLiteStorageEngineAdapter() | ||
mutationEventClearState = MutationEventClearState(storageAdapter: mockStorageAdapter) | ||
} | ||
|
||
func testInProcessIsSetFromTrueToFalse() { | ||
let queryExpectation = expectation(description: "query is called") | ||
let saveExpectation = expectation(description: "save is Called") | ||
let completionExpectation = expectation(description: "completion handler is called") | ||
|
||
let queryResponder = QueryModelTypePredicateAdditionalStatementsResponder<MutationEvent> { _, _, _ in | ||
queryExpectation.fulfill() | ||
var mutationEvent = MutationEvent(modelId: "1111-22", | ||
modelName: "Post", | ||
json: "{}", | ||
mutationType: .create) | ||
mutationEvent.inProcess = true | ||
return .success([mutationEvent]) | ||
} | ||
mockStorageAdapter.responders[.queryModelTypePredicateAdditionalStatements] = queryResponder | ||
|
||
let saveResponder = SaveModelCompletionResponder<MutationEvent> { model, completion in | ||
XCTAssertEqual("1111-22", model.modelId) | ||
XCTAssertFalse(model.inProcess) | ||
saveExpectation.fulfill() | ||
completion(.success(model)) | ||
} | ||
mockStorageAdapter.responders[.saveModelCompletion] = saveResponder | ||
|
||
mutationEventClearState.clearStateOutgoingMutations { | ||
completionExpectation.fulfill() | ||
} | ||
wait(for: [queryExpectation, | ||
saveExpectation, | ||
completionExpectation], timeout: 1.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.