func testDeltaSyncMetadataClear()

in AWSAppSyncIntegrationTests/AWSAppSyncMultiAuthClientsTests.swift [235:313]


    func testDeltaSyncMetadataClear() throws {
        let testBundle = Bundle(for: AWSAppSyncCognitoAuthTests.self)

        // This tests needs a physical DB for the SubscriptionMetadataCache to properly return a "lastSynced" value.
        let rootDirectory = FileManager.default.temporaryDirectory.appendingPathComponent("testDeltaSyncMetadataClear")
        try? FileManager.default.removeItem(at: rootDirectory)
        let cacheConfiguration = try AWSAppSyncCacheConfiguration(withRootDirectory: rootDirectory)

        // Create IAM based client
        let iamHelper = try AppSyncClientTestHelper(
            with: .cognitoIdentityPools,
            cacheConfiguration: cacheConfiguration,
            testBundle: testBundle
        )
        let iamAppSyncClient = iamHelper.appSyncClient
        _ = try iamAppSyncClient.clearCaches()

        let listPostQuery = ListPostsQuery()
        let listPostDeltaQuery = ListPostsDeltaQuery()
        let queryCallbackExpect = expectation(description: "Query callback")

        // Need to take a strong references to sync return
        var watchers: [Cancellable] = []

        let phase1Watcher = iamAppSyncClient.sync(baseQuery: listPostQuery
        , baseQueryResultHandler: { (result, error) in
            if let _ = result,
                result!.source == .server {
                queryCallbackExpect.fulfill()
            }
        }, deltaQuery: listPostDeltaQuery, deltaQueryResultHandler: { (result, transaction, error) in
            XCTFail("Not expecting a delta query result")
        })
        watchers.append(phase1Watcher)

        wait(for: [queryCallbackExpect], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        sleep(1) // Database has eventually consistent read on the lastSyncTime

        // Phase 2

        let deltaQueryCallbackExpect = expectation(description: "Delta query callback")

        // Need to take a strong reference
        let phase2Watcher = iamAppSyncClient.sync(baseQuery: listPostQuery
        , baseQueryResultHandler: { (result, error) in
            if let result = result,
                result.source == .server {
                XCTFail("Not expecting a base query result")
            }
        }, deltaQuery: listPostDeltaQuery, deltaQueryResultHandler: { (result, transaction, error) in
            if let _ = result {
                deltaQueryCallbackExpect.fulfill()
            }
        })
        watchers.append(phase2Watcher)

        wait(for: [deltaQueryCallbackExpect], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        // Reset

        let _ = try iamAppSyncClient.clearCaches()

        let queryCallbackExpect2 = expectation(description: "Query callback 2")

        // Need to take a strong reference
        let resetWatcher = iamAppSyncClient.sync(baseQuery: listPostQuery
        , baseQueryResultHandler: { (result, error) in
            if let result = result,
                result.source == .server {
                queryCallbackExpect2.fulfill()
            }
        }, deltaQuery: listPostDeltaQuery, deltaQueryResultHandler: { (result, transaction, error) in
            XCTFail("Not expecting a delta query result 2")
        })
        watchers.append(resetWatcher)

        wait(for: [queryCallbackExpect2], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
    }