Skip to content

Commit

Permalink
Make waitForExpectation use configurable wait rate
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Oct 20, 2023
1 parent 7a60297 commit 248ab5f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
41 changes: 34 additions & 7 deletions Sources/SpellbookTestUtils/Extensions - XCTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion Tests/SpellbookTests/Common/BlockingQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Tests/SpellbookTests/LowLevel/MachTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
10 changes: 5 additions & 5 deletions Tests/SpellbookTests/Observing/EventAskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 248ab5f

Please sign in to comment.