Skip to content

Commit

Permalink
fix(datastore): update assertion usage in datastore integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian committed Oct 13, 2023
1 parent dcf5789 commit 687a3bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DataStoreHubEventTests: HubEventsIntegrationTestBase {
try await Task.sleep(seconds: 1)

let networkStatusReceived = expectation(description: "networkStatus received")
networkStatusReceived.assertForOverFulfill = false
var networkStatusActive = false
let subscriptionsEstablishedReceived = expectation(description: "subscriptionsEstablished received")
let syncQueriesStartedReceived = expectation(description: "syncQueriesStarted received")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
let receivedPost = expectation(description: "received Post")
try await savePostAndWaitForSync(Post(title: "title", content: "content", createdAt: .now()),
postSyncedExpctation: receivedPost)
await fulfillment(of: [snapshotWithIsSynced, receivedPost], timeout: 100)
await fulfillment(of: [snapshotWithIsSynced], timeout: 100)
task.cancel()
await fulfillment(of: [querySnapshotsCancelled], timeout: 10)
}
Expand Down Expand Up @@ -185,6 +185,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
let snapshotWithIsSynced = expectation(description: "query snapshot with isSynced true")
var snapshotWithIsSyncedFulfilled = false
let receivedPostFromObserveQuery = expectation(description: "received Post")
receivedPostFromObserveQuery.assertForOverFulfill = false
let post4 = Post(title: "\(randomTitle) 4", content: "content", createdAt: .now())
let predicate = Post.keys.title.beginsWith(randomTitle)
Amplify.Publisher.create(Amplify.DataStore.observeQuery(for: Post.self, where: predicate)).sink { completed in
Expand Down Expand Up @@ -233,6 +234,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
let post1 = Post(title: "title", content: "content", createdAt: .now())
let post2 = Post(title: "title", content: "content", createdAt: .now().add(value: 1, to: .second))
let snapshotWithSavedPost = expectation(description: "query snapshot with saved post")
snapshotWithSavedPost.assertForOverFulfill = false
Amplify.Publisher.create(Amplify.DataStore.observeQuery(for: Post.self, sort: .ascending(Post.keys.createdAt)))
.sink { completed in
switch completed {
Expand Down Expand Up @@ -339,7 +341,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
try await savePostAndWaitForSync(newPost,
postSyncedExpctation: receivedPost)

await fulfillment(of: [snapshotWithIsSynced, receivedPost], timeout: 30)
await fulfillment(of: [snapshotWithIsSynced], timeout: 30)
XCTAssertTrue(snapshots.count >= 2)
XCTAssertFalse(snapshots[0].isSynced)
XCTAssertEqual(1, snapshots.filter({ $0.isSynced }).count)
Expand Down Expand Up @@ -669,10 +671,13 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
func savePostAndWaitForSync(_ post: Post, postSyncedExpctation: XCTestExpectation? = nil) async throws {
// Wait for a fulfillment count of 2 (first event due to the locally source mutation saved to the local store
// and the second event due to the subscription event received from the remote store)
let receivedPost = postSyncedExpctation ?? expectation(description: "received Post")
if postSyncedExpctation == nil {
receivedPost.expectedFulfillmentCount = 2
}
let receivedPost = postSyncedExpctation ?? {
let e = expectation(description: "received Post")
e.expectedFulfillmentCount = 2
return e
}()
receivedPost.assertForOverFulfill = false

Task {
let mutationEvents = Amplify.DataStore.observe(Post.self)
do {
Expand All @@ -687,18 +692,18 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
}

_ = try await Amplify.DataStore.save(post)
if postSyncedExpctation == nil {
await fulfillment(of: [receivedPost], timeout: 100)
}
await fulfillment(of: [receivedPost], timeout: 100)
}

func deletePostAndWaitForSync(_ post: Post, postSyncedExpctation: XCTestExpectation? = nil) async throws {
// Wait for a fulfillment count of 2 (first event due to the locally source mutation deleted from the local
// store and the second event due to the subscription event received from the remote store)
let deletedPost = postSyncedExpctation ?? expectation(description: "deleted Post")
if postSyncedExpctation == nil {
deletedPost.expectedFulfillmentCount = 2
}
let deletedPost = postSyncedExpctation ?? {
let e = expectation(description: "deleted Post")
e.expectedFulfillmentCount = 2
return e
}()
deletedPost.assertForOverFulfill = false

Task {
let mutationEvents = Amplify.DataStore.observe(Post.self)
Expand All @@ -714,9 +719,7 @@ class DataStoreObserveQueryTests: SyncEngineIntegrationTestBase {
}

_ = try await Amplify.DataStore.delete(post)
if postSyncedExpctation == nil {
await fulfillment(of: [deletedPost], timeout: 100)
}
await fulfillment(of: [deletedPost], timeout: 100)
}

func queryNumberOfPosts() async throws -> Int {
Expand Down

0 comments on commit 687a3bb

Please sign in to comment.