From d0780950d3d42a8e26d44dc8e74579c639fdc8d5 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Thu, 4 Jan 2024 10:46:10 -0800 Subject: [PATCH] resolve comments --- .../SyncMutationToCloudOperation.swift | 12 ++++++++++++ .../Sync/Support/MutationEventExtensionsTests.swift | 6 ------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/MutationSync/OutgoingMutationQueue/SyncMutationToCloudOperation.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/MutationSync/OutgoingMutationQueue/SyncMutationToCloudOperation.swift index bd50e92c7a..738558fdc6 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/MutationSync/OutgoingMutationQueue/SyncMutationToCloudOperation.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/MutationSync/OutgoingMutationQueue/SyncMutationToCloudOperation.swift @@ -103,6 +103,18 @@ class SyncMutationToCloudOperation: AsynchronousOperation { } } + /// Always retrieve and use the largest version when available. The source of the version comes + /// from either the MutationEvent itself, which represents the queue request, or the persisted version + /// from the metadata table. + /// + /// **Version in the Mutation Event**. If there are mulitple mutation events pending, each outgoing + /// mutation processing will result in synchronously updating the pending mutation's version + /// before enqueuing the mutation response for reconciliation. + /// + /// **Version persisted in the metadata table**: Reconciliation will persist the latest version in the + /// metadata table. In cases of quick consecutive updates, the MutationEvent's version could + /// be greater than the persisted since the MutationEvent is updated from the original thread that + /// processed the outgoing mutation. private func getLatestVersion(_ mutationEvent: MutationEvent) -> Int? { let latestSyncedMetadataVersion = getLatestSyncMetadata()?.version let mutationEventVersion = mutationEvent.version diff --git a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Sync/Support/MutationEventExtensionsTests.swift b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Sync/Support/MutationEventExtensionsTests.swift index 7938a953ff..ab8f7640c1 100644 --- a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Sync/Support/MutationEventExtensionsTests.swift +++ b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Sync/Support/MutationEventExtensionsTests.swift @@ -14,7 +14,6 @@ import XCTest @testable import AWSDataStorePlugin @testable import AWSPluginsCore -// TODO: This flaky test has been disabled, tracking issue: https://github.com/aws-amplify/amplify-ios/issues/1831 // swiftlint:disable type_body_length class MutationEventExtensionsTest: BaseDataStoreTests { @@ -23,7 +22,6 @@ class MutationEventExtensionsTest: BaseDataStoreTests { /// - When: The sent model matches the received model and the first pending mutation event version is `nil`. /// - Then: The pending mutation event version should be updated to the received model version of 1. func testSentModelWithNilVersion_Reconciled() throws { - throw XCTSkip("TODO: fix this test") let modelId = UUID().uuidString let post = Post(id: modelId, title: "title", content: "content", createdAt: .now()) let requestMutationEvent = try createMutationEvent(model: post, @@ -88,7 +86,6 @@ class MutationEventExtensionsTest: BaseDataStoreTests { /// - Then: The first pending mutation event(update) version should be updated to the received model version of 1 /// and the second pending mutation event version(delete) should not be updated. func testSentModelWithNilVersion_SecondPendingEventNotReconciled() throws { - throw XCTSkip("TODO: fix this test") let modelId = UUID().uuidString let post = Post(id: modelId, title: "title", content: "content", createdAt: .now()) let requestMutationEvent = try createMutationEvent(model: post, @@ -158,7 +155,6 @@ class MutationEventExtensionsTest: BaseDataStoreTests { /// - When: The sent model matches the received model and the first pending mutation event version is 2. /// - Then: The first pending mutation event version should NOT be updated. func testSentModelVersionNewerThanResponseVersion_PendingEventNotReconciled() throws { - throw XCTSkip("TODO: fix this test") let modelId = UUID().uuidString let post1 = Post(id: modelId, title: "title1", content: "content1", createdAt: .now()) let post2 = Post(id: modelId, title: "title2", content: "content2", createdAt: .now()) @@ -221,7 +217,6 @@ class MutationEventExtensionsTest: BaseDataStoreTests { /// - When: The sent model doesn't match the received model and the first pending mutation event version is 1. /// - Then: The first pending mutation event version should NOT be updated. func testSentModelNotEqualToResponseModel_PendingEventNotReconciled() throws { - throw XCTSkip("TODO: fix this test") let modelId = UUID().uuidString let post1 = Post(id: modelId, title: "title1", content: "content1", createdAt: .now()) let post2 = Post(id: modelId, title: "title2", content: "content2", createdAt: .now()) @@ -285,7 +280,6 @@ class MutationEventExtensionsTest: BaseDataStoreTests { /// - When: The sent model matches the received model and the first pending mutation event version is 1. /// - Then: The first pending mutation event version should be updated to received mutation sync version i.e. 2. func testPendingVersionReconciledSuccess() throws { - throw XCTSkip("TODO: fix this test") let modelId = UUID().uuidString let post1 = Post(id: modelId, title: "title1", content: "content1", createdAt: .now()) let post2 = Post(id: modelId, title: "title2", content: "content2", createdAt: .now())