Skip to content

Commit c716f73

Browse files
author
Di Wu
authored
test(datastore): enable datastore base integration test (#3014)
1 parent 10e03f1 commit c716f73

File tree

18 files changed

+118
-234
lines changed

18 files changed

+118
-234
lines changed

.github/workflows/integ_test_datastore.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ jobs:
3030
aws_s3_bucket: ${{ secrets.AWS_S3_BUCKET_INTEG }}
3131

3232
datastore-base-test:
33-
if: ${{ false }}
3433
needs: prepare-for-test
3534
uses: ./.github/workflows/integ_test_datastore_base.yml
3635
secrets: inherit
@@ -51,7 +50,6 @@ jobs:
5150
secrets: inherit
5251

5352
datastore-multi-auth-test:
54-
if: ${{ false }}
5553
needs: prepare-for-test
5654
uses: ./.github/workflows/integ_test_datastore_multi_auth.yml
5755
secrets: inherit

.github/workflows/integ_test_datastore_base.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ permissions:
99

1010
jobs:
1111
datastore-integration-test-base:
12-
timeout-minutes: 30
12+
timeout-minutes: 45
1313
runs-on: macos-13
1414
environment: IntegrationTest
1515
steps:

.github/workflows/integ_test_datastore_lazy_load.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ permissions:
99

1010
jobs:
1111
datastore-integration-lazy-load-test:
12-
timeout-minutes: 30
13-
runs-on: macos-12
12+
timeout-minutes: 45
13+
runs-on: macos-13
1414
environment: IntegrationTest
1515
steps:
1616
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
@@ -33,3 +33,5 @@ jobs:
3333
with:
3434
project_path: ./AmplifyPlugins/DataStore/Tests/DataStoreHostApp
3535
scheme: AWSDataStorePluginLazyLoadTests
36+
destination: 'platform=iOS Simulator,name=iPhone 14,OS=latest'
37+
xcode_path: '/Applications/Xcode_14.3.app'

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
236236

237237
public func reset() async {
238238
dispatchedModelSyncedEvents = [:]
239+
dataStorePublisher?.sendFinished()
239240
if let resettable = storageEngine as? Resettable {
240241
log.verbose("Resetting storageEngine")
241242
await resettable.reset()

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginAuthCognitoTests/AWSDataStoreAuthBaseTest.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class AuthRecorderInterceptor: URLRequestInterceptor {
6464
}
6565

6666
class AWSDataStoreAuthBaseTest: XCTestCase {
67-
var requests: Set<AnyCancellable> = []
68-
6967
var amplifyConfig: AmplifyConfiguration!
7068
var user1: TestUser?
7169
var user2: TestUser?
@@ -77,9 +75,9 @@ class AWSDataStoreAuthBaseTest: XCTestCase {
7775

7876
override func tearDown() async throws {
7977
try await clearDataStore()
80-
requests = []
8178
try await signOut()
8279
await Amplify.reset()
80+
try await Task.sleep(seconds: 1)
8381
}
8482

8583
// MARK: - Test Helpers
@@ -302,8 +300,11 @@ extension AWSDataStoreAuthBaseTest {
302300

303301
/// Asserts that DataStore is in a ready state and subscriptions are established
304302
/// - Parameter events: DataStore Hub events
305-
func assertDataStoreReady(_ expectations: AuthTestExpectations,
306-
expectedModelSynced: Int = 1) async throws {
303+
func assertDataStoreReady(
304+
_ expectations: AuthTestExpectations,
305+
expectedModelSynced: Int = 1
306+
) async throws {
307+
var requests: Set<AnyCancellable> = []
307308
var modelSyncedCount = 0
308309
let dataStoreEvents = HubPayload.EventName.DataStore.self
309310
Amplify
@@ -341,9 +342,12 @@ extension AWSDataStoreAuthBaseTest {
341342
/// - model: model instance saved and then deleted
342343
/// - expectations: test expectations
343344
/// - onFailure: failure callback
344-
func assertMutations<M: Model>(model: M,
345-
_ expectations: AuthTestExpectations,
346-
onFailure: @escaping (_ error: DataStoreError) -> Void) async throws {
345+
func assertMutations<M: Model>(
346+
model: M,
347+
_ expectations: AuthTestExpectations,
348+
onFailure: @escaping (_ error: DataStoreError) -> Void
349+
) async throws {
350+
var requests: Set<AnyCancellable> = []
347351
Amplify
348352
.Hub
349353
.publisher(for: .dataStore)

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/Connection/DataStoreConnectionScenario1Tests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DataStoreConnectionScenario1Tests: SyncEngineIntegrationTestBase {
6464
}
6565

6666
_ = try await Amplify.DataStore.save(team)
67-
await waitForExpectations(timeout: networkTimeout)
67+
await fulfillment(of: [syncedTeamReceived], timeout: networkTimeout)
6868

6969
let syncProjectReceived = expectation(description: "received project from sync path")
7070
hubListener = Amplify.Hub.listen(to: .dataStore,
@@ -84,7 +84,7 @@ class DataStoreConnectionScenario1Tests: SyncEngineIntegrationTestBase {
8484
return
8585
}
8686
_ = try await Amplify.DataStore.save(project)
87-
await waitForExpectations(timeout: networkTimeout)
87+
await fulfillment(of: [syncProjectReceived], timeout: networkTimeout)
8888

8989
let queriedProjectOptional = try await Amplify.DataStore.query(Project1.self, byId: project.id)
9090
guard let queriedProject = queriedProjectOptional else {
@@ -126,7 +126,7 @@ class DataStoreConnectionScenario1Tests: SyncEngineIntegrationTestBase {
126126
}
127127
project.team = anotherTeam
128128
_ = try await Amplify.DataStore.save(project)
129-
await waitForExpectations(timeout: networkTimeout)
129+
await fulfillment(of: [syncUpdatedProjectReceived], timeout: networkTimeout)
130130

131131
let queriedProjectOptional = try await Amplify.DataStore.query(Project1.self, byId: project.id)
132132
XCTAssertNotNil(queriedProjectOptional)
@@ -173,8 +173,7 @@ class DataStoreConnectionScenario1Tests: SyncEngineIntegrationTestBase {
173173

174174
_ = try await Amplify.DataStore.save(team)
175175
_ = try await Amplify.DataStore.save(project)
176-
177-
await waitForExpectations(timeout: TestCommonConstants.networkTimeout)
176+
await fulfillment(of: [createReceived], timeout: TestCommonConstants.networkTimeout)
178177

179178
let deleteReceived = expectation(description: "Delete notification received")
180179
deleteReceived.expectedFulfillmentCount = 2 // 1 project and 1 team
@@ -206,7 +205,7 @@ class DataStoreConnectionScenario1Tests: SyncEngineIntegrationTestBase {
206205
// TODO: Delete Team should not be necessary, cascade delete should delete the team when deleting the project.
207206
// Once cascade works for hasOne, the following code can be removed.
208207
_ = try await Amplify.DataStore.delete(team)
209-
await waitForExpectations(timeout: TestCommonConstants.networkTimeout)
208+
await fulfillment(of: [deleteReceived], timeout: TestCommonConstants.networkTimeout)
210209

211210
let queriedProject = try await Amplify.DataStore.query(Project1.self, byId: project.id)
212211
XCTAssertNil(queriedProject)

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/DataStoreEndToEndTests.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
3636

3737
func testCreate() async throws {
3838
await setUp(withModels: TestModelRegistration())
39-
try await startAmplifyAndWaitForSync()
39+
try await startAmplifyAndWaitForReady()
4040
var cancellables = Set<AnyCancellable>()
4141
let date = Temporal.DateTime.now()
4242
let newPost = Post(
@@ -55,20 +55,25 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
5555
let syncReceived = expectation(description: "SyncReceived(MutationEvent(version: 1))")
5656
let localEventReceived = expectation(description: "received mutation event with version nil")
5757
let remoteEventReceived = expectation(description: "received mutation event with version 1")
58+
5859
Amplify.Hub.publisher(for: .dataStore)
5960
.sink { payload in
6061
let event = DataStoreHubEvent(payload: payload)
6162
switch event {
62-
case .outboxMutationEnqueued:
63-
outboxMutationEnqueued.fulfill()
63+
case .outboxMutationEnqueued(let event):
64+
if event.element.model.identifier == newPost.identifier {
65+
outboxMutationEnqueued.fulfill()
66+
}
6467
case .outboxStatus(let status):
6568
if !status.isEmpty {
6669
outboxIsNotEmptyReceived.fulfill()
6770
} else {
6871
outboxIsEmptyReceived.fulfill()
6972
}
70-
case .outboxMutationProcessed:
71-
outboxMutationProcessed.fulfill()
73+
case .outboxMutationProcessed(let event):
74+
if event.element.model.identifier == newPost.identifier {
75+
outboxMutationProcessed.fulfill()
76+
}
7277
case .syncReceived(let mutationEvent):
7378
guard let post = try? mutationEvent.decodeModel() as? Post, post.id == newPost.id else {
7479
return
@@ -77,14 +82,13 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
7782
XCTAssertEqual(post.content, newPost.content)
7883
XCTAssertEqual(mutationEvent.version, 1)
7984
syncReceived.fulfill()
80-
return
8185
}
8286
default:
8387
break
8488
}
8589
}.store(in: &cancellables)
8690

87-
let task = Task {
91+
Task {
8892
let mutationEvents = Amplify.DataStore.observe(Post.self)
8993
do {
9094
for try await mutationEvent in mutationEvents {
@@ -109,7 +113,7 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
109113
syncReceived,
110114
localEventReceived,
111115
remoteEventReceived
112-
], timeout: 10)
116+
], timeout: 30)
113117
}
114118

115119
func testCreateMutateDelete() async throws {
@@ -530,8 +534,8 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
530534
try await startAmplifyAndWaitForSync()
531535

532536
let parallelSize = 100
533-
let initExpectation = asyncExpectation(description: "expect MutationEventPublisher works fine")
534-
let parallelExpectation = asyncExpectation(description: "expect parallel processing no data loss")
537+
let initExpectation = expectation(description: "expect MutationEventPublisher works fine")
538+
let parallelExpectation = expectation(description: "expect parallel processing no data loss")
535539

536540
let newPost = Post(title: UUID().uuidString, content: UUID().uuidString, createdAt: .now())
537541

@@ -553,27 +557,27 @@ class DataStoreEndToEndTests: SyncEngineIntegrationTestBase {
553557
.compactMap(extractPost)
554558
.sink { post in
555559
if post.title == newPost.title {
556-
Task { await initExpectation.fulfill() }
560+
initExpectation.fulfill()
557561
}
558562

559563
if post.title.hasPrefix(titlePrefix) {
560564
expectedResult.insert(post.title)
561565
}
562566

563567
if expectedResult.count == parallelSize {
564-
Task { await parallelExpectation.fulfill() }
568+
parallelExpectation.fulfill()
565569
}
566570
}
567571
try await Amplify.DataStore.save(newPost)
568-
await AsyncTesting.waitForExpectations([initExpectation], timeout: 10)
572+
await fulfillment(of: [initExpectation], timeout: 10)
569573
try await Task.sleep(seconds: 1)
570574

571575
for post in posts {
572576
Task {
573577
try? await Amplify.DataStore.save(post)
574578
}
575579
}
576-
await AsyncTesting.waitForExpectations([parallelExpectation], timeout: Double(parallelSize))
580+
await fulfillment(of: [parallelExpectation], timeout: Double(parallelSize))
577581
cancellable.cancel()
578582
XCTAssertEqual(expectedResult, Set(posts.map(\.title)))
579583
}

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/DataStoreHubEventsTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase {
4141
try configureAmplify(withModels: TestModelRegistration())
4242
try await Amplify.DataStore.clear()
4343
await Amplify.reset()
44+
try await Task.sleep(seconds: 1)
4445

4546
let networkStatusReceived = expectation(description: "networkStatus received")
4647
var networkStatusActive = false

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginIntegrationTests/DataStoreObserveQueryTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
476476

477477
let testId = UUID().uuidString
478478
var snapshotCount = 0
479-
let observeQueryReadyForTest = asyncExpectation(description: "observeQuery initial query completed")
480-
let allSnapshotsReceived = asyncExpectation(description: "query snapshots received")
479+
let observeQueryReadyForTest = expectation(description: "observeQuery initial query completed")
480+
let allSnapshotsReceived = expectation(description: "query snapshots received")
481481
let sink = Amplify.Publisher.create(Amplify.DataStore.observeQuery(for: Post.self,
482482
where: Post.keys.content == testId,
483483
sort: .ascending(Post.keys.title)))
@@ -495,7 +495,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
495495
switch snapshotCount {
496496
case 1:
497497
XCTAssertEqual(items.count, 0)
498-
Task { await observeQueryReadyForTest.fulfill() }
498+
observeQueryReadyForTest.fulfill()
499499
case 2, 3:
500500
XCTAssertEqual(items.count, 1)
501501
XCTAssertEqual(items[0].title, "a")
@@ -527,14 +527,14 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
527527
XCTAssertEqual(items[0].title, "b")
528528
case 14:
529529
XCTAssertEqual(items.count, 0)
530-
Task { await allSnapshotsReceived.fulfill() }
530+
allSnapshotsReceived.fulfill()
531531
default:
532532
break
533533
}
534534

535535
}
536536

537-
await waitForExpectations([observeQueryReadyForTest], timeout: 10)
537+
await fulfillment(of: [observeQueryReadyForTest], timeout: 10)
538538
// (1) Add several models
539539
let postA = Post(title: "a", content: testId, createdAt: .now())
540540
try await savePostAndWaitForSync(postA)
@@ -559,7 +559,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
559559
try await deletePostAndWaitForSync(postNFromA)
560560
try await deletePostAndWaitForSync(postBFromZ)
561561

562-
await waitForExpectations([allSnapshotsReceived], timeout: 100)
562+
await fulfillment(of: [allSnapshotsReceived], timeout: 100)
563563

564564
sink.cancel()
565565
}

0 commit comments

Comments
 (0)