in AWSAppSyncIntegrationTests/AWSAppSyncMultiAuthClientsTests.swift [58:128]
func testMultiClientSubscriptions() throws {
let testBundle = Bundle(for: AWSAppSyncCognitoAuthTests.self)
// Create User Pools based client. This client waits 999 seconds before returning authorization.
let userPoolsHelper = try AppSyncClientTestHelper(
with: .delayedInvalidOIDC,
testBundle: testBundle
)
let userPoolsAppSyncClient = userPoolsHelper.appSyncClient
// Create IAM based client
let iamHelper = try AppSyncClientTestHelper(
with: .cognitoIdentityPools,
testBundle: testBundle
)
let iamAppSyncClient = iamHelper.appSyncClient
// Create a post to upvote
let postCreated = expectation(description: "Post created successfully.")
let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation
var createdId: GraphQLID?
iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
createdId = result!.data!.createPostWithoutFileUsingParameters!.id
XCTAssertEqual(
result!.data!.createPostWithoutFileUsingParameters?.author,
DefaultTestPostData.author
)
postCreated.fulfill()
})
wait(for: [postCreated], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
// Start a failure subscription that blocks on credentials
let blockingSubscriptionWatcher = try userPoolsAppSyncClient.subscribe(subscription: OnUpvotePostSubscription(id: createdId!)) { (result, transaction, error) in
XCTFail("This client should be blocked until after test exits")
}
// Start listening for events
let upVoteEventTriggered = expectation(description: "Up vote event triggered")
let upVoteEventConnected = expectation(description: "Subscription active")
let watcher = try iamAppSyncClient.subscribe(subscription: OnUpvotePostSubscription(id: createdId!), statusChangeHandler: { status in
if case .connected = status {
upVoteEventConnected.fulfill()
}
}) { (result, transaction, error) in
XCTAssertNil(error)
if (result?.data?.onUpvotePost?.id == createdId) {
upVoteEventTriggered.fulfill()
}
}
_ = [blockingSubscriptionWatcher, watcher] // Silence unused variable warning
// Delay so that subscription setup is complete
wait(for: [upVoteEventConnected], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
// Create an event that should trigger subscription
let upVote = UpvotePostMutation(id: createdId!)
let upVoted = expectation(description: "Post upvoted")
iamAppSyncClient.perform(mutation: upVote, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler: { result, error in
XCTAssertNil(error)
upVoted.fulfill()
})
wait(for: [upVoted], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
wait(for: [upVoteEventTriggered], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
}