Skip to content

Commit cfee3f0

Browse files
committed
Merge branch 'pr-256'
2 parents ebbbe82 + 816dca6 commit cfee3f0

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ pollution for whatever incomplete code that was running on the main thread.
387387
Blocking the main thread can be caused by blocking IO, calls to sleep(),
388388
deadlocks, and synchronous IPC.
389389
390+
In some cases (e.g. when running on slower machines) it can be useful to modify
391+
the default timeout and poll interval values. This can be done as follows:
392+
393+
```swift
394+
// Swift
395+
396+
// Increase the global timeout to 5 seconds:
397+
Nimble.Defaults.AsyncTimeout = 5
398+
399+
// Slow the polling interval to 0.1 seconds:
400+
Nimble.Defaults.AsyncPollInterval = 0.1
401+
```
402+
390403
## Objective-C Support
391404

392405
Nimble has full support for Objective-C. However, there are two things

Sources/Nimble/Wrappers/AsyncMatcherWrapper.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import Foundation
22

33
#if _runtime(_ObjC)
44

5+
public struct AsyncDefaults {
6+
public static var Timeout: NSTimeInterval = 1
7+
public static var PollInterval: NSTimeInterval = 0.01
8+
}
9+
510
internal struct AsyncMatcherWrapper<T, U where U: Matcher, U.ValueType == T>: Matcher {
611
let fullMatcher: U
712
let timeoutInterval: NSTimeInterval
813
let pollInterval: NSTimeInterval
914

10-
init(fullMatcher: U, timeoutInterval: NSTimeInterval = 1, pollInterval: NSTimeInterval = 0.01) {
15+
init(fullMatcher: U, timeoutInterval: NSTimeInterval = AsyncDefaults.Timeout, pollInterval: NSTimeInterval = AsyncDefaults.PollInterval) {
1116
self.fullMatcher = fullMatcher
1217
self.timeoutInterval = timeoutInterval
1318
self.pollInterval = pollInterval
@@ -79,7 +84,7 @@ extension Expectation {
7984
/// @discussion
8085
/// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function
8186
/// is executing. Any attempts to touch the run loop may cause non-deterministic behavior.
82-
public func toEventually<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = 1, pollInterval: NSTimeInterval = 0.01, description: String? = nil) {
87+
public func toEventually<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = AsyncDefaults.Timeout, pollInterval: NSTimeInterval = AsyncDefaults.PollInterval, description: String? = nil) {
8388
if expression.isClosure {
8489
let (pass, msg) = expressionMatches(
8590
expression,
@@ -102,7 +107,7 @@ extension Expectation {
102107
/// @discussion
103108
/// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function
104109
/// is executing. Any attempts to touch the run loop may cause non-deterministic behavior.
105-
public func toEventuallyNot<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = 1, pollInterval: NSTimeInterval = 0.01, description: String? = nil) {
110+
public func toEventuallyNot<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = AsyncDefaults.Timeout, pollInterval: NSTimeInterval = AsyncDefaults.PollInterval, description: String? = nil) {
106111
if expression.isClosure {
107112
let (pass, msg) = expressionDoesNotMatch(
108113
expression,
@@ -127,7 +132,7 @@ extension Expectation {
127132
/// @discussion
128133
/// This function manages the main run loop (`NSRunLoop.mainRunLoop()`) while this function
129134
/// is executing. Any attempts to touch the run loop may cause non-deterministic behavior.
130-
public func toNotEventually<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = 1, pollInterval: NSTimeInterval = 0.01, description: String? = nil) {
135+
public func toNotEventually<U where U: Matcher, U.ValueType == T>(matcher: U, timeout: NSTimeInterval = AsyncDefaults.Timeout, pollInterval: NSTimeInterval = AsyncDefaults.PollInterval, description: String? = nil) {
131136
return toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description)
132137
}
133138
}

Tests/Nimble/AsynchronousTest.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AsyncTest: XCTestCase, XCTestCaseProvider {
1212
("testToEventuallyPositiveMatches", testToEventuallyPositiveMatches),
1313
("testToEventuallyNegativeMatches", testToEventuallyNegativeMatches),
1414
("testWaitUntilPositiveMatches", testWaitUntilPositiveMatches),
15+
("testToEventuallyWithCustomDefaultTimeout", testToEventuallyWithCustomDefaultTimeout),
1516
("testWaitUntilTimesOutIfNotCalled", testWaitUntilTimesOutIfNotCalled),
1617
("testWaitUntilTimesOutWhenExceedingItsTime", testWaitUntilTimesOutWhenExceedingItsTime),
1718
("testWaitUntilNegativeMatches", testWaitUntilNegativeMatches),
@@ -54,6 +55,27 @@ class AsyncTest: XCTestCase, XCTestCaseProvider {
5455
}
5556
}
5657

58+
func testToEventuallyWithCustomDefaultTimeout() {
59+
AsyncDefaults.Timeout = 2
60+
defer {
61+
AsyncDefaults.Timeout = 1
62+
}
63+
64+
var value = 0
65+
66+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
67+
NSThread.sleepForTimeInterval(1.1)
68+
value = 1
69+
}
70+
expect { value }.toEventually(equal(1))
71+
72+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
73+
NSThread.sleepForTimeInterval(1.1)
74+
value = 0
75+
}
76+
expect { value }.toEventuallyNot(equal(1))
77+
}
78+
5779
func testWaitUntilPositiveMatches() {
5880
waitUntil { done in
5981
done()

0 commit comments

Comments
 (0)