forked from cloudfoundry/go-log-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_test.go
320 lines (245 loc) · 7.08 KB
/
window_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package client_test
import (
"context"
"errors"
"reflect"
"testing"
"time"
client "code.cloudfoundry.org/go-log-cache/v3"
"code.cloudfoundry.org/go-loggregator/v10/rpc/loggregator_v2"
)
func TestWindowAdvancesStartTime(t *testing.T) {
w := windowSetup(t)
w.v.result = []bool{true, false}
client.Window(w.ctx, w.v.visit, w.w.walk, client.WithWindowInterval(time.Nanosecond))
if len(w.w.starts) != 2 {
t.Fatalf("expected walk to have 2 starts: %d", len(w.w.starts))
}
if w.w.starts[0].IsZero() {
t.Fatal("expected start to be non-zero")
}
if w.w.starts[1].Sub(w.w.starts[0]) != time.Nanosecond {
t.Fatalf("expected interval to equal 1 nanosecond: %v", w.w.starts[1].Sub(w.w.starts[0]))
}
if w.w.ends[1].Sub(w.w.starts[1]) != time.Hour {
t.Fatalf("expected range to equal 1 hour: %v", w.w.ends[1].Sub(w.w.starts[1]))
}
if len(w.v.e) != 2 {
t.Fatalf("expected to have two sets of envelopes: %d", len(w.v.e))
}
if !reflect.DeepEqual(w.v.e[0], []*loggregator_v2.Envelope{{Timestamp: 2}}) {
t.Fatalf("expected to have certain envelope")
}
if !reflect.DeepEqual(w.v.e[1], []*loggregator_v2.Envelope{{Timestamp: 2}}) {
t.Fatalf("expected to have certain envelope")
}
}
func TestWindowQueriesOverARange(t *testing.T) {
w := windowSetup(t)
w.cancel()
client.Window(w.ctx, w.v.visit, w.w.walk, client.WithWindowInterval(time.Nanosecond))
if len(w.w.starts) != 1 {
t.Fatalf("expected walk to have 1 start: %d", len(w.w.starts))
}
if w.w.starts[0].IsZero() {
t.Fatal("expected start to be non-zero")
}
if w.w.ends[0].Sub(w.w.starts[0]) != time.Hour {
t.Fatalf("expected range to equal 1 hour: %v", w.w.ends[0].Sub(w.w.starts[0]))
}
}
func TestWindowUsesGivenStartTime(t *testing.T) {
w := windowSetup(t)
w.cancel()
client.Window(w.ctx, w.v.visit, w.w.walk,
client.WithWindowStartTime(time.Unix(1, 0)),
client.WithWindowInterval(time.Nanosecond),
)
if len(w.w.starts) != 1 {
t.Fatalf("expected walk to have 1 start: %d", len(w.w.starts))
}
if w.w.starts[0] != time.Unix(1, 0) {
t.Fatalf("expected start to equal the given value: %v", w.w.starts[0])
}
if w.w.ends[0].Sub(w.w.starts[0]) != time.Hour {
t.Fatalf("expected range to equal 1 hour: %v", w.w.ends[0].Sub(w.w.starts[0]))
}
}
func TestWindowUsesGivenWidth(t *testing.T) {
w := windowSetup(t)
w.cancel()
client.Window(w.ctx, w.v.visit, w.w.walk,
client.WithWindowStartTime(time.Unix(1, 0)),
client.WithWindowWidth(time.Minute),
client.WithWindowInterval(time.Nanosecond),
)
if len(w.w.starts) != 1 {
t.Fatalf("expected walk to have 1 start: %d", len(w.w.starts))
}
if w.w.starts[0] != time.Unix(1, 0) {
t.Fatalf("expected start to equal the given value: %v", w.w.starts[0])
}
if w.w.ends[0].Sub(w.w.starts[0]) != time.Minute {
t.Fatalf("expected range to equal 1 minute: %v", w.w.ends[0].Sub(w.w.starts[0]))
}
}
func TestWindowUsesGivenWidthWithoutStartSet(t *testing.T) {
w := windowSetup(t)
client.Window(w.ctx, w.v.visit, w.w.walk,
client.WithWindowWidth(time.Minute),
client.WithWindowInterval(time.Nanosecond),
)
if len(w.w.starts) != 1 {
t.Fatalf("expected walk to have 1 start: %d", len(w.w.starts))
}
if time.Since(w.w.starts[0]) >= time.Hour {
t.Fatalf("expected start to be now-Minute: %v", w.w.starts[0])
}
if w.w.ends[0].Sub(w.w.starts[0]) != time.Minute {
t.Fatalf("expected range to equal 1 minute: %v", w.w.ends[0].Sub(w.w.starts[0]))
}
}
func TestWindowCreatesWalkContextWithTimeoutAsInterval(t *testing.T) {
w := windowSetup(t)
now := time.Now()
interval := 100 * time.Millisecond
client.Window(
w.ctx,
w.v.visit,
w.w.walk,
client.WithWindowInterval(interval),
)
if len(w.w.ctxs) != 1 {
t.Fatalf("expected walk to have 1 context: %d", len(w.w.ctxs))
}
for _, c := range w.w.ctxs {
timeout, ok := c.Deadline()
if !ok {
t.Fatalf("Your deadline isn't set")
}
// context deadline gets set one interval into the ticker
if !almostEquals(timeout, now.Add(2*interval), interval) {
t.Fatalf("Deadline on walk context is too long")
}
}
}
func TestBuildWalker(t *testing.T) {
w := windowSetup(t)
w.r.envelopes = append(w.r.envelopes, []*loggregator_v2.Envelope{
{Timestamp: 100},
{Timestamp: 110},
})
w.r.envelopes = append(w.r.envelopes, []*loggregator_v2.Envelope{
{Timestamp: 120},
{Timestamp: 190},
})
w.r.errs = append(w.r.errs, nil, nil)
ww := client.BuildWalker("some-id", w.r.read)
es := ww(w.ctx, time.Unix(0, 100), time.Unix(0, 200))
if len(es) != 4 {
t.Fatalf("expected 4 envelopes: %d", len(es))
}
if !reflect.DeepEqual(es, []*loggregator_v2.Envelope{
{Timestamp: 100},
{Timestamp: 110},
{Timestamp: 120},
{Timestamp: 190},
}) {
t.Fatalf("expected to have certain envelope")
}
if w.r.sourceIDs[0] != "some-id" {
t.Fatalf("expected sourceID to equal some-id: %s", w.r.sourceIDs[0])
}
if w.r.starts[0] != 100 {
t.Fatalf("expected start to equal 100: %d", w.r.starts[0])
}
}
func TestWalkerStopsReadingAfterError(t *testing.T) {
w := windowSetup(t)
w.r.envelopes = append(w.r.envelopes, []*loggregator_v2.Envelope{
{Timestamp: 100},
{Timestamp: 110},
})
w.r.envelopes = append(w.r.envelopes, []*loggregator_v2.Envelope{
// Correlates with error
})
w.r.envelopes = append(w.r.envelopes, []*loggregator_v2.Envelope{
{Timestamp: 120},
{Timestamp: 190},
})
w.r.errs = append(w.r.errs, nil, errors.New("some-error"), nil)
ww := client.BuildWalker("some-id", w.r.read)
es := ww(w.ctx, time.Unix(0, 100), time.Unix(0, 200))
if len(es) != 2 {
t.Fatalf("expected 2 envelopes: %d", len(es))
}
if !reflect.DeepEqual(es, []*loggregator_v2.Envelope{
{Timestamp: 100},
{Timestamp: 110},
}) {
t.Fatalf("expected to have certain envelope")
}
if w.r.sourceIDs[0] != "some-id" {
t.Fatalf("expected sourceID to equal some-id: %s", w.r.sourceIDs[0])
}
if w.r.starts[0] != 100 {
t.Fatalf("expected start to equal 100: %d", w.r.starts[0])
}
}
type windowT struct {
ctx context.Context
cancel func()
w *stubWalker
v *stubVisitor
r *stubReader
}
func windowSetup(t *testing.T) *windowT {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
return &windowT{
ctx: ctx,
cancel: cancel,
w: newStubWalker(),
v: newStubVisitor(),
r: newStubReader(),
}
}
type stubWalker struct {
ctxs []context.Context
starts []time.Time
ends []time.Time
}
func newStubWalker() *stubWalker {
return &stubWalker{}
}
func (s *stubWalker) walk(
ctx context.Context,
start time.Time,
end time.Time,
) []*loggregator_v2.Envelope {
s.ctxs = append(s.ctxs, ctx)
s.starts = append(s.starts, start)
s.ends = append(s.ends, end)
return []*loggregator_v2.Envelope{
{Timestamp: 2},
}
}
type stubVisitor struct {
e [][]*loggregator_v2.Envelope
result []bool
}
func newStubVisitor() *stubVisitor {
return &stubVisitor{}
}
func (s *stubVisitor) visit(e []*loggregator_v2.Envelope) bool {
s.e = append(s.e, e)
if len(s.result) == 0 {
return false
}
r := s.result[0]
s.result = s.result[1:]
return r
}
func almostEquals(value, expected time.Time, epsilon time.Duration) bool {
return value.Before(expected.Add(epsilon)) && value.After(expected.Add(-epsilon))
}