12 | 12 |
*/
|
13 | 13 |
package gomega
|
14 | 14 |
|
15 | |
import "time"
|
|
15 |
import (
|
|
16 |
"fmt"
|
|
17 |
"reflect"
|
|
18 |
"time"
|
|
19 |
)
|
16 | 20 |
|
17 | 21 |
const GOMEGA_VERSION = "0.9"
|
18 | 22 |
|
19 | 23 |
var globalFailHandler OmegaFailHandler
|
20 | 24 |
|
21 | 25 |
type OmegaFailHandler func(message string, callerSkip ...int)
|
|
26 |
|
|
27 |
//The default timeout duration for Eventually. Eventually will repeatedly poll your condition until it succeeds, or until this timeout elapses.
|
|
28 |
var DefaultEventuallyTimeout = time.Second
|
|
29 |
|
|
30 |
//The default polling interval for Eventually.
|
|
31 |
var DefaultEventuallyPollingInterval = 10 * time.Millisecond
|
|
32 |
|
|
33 |
//The default duration for Consistently. Consistently will verify that your condition is satsified for this long.
|
|
34 |
var DefaultConsistentlyDuration = 100 * time.Millisecond
|
|
35 |
|
|
36 |
//The default polling interval for Consistently.
|
|
37 |
var DefaultConsistentlyPollingInterval = 10 * time.Millisecond
|
22 | 38 |
|
23 | 39 |
//RegisterFailHandler connects Ginkgo to Gomega. When a matcher fails
|
24 | 40 |
//the fail handler passed into RegisterFailHandler is called.
|
|
85 | 101 |
//The assertion is tried periodically until it passes or a timeout occurs.
|
86 | 102 |
//
|
87 | 103 |
//Both the timeout and polling interval are configurable as optional arguments:
|
88 | |
//The first optional argument is the timeout in seconds expressed as a float64.
|
89 | |
//The second optional argument is the polling interval in seconds expressd as a float64.
|
|
104 |
//The first optional argument is the timeout
|
|
105 |
//The second optional argument is the polling interval
|
|
106 |
//
|
|
107 |
//Both intervals can either be specified as time.Duration or as floats/integers. In the latter case they are interpreted as seconds.
|
90 | 108 |
//
|
91 | 109 |
//If Eventually is passed an actual that is a function taking no arguments and returning at least one value,
|
92 | 110 |
//then Eventually will call the function periodically and try the matcher against the function's first return value.
|
|
114 | 132 |
//Will pass only if the the returned error is nil and the returned string passes the matcher.
|
115 | 133 |
//
|
116 | 134 |
//Eventually's default timeout is 1 second, and its default polling interval is 10ms
|
117 | |
func Eventually(actual interface{}, intervals ...float64) AsyncActual {
|
|
135 |
func Eventually(actual interface{}, intervals ...interface{}) AsyncActual {
|
118 | 136 |
return EventuallyWithOffset(0, actual, intervals...)
|
119 | 137 |
}
|
120 | 138 |
|
121 | 139 |
//EventuallyWithOffset operates like Eventually but takes an additional
|
122 | 140 |
//initial argument to indicate an offset in the call stack. This is useful when building helper
|
123 | 141 |
//functions that contain matchers. To learn more, read about `ExpectWithOffset`.
|
124 | |
func EventuallyWithOffset(offset int, actual interface{}, intervals ...float64) AsyncActual {
|
125 | |
timeoutInterval := time.Duration(1 * time.Second)
|
126 | |
pollingInterval := time.Duration(10 * time.Millisecond)
|
|
142 |
func EventuallyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncActual {
|
|
143 |
timeoutInterval := DefaultEventuallyTimeout
|
|
144 |
pollingInterval := DefaultEventuallyPollingInterval
|
127 | 145 |
if len(intervals) > 0 {
|
128 | |
timeoutInterval = time.Duration(intervals[0] * float64(time.Second))
|
|
146 |
timeoutInterval = toDuration(intervals[0])
|
129 | 147 |
}
|
130 | 148 |
if len(intervals) > 1 {
|
131 | |
pollingInterval = time.Duration(intervals[1] * float64(time.Second))
|
|
149 |
pollingInterval = toDuration(intervals[1])
|
132 | 150 |
}
|
133 | 151 |
return newAsyncActual(asyncActualTypeEventually, actual, globalFailHandler, timeoutInterval, pollingInterval, offset)
|
134 | 152 |
}
|
|
137 | 155 |
//The assertion is tried periodically and is required to pass for a period of time.
|
138 | 156 |
//
|
139 | 157 |
//Both the total time and polling interval are configurable as optional arguments:
|
140 | |
//The first optional argument is the duration, in seconds expressed as a float64, that Consistently will run for.
|
141 | |
//The second optional argument is the polling interval in seconds expressd as a float64.
|
|
158 |
//The first optional argument is the duration that Consistently will run for
|
|
159 |
//The second optional argument is the polling interval
|
|
160 |
//
|
|
161 |
//Both intervals can either be specified as time.Duration or as floats/integers. In the latter case they are interpreted as seconds.
|
142 | 162 |
//
|
143 | 163 |
//If Consistently is passed an actual that is a function taking no arguments and returning at least one value,
|
144 | 164 |
//then Consistently will call the function periodically and try the matcher against the function's first return value.
|
|
153 | 173 |
// Consistently(channel).ShouldNot(Receive())
|
154 | 174 |
//
|
155 | 175 |
//Consistently's default duration is 100ms, and its default polling interval is 10ms
|
156 | |
func Consistently(actual interface{}, intervals ...float64) AsyncActual {
|
|
176 |
func Consistently(actual interface{}, intervals ...interface{}) AsyncActual {
|
157 | 177 |
return ConsistentlyWithOffset(0, actual, intervals...)
|
158 | 178 |
}
|
159 | 179 |
|
160 | 180 |
//ConsistentlyWithOffset operates like Consistnetly but takes an additional
|
161 | 181 |
//initial argument to indicate an offset in the call stack. This is useful when building helper
|
162 | 182 |
//functions that contain matchers. To learn more, read about `ExpectWithOffset`.
|
163 | |
func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...float64) AsyncActual {
|
164 | |
timeoutInterval := time.Duration(100 * time.Millisecond)
|
165 | |
pollingInterval := time.Duration(10 * time.Millisecond)
|
|
183 |
func ConsistentlyWithOffset(offset int, actual interface{}, intervals ...interface{}) AsyncActual {
|
|
184 |
timeoutInterval := DefaultConsistentlyDuration
|
|
185 |
pollingInterval := DefaultConsistentlyPollingInterval
|
166 | 186 |
if len(intervals) > 0 {
|
167 | |
timeoutInterval = time.Duration(intervals[0] * float64(time.Second))
|
|
187 |
timeoutInterval = toDuration(intervals[0])
|
168 | 188 |
}
|
169 | 189 |
if len(intervals) > 1 {
|
170 | |
pollingInterval = time.Duration(intervals[1] * float64(time.Second))
|
|
190 |
pollingInterval = toDuration(intervals[1])
|
171 | 191 |
}
|
172 | 192 |
return newAsyncActual(asyncActualTypeConsistently, actual, globalFailHandler, timeoutInterval, pollingInterval, offset)
|
173 | 193 |
}
|
|
222 | 242 |
NegatedFailureMessage(actual interface{}) (message string)
|
223 | 243 |
}
|
224 | 244 |
|
|
245 |
//The DeprecatedOmegaMatcher interface is... deprecated. It will be removed in v1.0
|
|
246 |
//
|
|
247 |
//The new OmegaMatcher interface allows for lazy failure message evaluation.
|
225 | 248 |
type DeprecatedOmegaMatcher interface {
|
226 | 249 |
Match(actual interface{}) (success bool, message string, err error)
|
227 | 250 |
}
|
|
251 |
|
|
252 |
func toDuration(input interface{}) time.Duration {
|
|
253 |
duration, ok := input.(time.Duration)
|
|
254 |
if ok {
|
|
255 |
return duration
|
|
256 |
}
|
|
257 |
|
|
258 |
value := reflect.ValueOf(input)
|
|
259 |
kind := reflect.TypeOf(input).Kind()
|
|
260 |
|
|
261 |
if reflect.Int <= kind && kind <= reflect.Int64 {
|
|
262 |
return time.Duration(value.Int()) * time.Second
|
|
263 |
} else if reflect.Uint <= kind && kind <= reflect.Uint64 {
|
|
264 |
return time.Duration(value.Uint()) * time.Second
|
|
265 |
} else if reflect.Float32 <= kind && kind <= reflect.Float64 {
|
|
266 |
return time.Duration(value.Float() * float64(time.Second))
|
|
267 |
}
|
|
268 |
|
|
269 |
panic(fmt.Sprintf("%v is not a valid interval. Must be time.Duration or a number.", input))
|
|
270 |
}
|