func doSubscriptionResultHandlerTesting()

in AWSAppSyncIntegrationTests/SubscriptionTests.swift [303:386]


    func doSubscriptionResultHandlerTesting(withCacheConfiguration cacheConfiguration: AWSAppSyncCacheConfiguration?) throws {
        let appSyncClient = try SubscriptionTests.makeAppSyncClient(authType: self.authType, cacheConfiguration: cacheConfiguration)

        // OnDeltaPostSubscription requires no knowledge of prior state, so we can use it to test operations on an
        // empty cache
        let subscriptionWatcherTriggered = expectation(description: "Subscription watcher was triggered")
        // We don't care if this gets triggered multiple times
        subscriptionWatcherTriggered.assertForOverFulfill = false

        let subscriptionIsActive = expectation(description: "Subscription should be active")
        let statusChangeHandler: SubscriptionStatusChangeHandler = { status in
            if case .connected = status {
                subscriptionIsActive.fulfill()
            }
        }

        let subscription = try appSyncClient.subscribe(subscription: OnDeltaPostSubscription(),
                                                       queue: SubscriptionTests.subscriptionAndFetchQueue,
                                                       statusChangeHandler: statusChangeHandler) { result, transaction, error in
            defer {
                subscriptionWatcherTriggered.fulfill()
            }

            guard let transaction = transaction else {
                XCTFail("Transaction unexpectedly nil in subscription watcher")
                return
            }

            guard error == nil else {
                XCTFail("Unexpected error in subscription watcher: \(error!.localizedDescription)")
                return
            }

            guard
                let result = result,
                let onDeltaPostGraphQLResult = result.data?.onDeltaPost
                else {
                    XCTFail("Result onDeltaPost unexpectedly empty in subscription watcher")
                    return
            }

            let newPost = ListPostsQuery.Data.ListPost(id: onDeltaPostGraphQLResult.id,
                                                       author: onDeltaPostGraphQLResult.author,
                                                       title: onDeltaPostGraphQLResult.title,
                                                       content: onDeltaPostGraphQLResult.content,
                                                       url: onDeltaPostGraphQLResult.url,
                                                       ups: onDeltaPostGraphQLResult.ups,
                                                       downs: onDeltaPostGraphQLResult.downs,
                                                       file: nil,
                                                       createdDate: onDeltaPostGraphQLResult.createdDate,
                                                       awsDs: onDeltaPostGraphQLResult.awsDs)

            do {
                try transaction.update(query: ListPostsQuery()) { (data: inout ListPostsQuery.Data) in
                    XCTAssertNil(data.listPosts)
                    data.listPosts = [newPost]
                }
            } catch {
                XCTFail("Unexpected error updating local cache in subscription watcher: \(error.localizedDescription)")
                return
            }
        }

        defer {
            subscription?.cancel()
        }

        wait(for: [subscriptionIsActive], timeout: SubscriptionTests.networkOperationTimeout)

        let newPost = CreatePostWithoutFileUsingParametersMutation(author: "Test author",
                                                                   title: "Test Title",
                                                                   content: "Test content",
                                                                   url: "http://www.amazon.com/",
                                                                   ups: 0,
                                                                   downs: 0)

        let newPostCreated = expectation(description: "New post created")
        self.appSyncClient?.perform(mutation: newPost,
                                    queue: SubscriptionTests.mutationQueue) { _, _ in
                                        newPostCreated.fulfill()
        }

        wait(for: [newPostCreated, subscriptionWatcherTriggered], timeout: SubscriptionTests.networkOperationTimeout)
    }