master
go 78 lines 1.81 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package chartengine
4
5 import (
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9
10 "github.com/netdata/netdata/go/plugins/pkg/metrix"
11 )
12
13 func TestRouteCacheScenarios(t *testing.T) {
14 tests := map[string]struct {
15 run func(t *testing.T)
16 }{
17 "stores positive and negative routes": {
18 run: func(t *testing.T) {
19 cache := newRouteCache()
20
21 a := metrix.SeriesIdentity{ID: "a", Hash64: 1}
22 b := metrix.SeriesIdentity{ID: "b", Hash64: 1}
23
24 cache.Store(a, 1, 1, []routeBinding{{ChartID: "ca"}})
25 cache.Store(b, 1, 1, nil) // negative-cache entry
26
27 routesA, ok := cache.Lookup(a, 1, 1)
28 assert.True(t, ok)
29 assert.Equal(t, "ca", routesA[0].ChartID)
30
31 routesB, ok := cache.Lookup(b, 1, 1)
32 assert.True(t, ok)
33 assert.Empty(t, routesB)
34 },
35 },
36 "retain seen prunes by build sequence": {
37 run: func(t *testing.T) {
38 cache := newRouteCache()
39
40 a := metrix.SeriesIdentity{ID: "a", Hash64: 10}
41 b := metrix.SeriesIdentity{ID: "b", Hash64: 11}
42 c := metrix.SeriesIdentity{ID: "c", Hash64: 12}
43
44 cache.Store(a, 1, 1, []routeBinding{{ChartID: "ca"}})
45 cache.Store(b, 1, 1, []routeBinding{{ChartID: "cb"}})
46 cache.Store(c, 1, 1, nil)
47
48 cache.MarkSeenIfPresent(a, 2)
49 cache.MarkSeenIfPresent(c, 2)
50 cache.RetainSeen(2)
51
52 _, ok := cache.Lookup(a, 1, 2)
53 assert.True(t, ok)
54
55 _, ok = cache.Lookup(b, 1, 2)
56 assert.False(t, ok)
57
58 _, ok = cache.Lookup(c, 1, 2)
59 assert.True(t, ok)
60 },
61 },
62 "lookup misses on revision change": {
63 run: func(t *testing.T) {
64 cache := newRouteCache()
65 id := metrix.SeriesIdentity{ID: "a", Hash64: 1}
66
67 cache.Store(id, 1, 1, []routeBinding{{ChartID: "ca"}})
68
69 _, ok := cache.Lookup(id, 2, 2)
70 assert.False(t, ok)
71 },
72 },
73 }
74
75 for name, tc := range tests {
76 t.Run(name, tc.run)
77 }
78 }