-
Notifications
You must be signed in to change notification settings - Fork 7
/
run_test.go
203 lines (175 loc) · 4.26 KB
/
run_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
package reflex
import (
"context"
"testing"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunDelayLag(t *testing.T) {
errDone := errors.New("no more events to mock")
t0 := time.Now()
// Define common set of events for each test.
eventsFunc := func() []*Event {
return []*Event{
{ID: "0", Timestamp: t0.Add(time.Second * -2)},
{ID: "1", Timestamp: t0.Add(time.Second * -1)},
{ID: "2", Timestamp: t0.Add(time.Second * 0)},
{ID: "3", Timestamp: t0.Add(time.Second * 1)},
{ID: "4", Timestamp: t0.Add(time.Second * 2)},
}
}
tests := []struct {
name string
opt StreamOption
sleepSecs []int
wantErr bool
}{
{
name: "no lag",
opt: WithStreamToHead(),
sleepSecs: []int{}, // No events are delayed.
}, {
name: "1 sec lag",
opt: WithStreamLag(time.Second),
sleepSecs: []int{1, 2, 3}, // First two events are not delayed.
}, {
name: "5 sec lag",
opt: WithStreamLag(time.Second * 5),
sleepSecs: []int{3, 4, 5, 6, 7}, // All events are delayed.
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock the sleep durations
var sleeps []time.Duration
newTimer = func(d time.Duration) *time.Timer {
sleeps = append(sleeps, d)
return time.NewTimer(0)
}
// Mock current time.
since = func(t time.Time) time.Duration {
return t0.Sub(t)
}
// Mock the spec
var (
actualOpts []StreamOption
consumer mockconsumer
)
spec := NewSpec(func(ctx context.Context, after string, opts ...StreamOption) (StreamClient, error) {
actualOpts = opts
return &mockstreamclient{eventsFunc(), errDone, ""}, nil
}, &mockcursor{}, &consumer, tt.opt, WithStreamFromHead(), WithStreamToHead())
// Run
err := Run(context.Background(), spec)
jtest.Require(t, errDone, err)
// Assert lag options filtered out.
var temp StreamOptions
for _, opt := range actualOpts {
opt(&temp)
}
require.Zero(t, temp.Lag)
// Assert sleep durations
var expectedSleeps []time.Duration
for _, sec := range tt.sleepSecs {
expectedSleeps = append(expectedSleeps, time.Second*time.Duration(sec))
}
require.Equal(t, expectedSleeps, sleeps)
})
}
}
func TestMockStream(t *testing.T) {
tests := []struct {
name string
events []*Event
startCursorAt string
expectedCursor string
expectedEvents int64
}{
{
name: "test mock events",
events: []*Event{
{
ID: "1",
},
{
ID: "2",
},
{
ID: "3",
},
},
expectedCursor: "3",
expectedEvents: 3,
},
{
name: "mock events with cursor",
events: []*Event{
{
ID: "1",
},
{
ID: "2",
},
{
ID: "3",
},
},
startCursorAt: "2",
expectedCursor: "3",
expectedEvents: 1,
},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
endErr := errors.New("end of mock")
streamFunc := NewMockStream(tt.events, endErr)
consumer := &mockconsumer{}
memCursor := &mockcursor{
cursor: map[string]string{
consumer.Name(): tt.startCursorAt,
},
}
spec := NewSpec(streamFunc, memCursor, consumer)
err := Run(ctx, spec)
jtest.Require(t, endErr, err)
cursorResult, err := memCursor.GetCursor(ctx, consumer.Name())
jtest.RequireNil(t, err)
assert.Equal(t, tt.expectedCursor, cursorResult)
assert.Equal(t, tt.expectedEvents, int64(len(consumer.Events)))
})
}
}
type mockconsumer struct {
Events []*Event
}
func (m *mockconsumer) Name() string {
return ""
}
func (m *mockconsumer) Consume(ctx context.Context, event *Event) error {
m.Events = append(m.Events, event)
return nil
}
type mockcursor struct {
cursor map[string]string
}
func (m *mockcursor) GetCursor(_ context.Context, consumerName string) (string, error) {
val, exists := m.cursor[consumerName]
if !exists {
val = ""
}
return val, nil
}
func (m *mockcursor) SetCursor(_ context.Context, consumerName string, cursor string) error {
if m.cursor == nil {
m.cursor = make(map[string]string, 0)
}
m.cursor[consumerName] = cursor
return nil
}
func (m *mockcursor) Flush(_ context.Context) error {
return nil
}