forked from HuaweiTech/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_actual.go
182 lines (152 loc) · 4.42 KB
/
async_actual.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gomega
import (
"errors"
"fmt"
"reflect"
"time"
)
type asyncActualType uint
const (
asyncActualTypeEventually asyncActualType = iota
asyncActualTypeConsistently
)
type asyncActual struct {
asyncType asyncActualType
actualInput interface{}
timeoutInterval time.Duration
pollingInterval time.Duration
fail OmegaFailHandler
offset int
}
func newAsyncActual(asyncType asyncActualType, actualInput interface{}, fail OmegaFailHandler, timeoutInterval time.Duration, pollingInterval time.Duration, offset int) *asyncActual {
actualType := reflect.TypeOf(actualInput)
if actualType.Kind() == reflect.Func {
if actualType.NumIn() != 0 || actualType.NumOut() == 0 {
panic("Expected a function with no arguments and one or more return values.")
}
}
return &asyncActual{
asyncType: asyncType,
actualInput: actualInput,
fail: fail,
timeoutInterval: timeoutInterval,
pollingInterval: pollingInterval,
offset: offset,
}
}
func (actual *asyncActual) Should(matcher interface{}, optionalDescription ...interface{}) bool {
return actual.match(shimIfNecessary(matcher), true, optionalDescription...)
}
func (actual *asyncActual) ShouldNot(matcher interface{}, optionalDescription ...interface{}) bool {
return actual.match(shimIfNecessary(matcher), false, optionalDescription...)
}
func (actual *asyncActual) buildDescription(optionalDescription ...interface{}) string {
switch len(optionalDescription) {
case 0:
return ""
default:
return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...) + "\n"
}
}
func (actual *asyncActual) actualInputIsAFunction() bool {
actualType := reflect.TypeOf(actual.actualInput)
return actualType.Kind() == reflect.Func && actualType.NumIn() == 0 && actualType.NumOut() > 0
}
func (actual *asyncActual) pollActual() (interface{}, error) {
if actual.actualInputIsAFunction() {
values := reflect.ValueOf(actual.actualInput).Call([]reflect.Value{})
extras := []interface{}{}
for _, value := range values[1:] {
extras = append(extras, value.Interface())
}
success, message := vetExtras(extras)
if !success {
return nil, errors.New(message)
}
return values[0].Interface(), nil
}
return actual.actualInput, nil
}
type oracleMatcher interface {
MatchMayChangeInTheFuture(actual interface{}) bool
}
func (actual *asyncActual) matcherMayChange(matcher OmegaMatcher, value interface{}) bool {
if actual.actualInputIsAFunction() {
return true
}
oracleMatcher, ok := matcher.(oracleMatcher)
if !ok {
return true
}
return oracleMatcher.MatchMayChangeInTheFuture(value)
}
func (actual *asyncActual) match(matcher OmegaMatcher, desiredMatch bool, optionalDescription ...interface{}) bool {
timer := time.Now()
timeout := time.After(actual.timeoutInterval)
description := actual.buildDescription(optionalDescription...)
var matches bool
var err error
mayChange := true
value, err := actual.pollActual()
if err == nil {
mayChange = actual.matcherMayChange(matcher, value)
matches, err = matcher.Match(value)
}
fail := func(preamble string) {
errMsg := ""
message := ""
if err != nil {
errMsg = "Error: " + err.Error()
} else {
if desiredMatch {
message = matcher.FailureMessage(value)
} else {
message = matcher.NegatedFailureMessage(value)
}
}
actual.fail(fmt.Sprintf("%s after %.3fs.\n%s%s%s", preamble, time.Since(timer).Seconds(), description, message, errMsg), 3+actual.offset)
}
if actual.asyncType == asyncActualTypeEventually {
for {
if err == nil && matches == desiredMatch {
return true
}
if !mayChange {
fail("No future change is possible. Bailing out early")
return false
}
select {
case <-time.After(actual.pollingInterval):
value, err = actual.pollActual()
if err == nil {
mayChange = actual.matcherMayChange(matcher, value)
matches, err = matcher.Match(value)
}
case <-timeout:
fail("Timed out")
return false
}
}
} else if actual.asyncType == asyncActualTypeConsistently {
for {
if !(err == nil && matches == desiredMatch) {
fail("Failed")
return false
}
if !mayChange {
return true
}
select {
case <-time.After(actual.pollingInterval):
value, err = actual.pollActual()
if err == nil {
mayChange = actual.matcherMayChange(matcher, value)
matches, err = matcher.Match(value)
}
case <-timeout:
return true
}
}
}
return false
}