Codebase list golang-gomega / cb1d862
Eventually and Consistently can accept time.Duration Also, the default time periods/polling intervals can be configured at the package level. Onsi Fakhouri 10 years ago
3 changed file(s) with 62 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "errors"
4 "time"
45 . "github.com/onsi/ginkgo"
5 "time"
66 )
77
88 func init() {
122122
123123 Wait uses eventually under the hood and accepts the same timeout/polling intervals that eventually does.
124124 */
125 func (s *Session) Wait(timeout ...float64) *Session {
125 func (s *Session) Wait(timeout ...interface{}) *Session {
126126 Eventually(s, timeout...).Should(Exit())
127127 return s
128128 }
1212 */
1313 package gomega
1414
15 import "time"
15 import (
16 "fmt"
17 "reflect"
18 "time"
19 )
1620
1721 const GOMEGA_VERSION = "0.9"
1822
1923 var globalFailHandler OmegaFailHandler
2024
2125 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
2238
2339 //RegisterFailHandler connects Ginkgo to Gomega. When a matcher fails
2440 //the fail handler passed into RegisterFailHandler is called.
85101 //The assertion is tried periodically until it passes or a timeout occurs.
86102 //
87103 //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.
90108 //
91109 //If Eventually is passed an actual that is a function taking no arguments and returning at least one value,
92110 //then Eventually will call the function periodically and try the matcher against the function's first return value.
114132 //Will pass only if the the returned error is nil and the returned string passes the matcher.
115133 //
116134 //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 {
118136 return EventuallyWithOffset(0, actual, intervals...)
119137 }
120138
121139 //EventuallyWithOffset operates like Eventually but takes an additional
122140 //initial argument to indicate an offset in the call stack. This is useful when building helper
123141 //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
127145 if len(intervals) > 0 {
128 timeoutInterval = time.Duration(intervals[0] * float64(time.Second))
146 timeoutInterval = toDuration(intervals[0])
129147 }
130148 if len(intervals) > 1 {
131 pollingInterval = time.Duration(intervals[1] * float64(time.Second))
149 pollingInterval = toDuration(intervals[1])
132150 }
133151 return newAsyncActual(asyncActualTypeEventually, actual, globalFailHandler, timeoutInterval, pollingInterval, offset)
134152 }
137155 //The assertion is tried periodically and is required to pass for a period of time.
138156 //
139157 //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.
142162 //
143163 //If Consistently is passed an actual that is a function taking no arguments and returning at least one value,
144164 //then Consistently will call the function periodically and try the matcher against the function's first return value.
153173 // Consistently(channel).ShouldNot(Receive())
154174 //
155175 //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 {
157177 return ConsistentlyWithOffset(0, actual, intervals...)
158178 }
159179
160180 //ConsistentlyWithOffset operates like Consistnetly but takes an additional
161181 //initial argument to indicate an offset in the call stack. This is useful when building helper
162182 //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
166186 if len(intervals) > 0 {
167 timeoutInterval = time.Duration(intervals[0] * float64(time.Second))
187 timeoutInterval = toDuration(intervals[0])
168188 }
169189 if len(intervals) > 1 {
170 pollingInterval = time.Duration(intervals[1] * float64(time.Second))
190 pollingInterval = toDuration(intervals[1])
171191 }
172192 return newAsyncActual(asyncActualTypeConsistently, actual, globalFailHandler, timeoutInterval, pollingInterval, offset)
173193 }
222242 NegatedFailureMessage(actual interface{}) (message string)
223243 }
224244
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.
225248 type DeprecatedOmegaMatcher interface {
226249 Match(actual interface{}) (success bool, message string, err error)
227250 }
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 }