瀏覽代碼

Async/await variations of existing RC Swift API tests (#8825)

Paul Beusterien 4 年之前
父節點
當前提交
fd4572c291

+ 10 - 10
FirebaseRemoteConfig.podspec

@@ -82,11 +82,11 @@ app update.
   # Run Swift API tests on a real backend.
   s.test_spec 'swift-api-tests' do |swift_api|
     swift_api.scheme = { :code_coverage => true }
-    swift_api.platforms = {
-      :ios => ios_deployment_target,
-      :osx => osx_deployment_target,
-      :tvos => tvos_deployment_target
-    }
+
+    # Use recent platform versions to enable async await testing
+    swift_api.ios.deployment_target = "15.0"
+    swift_api.osx.deployment_target = "12.0"
+    swift_api.tvos.deployment_target = "15.0"
     swift_api.source_files = 'FirebaseRemoteConfig/Tests/SwiftAPI/*.swift',
                              'FirebaseRemoteConfig/Tests/FakeUtils/*.[hm]',
                              'FirebaseRemoteConfig/Tests/FakeUtils/*.swift'
@@ -100,11 +100,11 @@ app update.
   # Run Swift API tests and tests requiring console changes on a Fake Console.
   s.test_spec 'fake-console-tests' do |fake_console|
     fake_console.scheme = { :code_coverage => true }
-    fake_console.platforms = {
-      :ios => ios_deployment_target,
-      :osx => osx_deployment_target,
-      :tvos => tvos_deployment_target
-    }
+
+    # Use recent platform versions to enable async await testing
+    fake_console.ios.deployment_target = "15.0"
+    fake_console.osx.deployment_target = "12.0"
+    fake_console.tvos.deployment_target = "15.0"
     fake_console.source_files = 'FirebaseRemoteConfig/Tests/SwiftAPI/*.swift',
                                       'FirebaseRemoteConfig/Tests/FakeUtils/*.[hm]',
                                       'FirebaseRemoteConfig/Tests/FakeUtils/*.swift',

+ 2 - 2
FirebaseRemoteConfig/Tests/SwiftAPI/APITests.swift

@@ -283,8 +283,8 @@ class APITests: APITestBase {
   // MARK: - Private Helpers
 
   private func waitForExpectations() {
-    let kFIRStorageIntegrationTestTimeout = 10.0
-    waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
+    let kTestTimeout = 10.0
+    waitForExpectations(timeout: kTestTimeout,
                         handler: { (error) -> Void in
                           if let error = error {
                             print(error)

+ 158 - 0
FirebaseRemoteConfig/Tests/SwiftAPI/AsyncAwaitTests.swift

@@ -0,0 +1,158 @@
+// Copyright 2021 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import FirebaseCore
+@testable import FirebaseRemoteConfig
+
+import XCTest
+
+/// String constants used for testing.
+private enum Constants {
+  static let key1 = "Key1"
+  static let jedi = "Jedi"
+  static let sith = "Sith_Lord"
+  static let value1 = "Value1"
+  static let obiwan = "Obi-Wan"
+  static let yoda = "Yoda"
+  static let darthSidious = "Darth Sidious"
+}
+
+class AsyncAwaitTests: APITestBase {
+  var console: RemoteConfigConsole!
+
+  override func setUp() {
+    super.setUp()
+    if APITests.useFakeConfig {
+      fakeConsole.config = [Constants.key1: Constants.value1]
+    } else {
+      console = RemoteConfigConsole()
+      console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
+    }
+  }
+
+  override func tearDown() {
+    super.tearDown()
+
+    // If using RemoteConfigConsole, reset remote config values.
+    if !APITests.useFakeConfig {
+      console.removeRemoteConfigValue(forKey: Constants.sith)
+      console.removeRemoteConfigValue(forKey: Constants.jedi)
+    }
+  }
+
+  func testFetchThenActivate() async throws {
+    let status = try await config.fetch()
+    XCTAssertEqual(status, RemoteConfigFetchStatus.success)
+    let success = try await config.activate()
+    XCTAssertTrue(success)
+  }
+
+  func testFetchWithExpirationThenActivate() async throws {
+    let status = try await config.fetch(withExpirationDuration: 0)
+    XCTAssertEqual(status, RemoteConfigFetchStatus.success)
+    _ = try await config.activate()
+    XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
+  }
+
+  func testFetchAndActivate() async throws {
+    let status = try await config.fetchAndActivate()
+    XCTAssertEqual(status, .successFetchedFromRemote)
+    XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
+  }
+
+  // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
+  func testUnchangedActivateWillFlag() async throws {
+    let status = try await config.fetch()
+    XCTAssertEqual(status, RemoteConfigFetchStatus.success)
+    let changed = try await config.activate()
+    XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
+    XCTAssertTrue(!APITests.useFakeConfig || changed)
+    XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
+  }
+
+  func testFetchAndActivateUnchangedConfig() async throws {
+    guard APITests.useFakeConfig == false else { return }
+
+    XCTAssertEqual(config.settings.minimumFetchInterval, 0)
+
+    // Represents pre-fetch occurring sometime in past.
+    let status = try await config.fetch()
+    XCTAssertEqual(status, .success)
+
+    // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
+    let status2 = try await config.fetchAndActivate()
+    // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
+    // The behavior of the next test changed in Firebase 7.0.0.
+    // It's an open question which is correct, but it should only
+    // be changed in a major release.
+    // See https://github.com/firebase/firebase-ios-sdk/pull/8788
+    // XCTAssertEqual(status, .successUsingPreFetchedData)
+    XCTAssertEqual(status2, .successFetchedFromRemote)
+    // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
+    if let lastFetchTime = try? XCTUnwrap(config.lastFetchTime) {
+      XCTAssertLessThanOrEqual(Double(config.settings.lastETagUpdateTime),
+                               Double(lastFetchTime.timeIntervalSince1970))
+    } else {
+      XCTFail("Could not unwrap lastFetchTime.")
+    }
+  }
+
+  // MARK: - RemoteConfigConsole Tests
+
+  func testFetchConfigThenUpdateConsoleThenFetchAgain() async throws {
+    guard APITests.useFakeConfig == false else { return }
+
+    _ = try await config.fetchAndActivate()
+    let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
+    XCTAssertEqual(configValue, Constants.obiwan)
+
+    // Synchronously update the console.
+    console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
+
+    _ = try await config.fetchAndActivate()
+    let configValue2 = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
+    XCTAssertEqual(configValue2, Constants.yoda)
+  }
+
+  func testFetchConfigThenAddValueOnConsoleThenFetchAgain() async throws {
+    guard APITests.useFakeConfig == false else { return }
+
+    // Ensure no Sith Lord has been written to Remote Config yet.
+    _ = try await config.fetchAndActivate()
+    XCTAssertTrue(config.configValue(forKey: Constants.sith).dataValue.isEmpty)
+
+    // Synchronously update the console
+    console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
+
+    // Verify the Sith Lord can now be fetched from Remote Config
+    _ = try await config.fetchAndActivate()
+    let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.sith).stringValue)
+    XCTAssertEqual(configValue, Constants.darthSidious)
+  }
+
+  func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() async throws {
+    guard APITests.useFakeConfig == false else { return }
+
+    _ = try await config.fetchAndActivate()
+    let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
+    XCTAssertEqual(configValue, Constants.obiwan)
+
+    // Synchronously delete value on the console.
+    console.removeRemoteConfigValue(forKey: Constants.jedi)
+
+    _ = try await config.fetchAndActivate()
+    XCTAssertTrue(config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
+                  "Remote config should have been deleted.")
+  }
+}