forked from tarantool/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture_test.go
258 lines (225 loc) · 5.4 KB
/
future_test.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package tarantool_test
import (
"errors"
"sync"
"testing"
"time"
. "github.com/tarantool/go-tarantool"
)
func assertResponseIteratorValue(t testing.TB, it ResponseIterator,
code uint32, resp *Response) {
t.Helper()
if it.Err() != nil {
t.Errorf("An unexpected iteration error: %q", it.Err().Error())
}
if it.Value() == nil {
t.Errorf("An unexpected nil value")
} else if it.Value().Code != code {
t.Errorf("An unexpected response code %d, expected %d", it.Value().Code, code)
}
if it.Value() != resp {
t.Errorf("An unexpected response %v, expected %v", it.Value(), resp)
}
}
func assertResponseIteratorFinished(t testing.TB, it ResponseIterator) {
t.Helper()
if it.Err() != nil {
t.Errorf("An unexpected iteration error: %q", it.Err().Error())
}
if it.Value() != nil {
t.Errorf("An unexpected value %v", it.Value())
}
}
func TestFutureGetIteratorNoItems(t *testing.T) {
fut := NewFuture()
it := fut.GetIterator()
if it.Next() {
t.Errorf("An unexpected next value.")
} else {
assertResponseIteratorFinished(t, it)
}
}
func TestFutureGetIteratorNoResponse(t *testing.T) {
push := &Response{}
fut := NewFuture()
fut.AppendPush(push)
if it := fut.GetIterator(); it.Next() {
assertResponseIteratorValue(t, it, PushCode, push)
if it.Next() == true {
t.Errorf("An unexpected next value.")
}
assertResponseIteratorFinished(t, it)
} else {
t.Errorf("A push message expected.")
}
}
func TestFutureGetIteratorNoResponseTimeout(t *testing.T) {
push := &Response{}
fut := NewFuture()
fut.AppendPush(push)
if it := fut.GetIterator().WithTimeout(1 * time.Nanosecond); it.Next() {
assertResponseIteratorValue(t, it, PushCode, push)
if it.Next() == true {
t.Errorf("An unexpected next value.")
}
assertResponseIteratorFinished(t, it)
} else {
t.Errorf("A push message expected.")
}
}
func TestFutureGetIteratorResponseOnTimeout(t *testing.T) {
push := &Response{}
resp := &Response{}
fut := NewFuture()
fut.AppendPush(push)
var done sync.WaitGroup
var wait sync.WaitGroup
wait.Add(1)
done.Add(1)
go func() {
defer done.Done()
var it ResponseIterator
var cnt = 0
for it = fut.GetIterator().WithTimeout(5 * time.Second); it.Next(); {
code := PushCode
r := push
if cnt == 1 {
code = OkCode
r = resp
}
assertResponseIteratorValue(t, it, code, r)
cnt += 1
if cnt == 1 {
wait.Done()
}
}
assertResponseIteratorFinished(t, it)
if cnt != 2 {
t.Errorf("An unexpected count of responses %d != %d", cnt, 2)
}
}()
wait.Wait()
fut.SetResponse(resp)
done.Wait()
}
func TestFutureGetIteratorFirstResponse(t *testing.T) {
resp1 := &Response{}
resp2 := &Response{}
fut := NewFuture()
fut.SetResponse(resp1)
fut.SetResponse(resp2)
if it := fut.GetIterator(); it.Next() {
assertResponseIteratorValue(t, it, OkCode, resp1)
if it.Next() == true {
t.Errorf("An unexpected next value.")
}
assertResponseIteratorFinished(t, it)
} else {
t.Errorf("A response expected.")
}
}
func TestFutureGetIteratorFirstError(t *testing.T) {
const errMsg1 = "error1"
const errMsg2 = "error2"
fut := NewFuture()
fut.SetError(errors.New(errMsg1))
fut.SetError(errors.New(errMsg2))
it := fut.GetIterator()
if it.Next() {
t.Errorf("An unexpected value.")
} else if it.Err() == nil {
t.Errorf("An error expected.")
} else if it.Err().Error() != errMsg1 {
t.Errorf("An unexpected error %q, expected %q", it.Err().Error(), errMsg1)
}
}
func TestFutureGetIteratorResponse(t *testing.T) {
responses := []*Response{
{},
{},
{Code: OkCode},
}
fut := NewFuture()
for i, resp := range responses {
if i == len(responses)-1 {
fut.SetResponse(resp)
} else {
fut.AppendPush(resp)
}
}
var its = []ResponseIterator{
fut.GetIterator(),
fut.GetIterator().WithTimeout(5 * time.Second),
}
for _, it := range its {
var cnt = 0
for it.Next() {
code := PushCode
if cnt == len(responses)-1 {
code = OkCode
}
assertResponseIteratorValue(t, it, code, responses[cnt])
cnt += 1
}
assertResponseIteratorFinished(t, it)
if cnt != len(responses) {
t.Errorf("An unexpected count of responses %d != %d", cnt, len(responses))
}
}
}
func TestFutureGetIteratorError(t *testing.T) {
const errMsg = "error message"
responses := []*Response{
{},
{},
}
err := errors.New(errMsg)
fut := NewFuture()
for _, resp := range responses {
fut.AppendPush(resp)
}
fut.SetError(err)
var its = []ResponseIterator{
fut.GetIterator(),
fut.GetIterator().WithTimeout(5 * time.Second),
}
for _, it := range its {
var cnt = 0
for it.Next() {
code := PushCode
assertResponseIteratorValue(t, it, code, responses[cnt])
cnt += 1
}
if err = it.Err(); err != nil {
if err.Error() != errMsg {
t.Errorf("An unexpected error %q, expected %q", err.Error(), errMsg)
}
} else {
t.Errorf("An error expected.")
}
if cnt != len(responses) {
t.Errorf("An unexpected count of responses %d != %d", cnt, len(responses))
}
}
}
func TestFutureSetStateRaceCondition(t *testing.T) {
err := errors.New("any error")
resp := &Response{}
respAppend := &Response{}
for i := 0; i < 1000; i++ {
fut := NewFuture()
for j := 0; j < 9; j++ {
go func(opt int) {
if opt%3 == 0 {
fut.AppendPush(respAppend)
} else if opt%3 == 1 {
fut.SetError(err)
} else {
fut.SetResponse(resp)
}
}(j)
}
}
// It may be false-positive, but very rarely - it's ok for such very
// simple race conditions tests.
}