From 248ab5fd8e2fc71131576fe002f26012b2851f9c Mon Sep 17 00:00:00 2001 From: "Alkenso (Vladimir Vashurkin)" Date: Fri, 20 Oct 2023 10:44:45 +0300 Subject: [PATCH] Make waitForExpectation use configurable wait rate --- .../Extensions - XCTestCase.swift | 41 +++++++++++++++---- .../Common/BlockingQueueTests.swift | 2 +- Tests/SpellbookTests/LowLevel/MachTests.swift | 8 ++-- .../Observing/EventAskTests.swift | 10 ++--- .../{ => Other}/LowLevelTests.swift | 0 .../{ => Other}/ObjCTests.swift | 0 .../ConcurrentBlockOperationTests.swift | 6 +-- 7 files changed, 47 insertions(+), 20 deletions(-) rename Tests/SpellbookTests/{ => Other}/LowLevelTests.swift (100%) rename Tests/SpellbookTests/{ => Other}/ObjCTests.swift (100%) diff --git a/Sources/SpellbookTestUtils/Extensions - XCTestCase.swift b/Sources/SpellbookTestUtils/Extensions - XCTestCase.swift index c22dcd2..f9e13b0 100644 --- a/Sources/SpellbookTestUtils/Extensions - XCTestCase.swift +++ b/Sources/SpellbookTestUtils/Extensions - XCTestCase.swift @@ -22,24 +22,51 @@ import XCTest -public extension XCTestCase { - static var waitTimeout: TimeInterval = 0.5 +extension XCTestCase { +#if SPELLBOOK_SLOW_CI_x10 + public static var waitRate = 10.0 +#elseif SPELLBOOK_SLOW_CI_x20 + public static var waitRate = 20.0 +#elseif SPELLBOOK_SLOW_CI_x30 + public static var waitRate = 30.0 +#elseif SPELLBOOK_SLOW_CI_x50 + public static var waitRate = 50.0 +#elseif SPELLBOOK_SLOW_CI_x100 + public static var waitRate = 100.0 +#else + public static var waitRate = 1.0 +#endif - static var testBundle: Bundle { + public static var waitTimeout: TimeInterval = 0.5 + + public static var testBundle: Bundle { return Bundle(for: Self.self) } - var testBundle: Bundle { + public var testBundle: Bundle { Self.testBundle } @discardableResult - func waitForExpectations(timeout: TimeInterval = XCTestCase.waitTimeout) -> Error? { + public func waitForExpectations(timeout: TimeInterval = XCTestCase.waitTimeout) -> Error? { + waitForExpectations(timeout: timeout, ignoreWaitRate: false) + } + + @discardableResult + public func waitForExpectations(timeout: TimeInterval = XCTestCase.waitTimeout, ignoreWaitRate: Bool) -> Error? { var error: Error? - waitForExpectations(timeout: timeout, handler: { + waitForExpectations(timeout: timeout * Self.waitRate) { error = $0 - }) + } return error } + + public static func sleep(interval: TimeInterval) { + Thread.sleep(forTimeInterval: interval * Self.waitRate) + } + + public func sleep(interval: TimeInterval) { + Self.sleep(interval: interval) + } } diff --git a/Tests/SpellbookTests/Common/BlockingQueueTests.swift b/Tests/SpellbookTests/Common/BlockingQueueTests.swift index b9b103a..cf7cc9a 100644 --- a/Tests/SpellbookTests/Common/BlockingQueueTests.swift +++ b/Tests/SpellbookTests/Common/BlockingQueueTests.swift @@ -32,7 +32,7 @@ class BlockingQueueTests: XCTestCase { XCTAssertEqual(queue.dequeue(), 10) dequeueExp.fulfill() } - waitForExpectations(timeout: 0.1) + waitForExpectations(timeout: 0.1, ignoreWaitRate: true) dequeueExp = expectation(description: "dequeued after enqueue") queue.enqueue(10) diff --git a/Tests/SpellbookTests/LowLevel/MachTests.swift b/Tests/SpellbookTests/LowLevel/MachTests.swift index 57f7f5b..14f490e 100644 --- a/Tests/SpellbookTests/LowLevel/MachTests.swift +++ b/Tests/SpellbookTests/LowLevel/MachTests.swift @@ -3,14 +3,14 @@ import XCTest class MachTests: XCTestCase { - private let ticksAccuracy: UInt64 = 200 + private let ticksAccuracy: UInt64 = UInt64(Double(700) * XCTestCase.waitRate) - func test_machTime() { + func test_machTime() throws { let currentMach = mach_absolute_time() - let calculated = Date().machTime! + let calculated = try XCTUnwrap(Date().machTime) XCTAssertLessThanOrEqual(calculated - currentMach, ticksAccuracy) - let currentDate = Date(machTime: mach_absolute_time())! + let currentDate = try XCTUnwrap(Date(machTime: mach_absolute_time())) XCTAssertEqual(currentDate.timeIntervalSince1970, Date().timeIntervalSince1970, accuracy: 0.001) } } diff --git a/Tests/SpellbookTests/Observing/EventAskTests.swift b/Tests/SpellbookTests/Observing/EventAskTests.swift index 4245e90..2ca09c4 100644 --- a/Tests/SpellbookTests/Observing/EventAskTests.swift +++ b/Tests/SpellbookTests/Observing/EventAskTests.swift @@ -76,33 +76,33 @@ final class EventAskTests: XCTestCase { // Set event processor that takes more time than timeout. event.subscribe { _ in - Thread.sleep(forTimeInterval: 0.1) + Self.sleep(interval: 0.1) return 1 }.store(in: &subscriptions) // Set event processor that takes less time than timeout. event.subscribe { _ in - Thread.sleep(forTimeInterval: 0.01) + Self.sleep(interval: 0.01) return 2 }.store(in: &subscriptions) // Assuming let exp1 = expectation(description: "Evaluated.") - event.askAsync("", timeout: .init(0.05, fallback: nil)) { + event.askAsync("", timeout: .init(0.05 * Self.waitRate, fallback: nil)) { XCTAssertEqual(Set($0), [2]) exp1.fulfill() } waitForExpectations() let exp2 = expectation(description: "Evaluated.") - event.askAsync("", timeout: .init(0.05, fallback: .replaceMissed(10))) { + event.askAsync("", timeout: .init(0.05 * Self.waitRate, fallback: .replaceMissed(10))) { XCTAssertEqual(Set($0), [2, 10]) exp2.fulfill() } waitForExpectations() let exp3 = expectation(description: "Evaluated.") - event.askAsync("", timeout: .init(0.05, fallback: .replaceOutput([123]))) { + event.askAsync("", timeout: .init(0.05 * Self.waitRate, fallback: .replaceOutput([123]))) { XCTAssertEqual(Set($0), [123]) exp3.fulfill() } diff --git a/Tests/SpellbookTests/LowLevelTests.swift b/Tests/SpellbookTests/Other/LowLevelTests.swift similarity index 100% rename from Tests/SpellbookTests/LowLevelTests.swift rename to Tests/SpellbookTests/Other/LowLevelTests.swift diff --git a/Tests/SpellbookTests/ObjCTests.swift b/Tests/SpellbookTests/Other/ObjCTests.swift similarity index 100% rename from Tests/SpellbookTests/ObjCTests.swift rename to Tests/SpellbookTests/Other/ObjCTests.swift diff --git a/Tests/SpellbookTests/Threading & Concurrency/ConcurrentBlockOperationTests.swift b/Tests/SpellbookTests/Threading & Concurrency/ConcurrentBlockOperationTests.swift index 80255f3..7fa7ad1 100644 --- a/Tests/SpellbookTests/Threading & Concurrency/ConcurrentBlockOperationTests.swift +++ b/Tests/SpellbookTests/Threading & Concurrency/ConcurrentBlockOperationTests.swift @@ -7,20 +7,20 @@ class ConcurrentBlockOperationTests: XCTestCase { func test() throws { let interval = 0.1 let op = ConcurrentBlockOperation { isCancelled, completion in - Thread.sleep(forTimeInterval: interval) + Self.sleep(interval: interval) completion() } let queue = OperationQueue() queue.addOperation(op) - Thread.sleep(forTimeInterval: 0.05) + Self.sleep(interval: 0.05) XCTAssertTrue(op.isAsynchronous) XCTAssertTrue(op.isReady) XCTAssertTrue(op.isExecuting) XCTAssertFalse(op.isFinished) - Thread.sleep(forTimeInterval: interval) + Self.sleep(interval: interval) XCTAssertFalse(op.isExecuting) XCTAssertTrue(op.isFinished)