forked from RecoLabs/gnata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_test.go
More file actions
602 lines (536 loc) · 17 KB
/
stream_test.go
File metadata and controls
602 lines (536 loc) · 17 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package gnata_test
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"time"
"github.com/rbbydotdev/gnata-sqlite"
)
const streamTestData = `{
"data": {"action": "grant-access", "user_type": 2},
"metadata": {"is_admin": true}
}`
// TestStreamEvaluator_Compile verifies that Compile() assigns sequential indices
// and that each compiled expression evaluates to the expected result.
func TestStreamEvaluator_Compile(t *testing.T) {
tests := []struct {
name string
exprs []string
wantResults []any
}{
{
name: "single expression",
exprs: []string{`data.action = "grant-access"`},
wantResults: []any{true},
},
{
name: "sequential index assignment and correct evaluation",
exprs: []string{
`data.action = "grant-access"`,
`data.user_type = 2`,
`metadata.is_admin = true`,
},
wantResults: []any{true, true, true},
},
{
name: "mixed fast-path and complex expressions",
exprs: []string{
`data.user_type = 2`, // comparison fast path
`data.user_type > 1`, // full eval
`data.user_type`, // pure-path fast path
},
wantResults: []any{true, true, float64(2)},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
indices := make([]int, len(tc.exprs))
for i, src := range tc.exprs {
idx, err := se.Compile(src)
if err != nil {
t.Fatalf("Compile(%q): %v", src, err)
}
if idx != i {
t.Fatalf("expr %d: want index %d, got %d", i, i, idx)
}
indices[i] = idx
}
if se.Len() != len(tc.exprs) {
t.Fatalf("Len: want %d, got %d", len(tc.exprs), se.Len())
}
results, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema", indices)
if err != nil {
t.Fatalf("EvalMany: %v", err)
}
for i, want := range tc.wantResults {
if !gnata.DeepEqual(results[i], want) {
t.Errorf("result[%d]: want %v (%T), got %v (%T)",
i, want, want, results[i], results[i])
}
}
})
}
}
// TestStreamEvaluator_Add verifies that Add() accepts pre-compiled Expressions
// and that EvalOne returns the expected result for each expression type.
func TestStreamEvaluator_Add(t *testing.T) {
tests := []struct {
name string
expr string
wantResult any
}{
{"comparison fast path - string eq", `data.action = "grant-access"`, true},
{"comparison fast path - num eq", `data.user_type = 2`, true},
{"comparison fast path - bool eq", `metadata.is_admin = true`, true},
{"comparison fast path - string neq", `data.action != "other"`, true},
{"comparison fast path - num neq", `data.user_type != 99`, true},
{"pure-path fast path", `data.user_type`, float64(2)},
{"full eval - gt", `data.user_type > 1`, true},
{"full eval - and", `data.user_type = 2 and metadata.is_admin = true`, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
compiled, err := gnata.Compile(tc.expr)
if err != nil {
t.Fatalf("gnata.Compile(%q): %v", tc.expr, err)
}
se := gnata.NewStreamEvaluator(nil)
idx := se.Add(compiled)
if idx != 0 {
t.Fatalf("first Add: want index 0, got %d", idx)
}
got, err := se.EvalOne(context.Background(), json.RawMessage(streamTestData), "", idx)
if err != nil {
t.Fatalf("EvalOne: %v", err)
}
if !gnata.DeepEqual(got, tc.wantResult) {
t.Errorf("want %v (%T), got %v (%T)", tc.wantResult, tc.wantResult, got, got)
}
})
}
}
// TestStreamEvaluator_CacheInvalidation verifies that adding an expression after
// the cache is warm causes a cache miss on the next EvalMany call, and that the
// newly added expression evaluates correctly.
func TestStreamEvaluator_CacheInvalidation(t *testing.T) {
tests := []struct {
name string
initialExpr string
addedExpr string
wantMissCount int64
}{
{
name: "add after single warmup call",
initialExpr: `data.action = "grant-access"`,
addedExpr: `data.user_type = 2`,
wantMissCount: 2, // warmup miss + post-Add miss
},
{
name: "add complex expression after warmup",
initialExpr: `data.user_type = 2`,
addedExpr: `data.user_type > 1`,
wantMissCount: 2,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
idx0, err := se.Compile(tc.initialExpr)
if err != nil {
t.Fatalf("Compile initial: %v", err)
}
// Warm the cache.
if _, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema", []int{idx0}); err != nil {
t.Fatalf("warmup EvalMany: %v", err)
}
if got := se.Stats().Misses; got != 1 {
t.Fatalf("after warmup: want 1 miss, got %d", got)
}
idx1, err := se.Compile(tc.addedExpr)
if err != nil {
t.Fatalf("Compile added: %v", err)
}
// Add must have invalidated the cache — expect a miss.
results, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema", []int{idx0, idx1})
if err != nil {
t.Fatalf("post-Add EvalMany: %v", err)
}
for i, got := range results {
if got != true {
t.Errorf("result[%d]: want true, got %v", i, got)
}
}
if got := se.Stats().Misses; got != tc.wantMissCount {
t.Errorf("miss count: want %d, got %d", tc.wantMissCount, got)
}
})
}
}
// TestStreamEvaluator_IndexStability verifies that previously assigned indices
// continue to point to the correct expression after additional expressions are added.
func TestStreamEvaluator_IndexStability(t *testing.T) {
tests := []struct {
name string
seedExprs []string
extraCount int
wantAll bool // all seed expressions should evaluate to true with json.RawMessage(streamTestData)
}{
{
name: "stable after 1 extra add",
seedExprs: []string{`data.action = "grant-access"`, `data.user_type = 2`},
extraCount: 1,
wantAll: true,
},
{
name: "stable after 10 extra adds",
seedExprs: []string{`data.action = "grant-access"`, `data.user_type = 2`},
extraCount: 10,
wantAll: true,
},
{
name: "stable after 100 extra adds",
seedExprs: []string{`data.action = "grant-access"`, `data.user_type = 2`},
extraCount: 100,
wantAll: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
seedIndices := make([]int, len(tc.seedExprs))
for i, src := range tc.seedExprs {
idx, err := se.Compile(src)
if err != nil {
t.Fatalf("Compile seed[%d]: %v", i, err)
}
seedIndices[i] = idx
}
for i := range tc.extraCount {
_, _ = se.Compile(fmt.Sprintf(`data.user_type = %d`, i+1000))
}
wantLen := len(tc.seedExprs) + tc.extraCount
if se.Len() != wantLen {
t.Fatalf("Len: want %d, got %d", wantLen, se.Len())
}
results, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "", seedIndices)
if err != nil {
t.Fatalf("EvalMany: %v", err)
}
for i, got := range results {
if got != tc.wantAll {
t.Errorf("seed[%d] (index %d): want %v, got %v",
i, seedIndices[i], tc.wantAll, got)
}
}
})
}
}
// TestStreamEvaluator_Replace verifies that Replace swaps an expression in-place
// and that subsequent evaluations use the new expression.
func TestStreamEvaluator_Replace(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
idx, err := se.Compile(`data.action`)
if err != nil {
t.Fatalf("Compile: %v", err)
}
got, err := se.EvalOne(context.Background(), json.RawMessage(streamTestData), "", idx)
if err != nil {
t.Fatalf("EvalOne before replace: %v", err)
}
if got != "grant-access" {
t.Fatalf("before replace: want grant-access, got %v", got)
}
newExpr, _ := gnata.Compile(`data.user_type`)
if err := se.Replace(idx, newExpr); err != nil {
t.Fatalf("Replace: %v", err)
}
got, err = se.EvalOne(context.Background(), json.RawMessage(streamTestData), "", idx)
if err != nil {
t.Fatalf("EvalOne after replace: %v", err)
}
if !gnata.DeepEqual(got, float64(2)) {
t.Fatalf("after replace: want 2, got %v", got)
}
}
// TestStreamEvaluator_Remove verifies that Remove marks an expression as removed
// and that subsequent evaluations return nil for that index.
func TestStreamEvaluator_Remove(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
idx0, _ := se.Compile(`data.action = "grant-access"`)
idx1, _ := se.Compile(`data.user_type = 2`)
if err := se.Remove(idx0); err != nil {
t.Fatalf("Remove: %v", err)
}
results, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "", []int{idx0, idx1})
if err != nil {
t.Fatalf("EvalMany: %v", err)
}
if results[0] != nil {
t.Errorf("removed expr: want nil, got %v", results[0])
}
if results[1] != true {
t.Errorf("kept expr: want true, got %v", results[1])
}
}
// TestStreamEvaluator_Reset verifies that Reset clears all expressions.
func TestStreamEvaluator_Reset(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
if _, err := se.Compile(`data.action`); err != nil {
t.Fatal(err)
}
if _, err := se.Compile(`data.user_type`); err != nil {
t.Fatal(err)
}
if se.Len() != 2 {
t.Fatalf("before reset: want Len 2, got %d", se.Len())
}
se.Reset()
if se.Len() != 0 {
t.Fatalf("after reset: want Len 0, got %d", se.Len())
}
idx, err := se.Compile(`metadata.is_admin`)
if err != nil {
t.Fatal(err)
}
if idx != 0 {
t.Fatalf("after reset: first Add should get index 0, got %d", idx)
}
}
// TestStreamEvaluator_MetricsHook verifies that the MetricsHook receives
// OnEval, OnCacheHit, and OnCacheMiss callbacks.
func TestStreamEvaluator_MetricsHook(t *testing.T) {
hook := &testMetricsHook{}
se := gnata.NewStreamEvaluator(nil, gnata.WithMetricsHook(hook))
idx, _ := se.Compile(`data.action = "grant-access"`)
// First call → cache miss.
if _, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema1", []int{idx}); err != nil {
t.Fatal(err)
}
if hook.misses != 1 {
t.Errorf("want 1 miss, got %d", hook.misses)
}
if hook.evals != 1 {
t.Errorf("want 1 eval, got %d", hook.evals)
}
// Second call → cache hit.
if _, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema1", []int{idx}); err != nil {
t.Fatal(err)
}
if hook.hits != 1 {
t.Errorf("want 1 hit, got %d", hook.hits)
}
if hook.evals != 2 {
t.Errorf("want 2 evals, got %d", hook.evals)
}
if hook.fastPaths < 1 {
t.Errorf("want at least 1 fast-path eval, got %d", hook.fastPaths)
}
}
// testMetricsHook is a test-only stub; not goroutine-safe. The concurrent
// safety tests (TestStreamEvaluator_ConcurrentSafety) do not attach a hook,
// so unsynchronized counters are acceptable here.
type testMetricsHook struct {
evals int
fastPaths int
hits int
misses int
evictions int
}
func (h *testMetricsHook) OnEval(_ int, fastPath bool, _ time.Duration, _ error) {
h.evals++
if fastPath {
h.fastPaths++
}
}
func (h *testMetricsHook) OnCacheHit(_ string) { h.hits++ }
func (h *testMetricsHook) OnCacheMiss(_ string) { h.misses++ }
func (h *testMetricsHook) OnEviction() { h.evictions++ }
// TestStreamEvaluator_EvalMap verifies that EvalMap produces identical results
// to EvalMany for the same data and expressions.
func TestStreamEvaluator_EvalMap(t *testing.T) {
rawData := json.RawMessage(streamTestData)
var dataMap map[string]json.RawMessage
if err := json.Unmarshal(rawData, &dataMap); err != nil {
t.Fatalf("unmarshal to map: %v", err)
}
tests := []struct {
name string
expr string
want any
}{
{"path lookup", `data.action`, "grant-access"},
{"comparison", `data.user_type = 2`, true},
{"boolean field", `metadata.is_admin`, true},
{"greater than", `data.user_type > 1`, true},
{"nested path", `data.user_type`, float64(2)},
{"and expression", `data.user_type = 2 and metadata.is_admin = true`, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
idx, err := se.Compile(tc.expr)
if err != nil {
t.Fatalf("Compile(%q): %v", tc.expr, err)
}
evalManyResult, err := se.EvalMany(context.Background(), rawData, "", []int{idx})
if err != nil {
t.Fatalf("EvalMany: %v", err)
}
evalMapResult, err := se.EvalMap(context.Background(), dataMap, "", []int{idx})
if err != nil {
t.Fatalf("EvalMap: %v", err)
}
if !gnata.DeepEqual(evalManyResult[0], evalMapResult[0]) {
t.Errorf("EvalMany=%v (%T), EvalMap=%v (%T)",
evalManyResult[0], evalManyResult[0],
evalMapResult[0], evalMapResult[0])
}
if !gnata.DeepEqual(evalMapResult[0], tc.want) {
t.Errorf("want %v (%T), got %v (%T)",
tc.want, tc.want, evalMapResult[0], evalMapResult[0])
}
})
}
}
// TestStreamEvaluator_EvalMap_MultipleExprs verifies that EvalMap evaluates
// multiple expressions against the same pre-parsed data.
func TestStreamEvaluator_EvalMap_MultipleExprs(t *testing.T) {
var dataMap map[string]json.RawMessage
if err := json.Unmarshal(json.RawMessage(streamTestData), &dataMap); err != nil {
t.Fatalf("unmarshal: %v", err)
}
se := gnata.NewStreamEvaluator(nil)
idx0, _ := se.Compile(`data.action`)
idx1, _ := se.Compile(`data.user_type = 2`)
idx2, _ := se.Compile(`metadata.is_admin`)
results, err := se.EvalMap(context.Background(), dataMap, "schema", []int{idx0, idx1, idx2})
if err != nil {
t.Fatalf("EvalMap: %v", err)
}
if results[0] != "grant-access" {
t.Errorf("result[0]: want grant-access, got %v", results[0])
}
if results[1] != true {
t.Errorf("result[1]: want true, got %v", results[1])
}
if results[2] != true {
t.Errorf("result[2]: want true, got %v", results[2])
}
}
// TestStreamEvaluator_EvalMap_NilAndEmpty verifies edge cases.
func TestStreamEvaluator_EvalMap_NilAndEmpty(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
idx, _ := se.Compile(`foo`)
results, err := se.EvalMap(context.Background(), nil, "", []int{idx})
if err != nil {
t.Fatalf("EvalMap(nil): %v", err)
}
if results[0] != nil {
t.Errorf("nil map: want nil result, got %v", results[0])
}
results, err = se.EvalMap(context.Background(), map[string]json.RawMessage{}, "", []int{idx})
if err != nil {
t.Fatalf("EvalMap(empty): %v", err)
}
if results[0] != nil {
t.Errorf("empty map: want nil result, got %v", results[0])
}
results, err = se.EvalMap(context.Background(), map[string]json.RawMessage{}, "", nil)
if err != nil {
t.Fatalf("EvalMap(no indices): %v", err)
}
if results != nil {
t.Errorf("no indices: want nil results, got %v", results)
}
}
// TestStreamEvaluator_EvalMap_FastPaths verifies that EvalMap takes the same
// fast paths (pure path, comparison, function) as EvalMany, using MetricsHook
// to confirm fastPath=true.
func TestStreamEvaluator_EvalMap_FastPaths(t *testing.T) {
var dataMap map[string]json.RawMessage
if err := json.Unmarshal(json.RawMessage(streamTestData), &dataMap); err != nil {
t.Fatalf("unmarshal: %v", err)
}
tests := []struct {
name string
expr string
want any
}{
{"pure path", `data.action`, "grant-access"},
{"comparison", `data.user_type = 2`, true},
{"function $exists", `$exists(data.action)`, true},
{"nested path", `data.user_type`, float64(2)},
{"missing top-level key", `nonexistent.field`, nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
hook := &testMetricsHook{}
se := gnata.NewStreamEvaluator(nil, gnata.WithMetricsHook(hook))
idx, err := se.Compile(tc.expr)
if err != nil {
t.Fatalf("Compile(%q): %v", tc.expr, err)
}
results, err := se.EvalMap(context.Background(), dataMap, "s", []int{idx})
if err != nil {
t.Fatalf("EvalMap: %v", err)
}
if !gnata.DeepEqual(results[0], tc.want) {
t.Errorf("want %v (%T), got %v (%T)", tc.want, tc.want, results[0], results[0])
}
if tc.want != nil && hook.fastPaths == 0 {
t.Errorf("expected fast path for %q but MetricsHook reported 0 fast paths", tc.expr)
}
})
}
}
// TestStreamEvaluator_ConcurrentSafety exercises concurrent Add / Compile calls
// interleaved with EvalMany to surface data races. Run with -race.
func TestStreamEvaluator_ConcurrentSafety(t *testing.T) {
tests := []struct {
name string
writers int
readers int
opsEach int
}{
{"light load", 2, 2, 25},
{"heavy load", 4, 4, 50},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
se := gnata.NewStreamEvaluator(nil)
seed, _ := se.Compile(`data.action = "grant-access"`)
var wg sync.WaitGroup
for g := range tc.writers {
wg.Add(1)
go func(g int) {
defer wg.Done()
for i := range tc.opsEach {
_, _ = se.Compile(fmt.Sprintf(`data.user_type = %d`, g*1000+i))
}
}(g)
}
for range tc.readers {
wg.Go(func() {
for range tc.opsEach {
results, err := se.EvalMany(context.Background(), json.RawMessage(streamTestData), "schema", []int{seed})
if err != nil {
t.Errorf("EvalMany: %v", err)
return
}
if results[0] != true {
t.Errorf("seed result: want true, got %v", results[0])
}
}
})
}
wg.Wait()
wantMin := 1 + tc.writers*tc.opsEach
if se.Len() < wantMin {
t.Errorf("Len: want >= %d, got %d", wantMin, se.Len())
}
})
}
}