AsyncAwaitTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. class AsyncAwaitTests: APITestBase {
  28. var console: RemoteConfigConsole!
  29. override func setUp() {
  30. super.setUp()
  31. if APITests.useFakeConfig {
  32. fakeConsole.config = [Constants.key1: Constants.value1]
  33. } else {
  34. console = RemoteConfigConsole()
  35. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  36. }
  37. }
  38. override func tearDown() {
  39. super.tearDown()
  40. // If using RemoteConfigConsole, reset remote config values.
  41. if !APITests.useFakeConfig {
  42. console.removeRemoteConfigValue(forKey: Constants.sith)
  43. console.removeRemoteConfigValue(forKey: Constants.jedi)
  44. }
  45. }
  46. func testFetchThenActivate() async throws {
  47. let status = try await config.fetch()
  48. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  49. let success = try await config.activate()
  50. XCTAssertTrue(success)
  51. }
  52. func testFetchWithExpirationThenActivate() async throws {
  53. let status = try await config.fetch(withExpirationDuration: 0)
  54. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  55. _ = try await config.activate()
  56. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  57. }
  58. func testFetchAndActivate() async throws {
  59. let status = try await config.fetchAndActivate()
  60. XCTAssertEqual(status, .successFetchedFromRemote)
  61. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  62. }
  63. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  64. func testUnchangedActivateWillFlag() async throws {
  65. let status = try await config.fetch()
  66. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  67. let changed = try await config.activate()
  68. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  69. XCTAssertTrue(!APITests.useFakeConfig || changed)
  70. XCTAssertEqual(config[Constants.key1].stringValue, Constants.value1)
  71. }
  72. func testFetchAndActivateUnchangedConfig() async throws {
  73. guard APITests.useFakeConfig == false else { return }
  74. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  75. // Represents pre-fetch occurring sometime in past.
  76. let status = try await config.fetch()
  77. XCTAssertEqual(status, .success)
  78. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  79. let status2 = try await config.fetchAndActivate()
  80. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  81. // The behavior of the next test changed in Firebase 7.0.0.
  82. // It's an open question which is correct, but it should only
  83. // be changed in a major release.
  84. // See https://github.com/firebase/firebase-ios-sdk/pull/8788
  85. // XCTAssertEqual(status, .successUsingPreFetchedData)
  86. XCTAssertEqual(status2, .successFetchedFromRemote)
  87. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  88. if let lastFetchTime = try? XCTUnwrap(config.lastFetchTime) {
  89. XCTAssertLessThanOrEqual(Double(config.settings.lastETagUpdateTime),
  90. Double(lastFetchTime.timeIntervalSince1970))
  91. } else {
  92. XCTFail("Could not unwrap lastFetchTime.")
  93. }
  94. }
  95. // MARK: - RemoteConfigConsole Tests
  96. func testFetchConfigThenUpdateConsoleThenFetchAgain() async throws {
  97. guard APITests.useFakeConfig == false else { return }
  98. _ = try await config.fetchAndActivate()
  99. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  100. XCTAssertEqual(configValue, Constants.obiwan)
  101. // Synchronously update the console.
  102. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  103. _ = try await config.fetchAndActivate()
  104. let configValue2 = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  105. XCTAssertEqual(configValue2, Constants.yoda)
  106. }
  107. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() async throws {
  108. guard APITests.useFakeConfig == false else { return }
  109. // Ensure no Sith Lord has been written to Remote Config yet.
  110. _ = try await config.fetchAndActivate()
  111. XCTAssertTrue(config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  112. // Synchronously update the console
  113. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  114. // Verify the Sith Lord can now be fetched from Remote Config
  115. _ = try await config.fetchAndActivate()
  116. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.sith).stringValue)
  117. XCTAssertEqual(configValue, Constants.darthSidious)
  118. }
  119. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() async throws {
  120. guard APITests.useFakeConfig == false else { return }
  121. _ = try await config.fetchAndActivate()
  122. let configValue = try? XCTUnwrap(config.configValue(forKey: Constants.jedi).stringValue)
  123. XCTAssertEqual(configValue, Constants.obiwan)
  124. // Synchronously delete value on the console.
  125. console.removeRemoteConfigValue(forKey: Constants.jedi)
  126. _ = try await config.fetchAndActivate()
  127. XCTAssertTrue(config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  128. "Remote config should have been deleted.")
  129. }
  130. }