AsyncAwaitTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. @testable import FirebaseRemoteConfig
  16. import XCTest
  17. /// String constants used for testing.
  18. private enum Constants {
  19. static let key1 = "Key1"
  20. static let jedi = "Jedi"
  21. static let sith = "Sith_Lord"
  22. static let value1 = "Value1"
  23. static let obiwan = "Obi-Wan"
  24. static let yoda = "Yoda"
  25. static let darthSidious = "Darth Sidious"
  26. }
  27. #if compiler(>=5.5) && canImport(_Concurrency)
  28. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  29. class AsyncAwaitTests: APITestBase {
  30. var console: RemoteConfigConsole!
  31. override func setUp() {
  32. super.setUp()
  33. if APITests.useFakeConfig {
  34. fakeConsole.config = [Constants.key1: Constants.value1]
  35. } else {
  36. console = RemoteConfigConsole()
  37. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  38. }
  39. }
  40. override func tearDown() {
  41. super.tearDown()
  42. // If using RemoteConfigConsole, reset remote config values.
  43. if !APITests.useFakeConfig {
  44. console.removeRemoteConfigValue(forKey: Constants.sith)
  45. console.removeRemoteConfigValue(forKey: Constants.jedi)
  46. }
  47. }
  48. func testFetchThenActivate() async throws {
  49. let status = try await config.fetch()
  50. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  51. let success = try await config.activate()
  52. XCTAssertTrue(success)
  53. }
  54. func testFetchWithExpirationThenActivate() async throws {
  55. let status = try await config.fetch(withExpirationDuration: 0)
  56. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  57. _ = try await config.activate()
  58. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  59. }
  60. func testFetchAndActivate() async throws {
  61. let status = try await config.fetchAndActivate()
  62. XCTAssertEqual(status, .successFetchedFromRemote)
  63. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  64. }
  65. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  66. func testUnchangedActivateWillFlag() async throws {
  67. let status = try await config.fetch()
  68. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  69. let changed = try await config.activate()
  70. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  71. XCTAssertTrue(!APITests.useFakeConfig || changed)
  72. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  73. }
  74. func testFetchAndActivateUnchangedConfig() async throws {
  75. guard APITests.useFakeConfig == false else { return }
  76. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  77. // Represents pre-fetch occurring sometime in past.
  78. let status = try await config.fetch()
  79. XCTAssertEqual(status, .success)
  80. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  81. let status2 = try await config.fetchAndActivate()
  82. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  83. // The behavior of the next test changed in Firebase 7.0.0.
  84. // It's an open question which is correct, but it should only
  85. // be changed in a major release.
  86. // See https://github.com/firebase/firebase-ios-sdk/pull/8788
  87. // XCTAssertEqual(status, .successUsingPreFetchedData)
  88. XCTAssertEqual(status2, .successFetchedFromRemote)
  89. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  90. if let lastFetchTime = try? XCTUnwrap(config.lastFetchTime) {
  91. XCTAssertLessThanOrEqual(Double(config.settings.lastETagUpdateTime),
  92. Double(lastFetchTime.timeIntervalSince1970))
  93. } else {
  94. XCTFail("Could not unwrap lastFetchTime.")
  95. }
  96. }
  97. // MARK: - RemoteConfigConsole Tests
  98. func testFetchConfigThenUpdateConsoleThenFetchAgain() async throws {
  99. guard APITests.useFakeConfig == false else { return }
  100. _ = try await config.fetchAndActivate()
  101. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  102. XCTAssertEqual(configValue, Constants.obiwan)
  103. // Synchronously update the console.
  104. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  105. _ = try await config.fetchAndActivate()
  106. let configValue2 = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  107. XCTAssertEqual(configValue2, Constants.yoda)
  108. }
  109. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() async throws {
  110. guard APITests.useFakeConfig == false else { return }
  111. // Ensure no Sith Lord has been written to Remote Config yet.
  112. _ = try await config.fetchAndActivate()
  113. XCTAssertTrue(config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  114. // Synchronously update the console
  115. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  116. // Verify the Sith Lord can now be fetched from Remote Config
  117. _ = try await config.fetchAndActivate()
  118. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.sith).stringValue)
  119. XCTAssertEqual(configValue, Constants.darthSidious)
  120. }
  121. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() async throws {
  122. guard APITests.useFakeConfig == false else { return }
  123. _ = try await config.fetchAndActivate()
  124. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  125. XCTAssertEqual(configValue, Constants.obiwan)
  126. // Synchronously delete value on the console.
  127. console.removeRemoteConfigValue(forKey: Constants.jedi)
  128. _ = try await config.fetchAndActivate()
  129. XCTAssertTrue(config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  130. "Remote config should have been deleted.")
  131. }
  132. }
  133. #endif