|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import Amplify |
| 10 | +@testable import AmplifyTestCommon |
| 11 | +@testable import AWSPluginsCore |
| 12 | +@testable import AWSDataStorePlugin |
| 13 | + |
| 14 | +final class IncomingAsyncSubscriptionEventPublisherTests: XCTestCase { |
| 15 | + var apiPlugin: MockAPICategoryPlugin! |
| 16 | + override func setUp() { |
| 17 | + apiPlugin = MockAPICategoryPlugin() |
| 18 | + ModelRegistry.register(modelType: Post.self) |
| 19 | + } |
| 20 | + |
| 21 | + /// This test was written to to reproduce a bug where the subscribe would miss events emitted by the publisher. |
| 22 | + /// The pattern in this test using the publisher (`IncomingAsyncSubscriptionEventPublisher`) and subscriber |
| 23 | + /// (`IncomingAsyncSubscriptionEventToAnyModelMapper`) are identical to the usage in `AWSModelReconciliationQueue.init()`. |
| 24 | + /// |
| 25 | + /// See the changes in this PR: https://github.com/aws-amplify/amplify-swift/pull/3489 |
| 26 | + /// |
| 27 | + /// Before the PR changes, the publisher would emit events concurrently which caused some of them to be missed |
| 28 | + /// by the subscriber even though the subscriber applied back pressure to process one event at a time (demand |
| 29 | + /// of `max(1)`). For more details regarding back-pressure, see |
| 30 | + /// https://developer.apple.com/documentation/combine/processing-published-elements-with-subscribers |
| 31 | + /// |
| 32 | + /// The change, to publish the events though the same TaskQueue ensures that the events are properly buffered |
| 33 | + /// and sent only when the subscriber demands for it. |
| 34 | + func testSubscriberRecievedEvents() async throws { |
| 35 | + let expectedEvents = expectation(description: "Expected number of ") |
| 36 | + let numberOfEvents = 50 |
| 37 | + expectedEvents.expectedFulfillmentCount = numberOfEvents |
| 38 | + let asyncEvents = await IncomingAsyncSubscriptionEventPublisher( |
| 39 | + modelSchema: Post.schema, |
| 40 | + api: apiPlugin, |
| 41 | + modelPredicate: nil, |
| 42 | + auth: nil, |
| 43 | + authModeStrategy: AWSDefaultAuthModeStrategy(), |
| 44 | + awsAuthService: nil) |
| 45 | + let mapper = IncomingAsyncSubscriptionEventToAnyModelMapper() |
| 46 | + asyncEvents.subscribe(subscriber: mapper) |
| 47 | + let sink = mapper |
| 48 | + .publisher |
| 49 | + .sink( |
| 50 | + receiveCompletion: { _ in }, |
| 51 | + receiveValue: { _ in |
| 52 | + expectedEvents.fulfill() |
| 53 | + } |
| 54 | + ) |
| 55 | + DispatchQueue.concurrentPerform(iterations: numberOfEvents) { index in |
| 56 | + asyncEvents.send(.connection(.connected)) |
| 57 | + } |
| 58 | + |
| 59 | + await fulfillment(of: [expectedEvents], timeout: 2) |
| 60 | + sink.cancel() |
| 61 | + } |
| 62 | + |
| 63 | + /// Ensure that the publisher-subscriber with back pressure is receiving all the events in the order in which they were sent. |
| 64 | + func testSubscriberRecievedEventsInOrder() async throws { |
| 65 | + let expectedEvents = expectation(description: "Expected number of ") |
| 66 | + let expectedOrder = AtomicValue<[String]>(initialValue: []) |
| 67 | + let actualOrder = AtomicValue<[String]>(initialValue: []) |
| 68 | + let numberOfEvents = 50 |
| 69 | + expectedEvents.expectedFulfillmentCount = numberOfEvents |
| 70 | + let asyncEvents = await IncomingAsyncSubscriptionEventPublisher( |
| 71 | + modelSchema: Post.schema, |
| 72 | + api: apiPlugin, |
| 73 | + modelPredicate: nil, |
| 74 | + auth: nil, |
| 75 | + authModeStrategy: AWSDefaultAuthModeStrategy(), |
| 76 | + awsAuthService: nil) |
| 77 | + let mapper = IncomingAsyncSubscriptionEventToAnyModelMapper() |
| 78 | + asyncEvents.subscribe(subscriber: mapper) |
| 79 | + let sink = mapper |
| 80 | + .publisher |
| 81 | + .sink( |
| 82 | + receiveCompletion: { _ in }, |
| 83 | + receiveValue: { event in |
| 84 | + switch event { |
| 85 | + case .payload(let mutationSync): |
| 86 | + actualOrder.append(mutationSync.syncMetadata.modelId) |
| 87 | + default: |
| 88 | + break |
| 89 | + } |
| 90 | + expectedEvents.fulfill() |
| 91 | + } |
| 92 | + ) |
| 93 | + |
| 94 | + for index in 0..<numberOfEvents { |
| 95 | + let post = Post(id: "\(index)", title: "title", content: "content", createdAt: .now()) |
| 96 | + expectedOrder.append(post.id) |
| 97 | + asyncEvents.send(.data(.success(.init(model: AnyModel(post), |
| 98 | + syncMetadata: .init(modelId: post.id, |
| 99 | + modelName: "Post", |
| 100 | + deleted: false, |
| 101 | + lastChangedAt: 0, |
| 102 | + version: 0))))) |
| 103 | + } |
| 104 | + |
| 105 | + await fulfillment(of: [expectedEvents], timeout: 2) |
| 106 | + XCTAssertEqual(expectedOrder.get(), actualOrder.get()) |
| 107 | + sink.cancel() |
| 108 | + } |
| 109 | +} |
0 commit comments