-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
179 lines (148 loc) · 3.97 KB
/
consumer_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
package cone_test
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/zapling/cone"
"github.com/zapling/cone/conetest"
)
func TestNew(t *testing.T) {
c := cone.New(nil, nil)
if c == nil {
t.Fatalf("Expected Consumer got nil")
}
}
func TestListenAndConsume(t *testing.T) {
t.Run("Nil source should error", func(t *testing.T) {
c := cone.New(nil, nil)
err := c.ListenAndConsume()
if err == nil {
t.Fatal("Expected error but got nil")
}
})
t.Run("Nil handler should error", func(t *testing.T) {
s := conetest.NewSource()
c := cone.New(s, nil)
err := c.ListenAndConsume()
if err == nil {
t.Fatalf("Expected error but got nil")
}
})
t.Run("Event should be served", func(t *testing.T) {
e := conetest.NewEvent("event.subject", nil)
s := conetest.NewSource()
s.AddEvent(e)
h := cone.NewHandlerMux()
h.HandleFunc("event.subject", func(r cone.Response, _ *cone.Event) {
_ = r.Ack()
})
c := cone.New(s, h)
go func() {
_ = c.ListenAndConsume()
}()
time.Sleep(5 * time.Millisecond)
if s.NumAckd() != 1 {
t.Error("Expected event to be acked, but it was not")
}
})
t.Run("Calling func while still running should error", func(t *testing.T) {
source := conetest.NewSource()
var handler cone.HandlerFunc = func(r cone.Response, e *cone.Event) {}
c := cone.New(source, handler)
go func() {
_ = c.ListenAndConsume()
}()
time.Sleep(1 * time.Millisecond) // Give first gorutine time to start
go func() {
err := c.ListenAndConsume()
if err == nil {
panic("Expected error but got nil")
}
}()
time.Sleep(5 * time.Millisecond)
})
}
func TestMiddlewareAroundConsumer(t *testing.T) {
s := conetest.NewSource()
h := cone.NewHandlerMux()
h.HandleFunc("event.subject", func(r cone.Response, e *cone.Event) {
})
var beenInsideMiddleware bool
middleware := func(next cone.Handler) cone.HandlerFunc {
return func(r cone.Response, e *cone.Event) {
beenInsideMiddleware = true
next.Serve(r, e)
}
}
c := cone.New(s, middleware(h))
r := conetest.NewRecorder()
c.Serve(r, conetest.NewEvent("event.subject", nil))
if !beenInsideMiddleware {
t.Fatal("Expected to hit the middleware, but did not")
}
}
func TestShutdown(t *testing.T) {
t.Run("Unstarted consumer should error", func(t *testing.T) {
s := conetest.NewSource()
h := cone.NewHandlerMux()
c := cone.New(s, h)
err := c.Shutdown(context.Background())
if err == nil {
t.Error("Expected error but got nil")
}
})
t.Run("Should wait for all started handles", func(t *testing.T) {
s := conetest.NewSource()
s.AddEvent(conetest.NewEvent("event.subject", nil))
h := cone.NewHandlerMux()
h.HandleFunc("event.subject", func(r cone.Response, _ *cone.Event) {
time.Sleep(1 * time.Second)
_ = r.Ack()
})
c := cone.New(s, h)
var consumerHasStopped atomic.Bool
go func() {
err := c.ListenAndConsume()
if !errors.Is(err, cone.ErrConsumerStopped) {
panic(fmt.Sprintf("Unexpected error: %s", err.Error()))
}
consumerHasStopped.Swap(true)
}()
time.Sleep(5 * time.Millisecond)
err := c.Shutdown(context.Background())
if err != nil {
t.Fatalf("Expected nil but got err: %s", err.Error())
}
if !consumerHasStopped.Load() {
t.Fatal("Consumer never stopped!")
}
numAcked := s.NumAckd()
if numAcked != 1 {
t.Fatalf("Expected 1 acked message, but got: %d", numAcked)
}
})
t.Run("Should stop eariler ListenAndConsume", func(t *testing.T) {
s := conetest.NewSource()
h := cone.NewHandlerMux()
c := cone.New(s, h)
var consumerHasStopped atomic.Bool
go func() {
err := c.ListenAndConsume()
if !errors.Is(err, cone.ErrConsumerStopped) {
panic(fmt.Sprintf("Unexpected error: %s", err.Error()))
}
consumerHasStopped.Swap(true)
}()
time.Sleep(5 * time.Millisecond)
err := c.Shutdown(context.Background())
if err != nil {
t.Fatalf("Expected nil but got err: %s", err.Error())
}
if !consumerHasStopped.Load() {
t.Fatal("Consumer never stopped!")
}
})
}