-
Notifications
You must be signed in to change notification settings - Fork 9
/
revorder_test.go
219 lines (200 loc) · 4.53 KB
/
revorder_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
// Copyright 2017-2020, Square, Inc.
package etre_test
import (
"strings"
"testing"
"time"
"github.com/go-test/deep"
"github.com/square/etre"
)
func TestRevOrder(t *testing.T) {
revo := etre.NewRevOrder(0, false)
e := etre.CDCEvent{
EntityId: "abc",
EntityRev: 0,
Op: "i",
}
ok, buf := revo.InOrder(e)
if !ok {
t.Error("not ok, expected ok")
}
if buf != nil {
t.Errorf("buf not nil, expected nil: %#v", buf)
}
ok, buf = revo.InOrder(e)
if ok {
t.Error("ok, expected not ok becuase it's the same rev")
}
if buf != nil {
t.Errorf("buf not nil, expected nil: %#v", buf)
}
e.EntityRev = 1
ok, buf = revo.InOrder(e)
if !ok {
t.Error("not ok, expected ok becuase rev += 1")
}
if buf != nil {
t.Errorf("buf not nil, expected nil: %#v", buf)
}
e.EntityRev = 3
ok, buf = revo.InOrder(e)
if ok {
t.Error("ok, expected not ok becuase rev 2 not sent yet")
}
if buf != nil {
t.Errorf("buf not nil, expected nil: %#v", buf)
}
e.EntityRev = 4
ok, buf = revo.InOrder(e)
if ok {
t.Error("ok, expected not ok becuase rev 2 not sent yet")
}
if buf != nil {
t.Errorf("buf not nil, expected nil: %#v", buf)
}
e.EntityRev = 2
ok, buf = revo.InOrder(e)
if !ok {
t.Error("not ok, expected ok becuase rev set complete (2, 3)")
}
if buf == nil {
t.Errorf("buf nil, expected [2,3]")
}
expect := []etre.CDCEvent{
{
EntityId: "abc",
EntityRev: 2,
Op: "i",
},
{
EntityId: "abc",
EntityRev: 3,
Op: "i",
},
{
EntityId: "abc",
EntityRev: 4,
Op: "i",
},
}
if diffs := deep.Equal(buf, expect); diffs != nil {
t.Error(diffs)
}
}
func TestRevOrderPanicRRltQR(t *testing.T) {
recoverChan := make(chan interface{}, 1)
go func() {
defer func() {
if r := recover(); r != nil {
recoverChan <- r
} else {
recoverChan <- nil
}
}()
revo := etre.NewRevOrder(10, false)
e := etre.CDCEvent{
EntityId: "abc",
EntityRev: 1,
Op: "i",
}
revo.InOrder(e)
e.EntityRev = 0 // causes panic
revo.InOrder(e)
}()
select {
case r := <-recoverChan:
if r == nil {
t.Errorf("nil panic/recover, expected non-nil recover() value")
} else {
pat := "revision 1 received first and revision 0 received second"
if !strings.Contains(r.(string), pat) {
t.Errorf("panic msg doesn't contain '%s': %s", pat, r)
}
}
case <-time.After(1 * time.Second):
t.Fatal("timeout waiting for goroutine to panic")
}
}
func TestRevOrderPanicEvict(t *testing.T) {
recoverChan := make(chan interface{}, 1)
go func() {
defer func() {
if r := recover(); r != nil {
recoverChan <- r
} else {
recoverChan <- nil
}
}()
revo := etre.NewRevOrder(2, false)
e := etre.CDCEvent{
EntityId: "abc",
EntityRev: 1,
Op: "i",
}
revo.InOrder(e)
e.EntityRev = 2
revo.InOrder(e)
e.EntityRev = 4
revo.InOrder(e)
// Now abc is being reordered
e.EntityId = "def"
e.EntityRev = 1
revo.InOrder(e)
e.EntityRev = 2
revo.InOrder(e)
// Now LRU cache is full: abc and def
// Add entity ghi which evicts abc and causes the panic
e.EntityId = "ghi"
revo.InOrder(e)
}()
select {
case r := <-recoverChan:
if r == nil {
t.Errorf("nil panic/recover, expected non-nil recover() value")
} else {
pat1 := "Entity abc evicted"
if !strings.Contains(r.(string), pat1) {
t.Errorf("panic msg doesn't contain '%s': %s", pat1, r)
}
pat2 := "revisions > 2. Received revisions: 4"
if !strings.Contains(r.(string), pat2) {
t.Errorf("panic msg doesn't contain '%s': %s", pat2, r)
}
}
case <-time.After(1 * time.Second):
t.Fatal("timeout waiting for goroutine to panic")
}
}
func TestRevOrderIgnorePastRevs(t *testing.T) {
// Same as TestRevOrderPanicRRltQR but ignorePastRevs=true which ignores
// the past rev instead of panicing. This is used in changestream.ServerStreamer
// because it has two inputs: events from the backlog (cdc.Store) and
// events from a MongoDB change stream. These two can overlap at the start.
recoverChan := make(chan interface{}, 1)
go func() {
defer func() {
if r := recover(); r != nil {
recoverChan <- r
} else {
recoverChan <- nil
}
}()
revo := etre.NewRevOrder(10, true) // = ignorePastRevs
e := etre.CDCEvent{
EntityId: "abc",
EntityRev: 1,
Op: "i",
}
revo.InOrder(e)
e.EntityRev = 0 // does NOT cause panic
revo.InOrder(e)
}()
select {
case r := <-recoverChan:
if r != nil {
t.Errorf("panic, expected nil: %v", r)
}
case <-time.After(1 * time.Second):
t.Fatal("timeout waiting for goroutine to panic")
}
}