-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_buffer_test.go
160 lines (136 loc) · 3.69 KB
/
event_buffer_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
package qmp
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
)
func toStr(ee []Event) string {
s := []string{}
for _, e := range ee {
s = append(s, fmt.Sprintf("{%s %d}", e.Type, e.Timestamp.Seconds))
}
return strings.Join(s, " ")
}
func TestBasic(t *testing.T) {
names := []string{
"TEST_EVENT_A", // 0
"TEST_EVENT_B", // 1
"TEST_EVENT_C", // 2
"TEST_EVENT_D", // 3
"TEST_EVENT_E", // 4
"TEST_EVENT_F", // 5
"TEST_EVENT_G", // 6
"TEST_EVENT_H", // 7
"TEST_EVENT_I", // 8
"TEST_EVENT_J", // 9
"TEST_EVENT_K", // 10
"TEST_EVENT_L", // 11
"TEST_EVENT_X", // 12
"TEST_EVENT_N", // 13
"TEST_EVENT_O", // 0
"TEST_EVENT_X", // 1
"TEST_EVENT_Q", // 2
"TEST_EVENT_R", // 3
"TEST_EVENT_S", // 4
}
eb := &eventBuffer{size: 14}
for i := 0; i < len(names); i++ {
ev := Event{Type: names[i]}
ev.Timestamp.Seconds = uint64(i)
eb.Put(ev)
}
// [1] Looking for TEST_EVENT_X with ts >= 13
if got, err := eb.Get(context.Background(), "TEST_EVENT_X", 13); err == nil {
if l := len(got); l != 1 {
t.Fatalf("[1] got %d records instead of 1: %+v", l, got)
}
want := "{TEST_EVENT_X 15}"
if s := toStr(got); s != want {
t.Fatalf("[1] got invalid record:\n\twant:\t%s\n\tgot:\t%s", want, s)
}
} else {
t.Fatal(err)
}
// [2] Looking for TEST_EVENT_X with any ts value
if got, err := eb.Get(context.Background(), "TEST_EVENT_X", 0); err == nil {
if l := len(got); l != 2 {
t.Fatalf("[2] got %d records instead of 2: %+v", l, got)
}
want := "{TEST_EVENT_X 12} {TEST_EVENT_X 15}"
if s := toStr(got); s != want {
t.Fatalf("[2] got invalid record:\n\twant:\t%s\n\tgot:\t%s", want, s)
}
} else {
t.Fatal(err)
}
// [3] Looking for TEST_EVENT_O with ts == 14
if got, err := eb.Get(context.Background(), "TEST_EVENT_O", 14); err == nil {
if l := len(got); l != 1 {
t.Fatalf("[3] got %d records instead of 1: %+v", l, got)
}
want := "{TEST_EVENT_O 14}"
if s := toStr(got); s != want {
t.Fatalf("[3] got invalid record:\n\twant:\t%s\n\tgot:\t%s", want, s)
}
} else {
t.Fatal(err)
}
// [4] Looking for TEST_EVENT_J with ts == 9
if got, err := eb.Get(context.Background(), "TEST_EVENT_J", 9); err == nil {
if l := len(got); l != 1 {
t.Fatalf("[4] got %d records instead of 1: %+v", l, got)
}
want := "{TEST_EVENT_J 9}"
if s := toStr(got); s != want {
t.Fatalf("[4] got invalid record:\n\twant:\t%s\n\tgot:\t%s", want, s)
}
} else {
t.Fatal(err)
}
// [5] Looking for any events with ts >= 12
if got, err := eb.Get(context.Background(), "", 12); err == nil {
if l := len(got); l != 7 {
t.Fatalf("[5] got %d records instead of 7: %+v", l, got)
}
want := "{TEST_EVENT_X 12} {TEST_EVENT_N 13} {TEST_EVENT_O 14} {TEST_EVENT_X 15} {TEST_EVENT_Q 16} {TEST_EVENT_R 17} {TEST_EVENT_S 18}"
if s := toStr(got); s != want {
t.Fatalf("[5] got invalid record:\n\twant:\t%s\n\tgot:\t%s", want, s)
}
} else {
t.Fatal(err)
}
}
func TestWaiting(t *testing.T) {
eb := &eventBuffer{size: 10, ctx: context.Background()}
var wg sync.WaitGroup
ready := make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
close(ready)
got, err := eb.Get(ctx, "TEST_EVENT", 1) // will wait for single Event{Type:"TEST", Timestamp:1}
if err != nil {
t.Error(err)
return
}
t.Logf("got %+v", got)
if l := len(got); l != 1 {
t.Errorf("got %d records instead of 1: %+v", l, got)
}
if got[0].Timestamp.Seconds != 1 {
t.Errorf("got record with wrong timestamp (want TS=1): %+v", got[0])
}
}()
<-ready
for i := 0; i < 3; i++ {
ev := Event{Type: "TEST_EVENT"}
ev.Timestamp.Seconds = uint64(i)
eb.Put(ev)
}
wg.Wait()
}