AuthSerialTaskQueueTests.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2023 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 Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class SerialTaskQueueTests: XCTestCase {
  19. func testExecution() {
  20. let expectation = self.expectation(description: #function)
  21. let queue = AuthSerialTaskQueue()
  22. queue.enqueueTask { completionArg in
  23. completionArg()
  24. expectation.fulfill()
  25. }
  26. waitForExpectations(timeout: 5)
  27. }
  28. func testCompletion() {
  29. let expectation = self.expectation(description: #function)
  30. let queue = AuthSerialTaskQueue()
  31. var completion: (() -> Void)?
  32. queue.enqueueTask { completionArg in
  33. completion = completionArg
  34. expectation.fulfill()
  35. }
  36. var executed = false
  37. var nextExpectation: XCTestExpectation?
  38. queue.enqueueTask { completionArg in
  39. executed = true
  40. completionArg()
  41. nextExpectation?.fulfill()
  42. }
  43. // The second task should not be executed until the first is completed.
  44. waitForExpectations(timeout: 5)
  45. XCTAssertNotNil(completion)
  46. XCTAssertFalse(executed)
  47. nextExpectation = self.expectation(description: "next")
  48. completion?()
  49. waitForExpectations(timeout: 5)
  50. XCTAssertTrue(executed)
  51. }
  52. func testTargetQueue() {
  53. let expectation = self.expectation(description: #function)
  54. let queue = AuthSerialTaskQueue()
  55. var executed = false
  56. kAuthGlobalWorkQueue.suspend()
  57. queue.enqueueTask { completionArg in
  58. executed = true
  59. completionArg()
  60. expectation.fulfill()
  61. }
  62. // The task should not executed until the global work queue is resumed.
  63. sleep(1)
  64. XCTAssertFalse(executed)
  65. kAuthGlobalWorkQueue.resume()
  66. waitForExpectations(timeout: 5)
  67. XCTAssertTrue(executed)
  68. }
  69. func testTaskQueueNoAffectTargetQueue() {
  70. let queue = AuthSerialTaskQueue()
  71. var completion: (() -> Void)?
  72. queue.enqueueTask { completionArg in
  73. completion = completionArg
  74. }
  75. var executed = false
  76. var nextExpectation: XCTestExpectation?
  77. queue.enqueueTask { completionArg in
  78. executed = true
  79. completionArg()
  80. nextExpectation?.fulfill()
  81. }
  82. let expectation = self.expectation(description: #function)
  83. kAuthGlobalWorkQueue.async {
  84. expectation.fulfill()
  85. }
  86. // The task queue waiting for completion should not affect the global work queue.
  87. waitForExpectations(timeout: 5)
  88. XCTAssertNotNil(completion)
  89. XCTAssertFalse(executed)
  90. nextExpectation = self.expectation(description: "next")
  91. completion?()
  92. waitForExpectations(timeout: 555)
  93. XCTAssertTrue(executed)
  94. }
  95. }