-
Notifications
You must be signed in to change notification settings - Fork 12
/
response_queue_test.go
82 lines (65 loc) · 1.89 KB
/
response_queue_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
package fiber_test
import (
"testing"
"time"
"github.com/gojek/fiber"
testUtilsHttp "github.com/gojek/fiber/internal/testutils/http"
"github.com/stretchr/testify/assert"
)
func chanToArray(ch <-chan fiber.Response) []fiber.Response {
var responses []fiber.Response
for r := range ch {
responses = append(responses, r)
}
return responses
}
func makeChan(r ...fiber.Response) chan fiber.Response {
out := make(chan fiber.Response)
go func() {
for _, resp := range r {
out <- resp
}
close(out)
}()
return out
}
func TestNewResponseQueue(t *testing.T) {
responses := []fiber.Response{
testUtilsHttp.MockResp(200, "foo", nil, nil),
testUtilsHttp.MockResp(200, "bar", nil, nil),
}
q := fiber.NewResponseQueue(makeChan(responses...), 0)
assert.Equal(t, responses, chanToArray(q.Iter()))
assert.Equal(t, responses, chanToArray(q.Iter()))
}
func TestNewResponseQueueFromResponses(t *testing.T) {
responses := []fiber.Response{
testUtilsHttp.MockResp(200, "foo", nil, nil),
testUtilsHttp.MockResp(200, "bar", nil, nil),
}
q := fiber.NewResponseQueueFromResponses(responses...)
assert.Equal(t, responses, chanToArray(q.Iter()))
assert.Equal(t, responses, chanToArray(q.Iter()))
q = fiber.NewResponseQueueFromResponses()
assert.Empty(t, chanToArray(q.Iter()))
}
func TestResponseQueue_Iter(t *testing.T) {
responses := []fiber.Response{
testUtilsHttp.MockResp(200, "fist", nil, nil),
testUtilsHttp.MockResp(200, "second", nil, nil),
testUtilsHttp.MockResp(200, "third", nil, nil),
testUtilsHttp.MockResp(200, "fourth", nil, nil),
}
out := make(chan fiber.Response)
go func() {
for i, r := range responses {
time.Sleep(time.Duration(i*50) * time.Millisecond)
out <- r
}
close(out)
}()
q := fiber.NewResponseQueue(out, 0)
assert.Equal(t, responses, chanToArray(q.Iter()))
time.Sleep(400 * time.Millisecond)
assert.Equal(t, responses, chanToArray(q.Iter()))
}