This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdb_test.go
More file actions
392 lines (336 loc) · 8.8 KB
/
Copy pathdb_test.go
File metadata and controls
392 lines (336 loc) · 8.8 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package limestone
import (
"context"
"encoding/json"
"strconv"
"testing"
"time"
"github.com/ridge/limestone/client"
"github.com/ridge/limestone/indices"
"github.com/ridge/limestone/kafka"
"github.com/ridge/limestone/kafka/mock"
"github.com/ridge/limestone/test"
"github.com/ridge/limestone/tlog"
"github.com/ridge/limestone/wire"
"github.com/ridge/parallel"
"github.com/stretchr/testify/require"
)
// Many of these tests rely on timeout for failure. They wait for a condition
// that becomes true almost immediately when the code is correct, but never if
// the test fails. This is uncomfortable for debugging, so this suite implements
// its own short timeouts.
//
// Upon timeout, the tap channels are closed, which causes the waiting code to
// give up and fail the test.
const testTimeout = time.Second
type fooID string
type fooA struct {
Meta `limestone:"name=foo,producer=a"`
ID fooID `limestone:"identity"`
A int
}
type fooB struct {
Meta `limestone:"name=foo,producer=b"`
B int
}
type foo struct {
fooA
fooB
}
func (f foo) Survive() bool {
return f.B >= 0
}
type barID string
type barA struct {
Meta `limestone:"name=bar,producer=a"`
ID barID `limestone:"identity"`
FooID fooID
A string
}
type barB struct {
Meta `limestone:"name=bar,producer=b"`
B string
}
type bar struct {
barA
barB
}
var (
kindFoo = KindOf(foo{})
indexFooID = indices.FieldIndex("FooID")
kindBar = KindOf(bar{}, indexFooID)
)
type option interface {
apply(*Config)
}
type optTap chan<- Snapshot
func (o optTap) apply(c *Config) {
c.DebugTap = o
}
type optWakeUp WakeUpFn
func (o optWakeUp) apply(c *Config) {
c.WakeUp = WakeUpFn(o)
}
func testEnv(t *testing.T) (kafka.Client, *parallel.Group) {
k := mock.New()
group := test.GroupWithTimeout(t, testTimeout)
return k, group
}
func createDB(kafka kafka.Client, group *parallel.Group, source Source, opt ...option) *DB {
config := Config{
Client: client.NewKafkaClient(kafka),
Entities: KindList{kindFoo, kindBar},
Source: source,
Logger: tlog.Get(group.Context()),
Session: 1,
}
for _, o := range opt {
o.apply(&config)
}
db := New(config)
var suffix string
if source.Producer != "" {
suffix = ":" + string(source.Producer)
}
group.Spawn("limestone"+suffix, parallel.Fail, func(ctx context.Context) error {
if config.DebugTap != nil {
defer close(config.DebugTap)
}
return db.Run(ctx)
})
return db
}
func TestAdminBeforeStart(t *testing.T) {
k, group := testEnv(t)
require.NoError(t, Bootstrap(group.Context(), k, 0, "txlog",
foo{fooA: fooA{ID: fooID("f1")}},
foo{fooA: fooA{ID: fooID("f2")}, fooB: fooB{B: -1}},
))
tap := make(chan Snapshot)
createDB(k, group, Source{}, optTap(tap))
for snapshot := range tap {
if snapshot.Get(fooID("f1"), new(foo)) {
break
}
var f foo
require.False(t, snapshot.Get(fooID("f2"), &f))
}
require.NoError(t, group.Context().Err()) // not timed out
}
func TestAdminAfterStart(t *testing.T) {
k, group := testEnv(t)
tap := make(chan Snapshot)
createDB(k, group, Source{}, optTap(tap))
require.NoError(t, Bootstrap(group.Context(), k, 0, "txlog",
foo{fooA: fooA{ID: fooID("f1")}},
foo{fooA: fooA{ID: fooID("f2")}, fooB: fooB{B: -1}},
))
for snapshot := range tap {
if snapshot.Get(fooID("f1"), new(foo)) {
break
}
var f foo
require.False(t, snapshot.Get(fooID("f2"), &f))
}
require.NoError(t, group.Context().Err()) // not timed out
}
func TestAudit(t *testing.T) {
k, group := testEnv(t)
require.NoError(t, Bootstrap(group.Context(), k, 0, "txlog"))
a := createDB(k, group, Source{Producer: "a"})
require.NoError(t, a.WaitReady(group.Context()))
var wtxn wire.Transaction
messages := make(chan *kafka.IncomingMessage, 2)
group.Spawn("reader", parallel.Fail, func(ctx context.Context) error {
return k.Read(ctx, "txlog", 0, messages)
})
require.Nil(t, <-messages)
a.Do(func(txn Transaction) {
txn.Set(foo{fooA: fooA{ID: "f1"}})
})
require.NoError(t, json.Unmarshal((<-messages).Value, &wtxn))
require.Nil(t, <-messages)
require.Nil(t, wtxn.Audit)
a.Do(func(txn Transaction) {
txn.Set(foo{fooA: fooA{ID: "f2"}})
txn.Annotate("animal", "squirrel")
txn.Annotate("vegetable", "carrot")
txn.Annotate("animal", "squirrel")
})
require.NoError(t, json.Unmarshal((<-messages).Value, &wtxn))
require.Nil(t, <-messages)
var audit map[string]string
require.NoError(t, json.Unmarshal(wtxn.Audit, &audit))
require.Equal(t, map[string]string{"animal": "squirrel", "vegetable": "carrot"}, audit)
}
func TestResponse(t *testing.T) {
k, group := testEnv(t)
require.NoError(t, Bootstrap(group.Context(), k, 0, "txlog"))
tap := make(chan Snapshot, 1)
a := createDB(k, group, Source{Producer: "a"}, optTap(tap))
createDB(k, group, Source{Producer: "b"}, optWakeUp(func(ctx context.Context, txn Transaction, entities []any) {
for _, e := range entities {
switch e := e.(type) {
case foo:
e.B = e.A
txn.Set(e)
default:
require.FailNow(t, "unexpected type %T", e)
}
}
}))
require.NoError(t, a.WaitReady(group.Context()))
a.Do(func(txn Transaction) {
txn.Set(foo{fooA: fooA{ID: fooID("f1"), A: 7}})
})
for snapshot := range tap {
var f1 foo
if !snapshot.Get(fooID("f1"), &f1) || f1.B == 0 {
continue
}
require.Equal(t, 7, f1.B)
break
}
require.NoError(t, group.Context().Err()) // not timed out
a.Do(func(txn Transaction) {
var f1 foo
MustGet(txn, fooID("f1"), &f1)
f1.A = 77
txn.Set(f1)
})
for snapshot := range tap {
var f1 foo
MustGet(snapshot, fooID("f1"), &f1)
if f1.B == 7 {
continue
}
require.Equal(t, 77, f1.B)
break
}
a.Do(func(txn Transaction) {
var f1 foo
MustGet(txn, fooID("f1"), &f1)
f1.A = -7
txn.Set(f1)
})
for snapshot := range tap {
var f1 foo
MustGet(snapshot, fooID("f1"), &f1)
if f1.B == 77 {
continue
}
require.Equal(t, -7, f1.B)
break
}
a.Do(func(txn Transaction) {
var f1 foo
require.False(t, txn.Get(fooID("f1"), &f1))
})
require.NoError(t, group.Context().Err()) // not timed out
}
func TestResponseLinked(t *testing.T) {
k, group := testEnv(t)
require.NoError(t, Bootstrap(group.Context(), k, 0, "txlog"))
tap := make(chan Snapshot, 1)
a := createDB(k, group, Source{Producer: "a"}, optTap(tap))
processFoo := func(txn Transaction, id fooID) {
var parent foo
var child bar
MustGet(txn, id, &parent)
parent.B = parent.A
iter := txn.Search(kindBar, indexFooID, id)
for iter(&child) {
parent.B += len(child.A)
child.B = child.A + strconv.Itoa(parent.A)
txn.Set(child)
}
txn.Set(parent)
}
createDB(k, group, Source{Producer: "b"}, optWakeUp(func(ctx context.Context, txn Transaction, entities []any) {
for _, e := range entities {
switch e := e.(type) {
case foo:
processFoo(txn, e.ID)
case bar:
var old bar
if txn.Before().Get(e.ID, &old) && old.FooID != e.FooID {
processFoo(txn, old.FooID)
}
processFoo(txn, e.FooID)
default:
require.FailNow(t, "unexpected type %T", e)
}
}
}))
require.NoError(t, a.WaitReady(group.Context()))
a.Do(func(txn Transaction) {
txn.Set(foo{fooA: fooA{ID: fooID("f1"), A: 7}})
txn.Set(bar{barA: barA{ID: barID("b11"), A: "one", FooID: fooID("f1")}})
txn.Set(bar{barA: barA{ID: barID("b12"), A: "two", FooID: fooID("f1")}})
txn.Set(foo{fooA: fooA{ID: fooID("f2"), A: 42}})
})
for snapshot := range tap {
var f1, f2 foo
var b11, b12 bar
if !snapshot.Get(fooID("f1"), &f1) || f1.B < 13 {
continue
}
if !snapshot.Get(fooID("f2"), &f2) || f2.B < 42 {
continue
}
if !snapshot.Get(barID("b11"), &b11) || b11.B == "" {
continue
}
if !snapshot.Get(barID("b12"), &b12) || b12.B == "" {
continue
}
require.Equal(t, 13, f1.B)
require.Equal(t, 42, f2.B)
require.Equal(t, "one7", b11.B)
require.Equal(t, "two7", b12.B)
break
}
require.NoError(t, group.Context().Err()) // not timed out
a.Do(func(txn Transaction) {
var f1 foo
MustGet(txn, fooID("f1"), &f1)
f1.A = 77
txn.Set(f1)
})
for snapshot := range tap {
var f1 foo
MustGet(snapshot, fooID("f1"), &f1)
var b11, b12 bar
MustGet(snapshot, barID("b11"), &b11)
MustGet(snapshot, barID("b12"), &b12)
if f1.B == 13 || b11.B == "one7" || b12.B == "two7" {
continue
}
require.Equal(t, 83, f1.B)
require.Equal(t, "one77", b11.B)
require.Equal(t, "two77", b12.B)
break
}
require.NoError(t, group.Context().Err()) // not timed out
a.Do(func(txn Transaction) {
var b11 bar
MustGet(txn, barID("b11"), &b11)
b11.FooID = fooID("f2")
txn.Set(b11)
})
for snapshot := range tap {
var f1, f2 foo
MustGet(snapshot, fooID("f1"), &f1)
MustGet(snapshot, fooID("f2"), &f2)
var b11 bar
MustGet(snapshot, barID("b11"), &b11)
if f1.B == 83 || f2.B == 42 || b11.B == "one77" {
continue
}
require.Equal(t, 80, f1.B)
require.Equal(t, 45, f2.B)
require.Equal(t, "one42", b11.B)
break
}
require.NoError(t, group.Context().Err()) // not timed out
}