-
Notifications
You must be signed in to change notification settings - Fork 2
/
derive_test.go
183 lines (155 loc) · 4.31 KB
/
derive_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package statedb
import (
"context"
"log/slog"
"slices"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/hivetest"
"github.com/cilium/hive/job"
"github.com/cilium/statedb/index"
"github.com/cilium/statedb/part"
)
type derived struct {
ID uint64
Deleted bool
}
var derivedIdIndex = Index[derived, uint64]{
Name: "id",
FromObject: func(t derived) index.KeySet {
return index.NewKeySet(index.Uint64(t.ID))
},
FromKey: index.Uint64,
Unique: true,
}
type nopHealth struct {
}
// Degraded implements cell.Health.
func (*nopHealth) Degraded(reason string, err error) {
}
// NewScope implements cell.Health.
func (h *nopHealth) NewScope(name string) cell.Health {
return h
}
// OK implements cell.Health.
func (*nopHealth) OK(status string) {
}
// Stopped implements cell.Health.
func (*nopHealth) Stopped(reason string) {
}
func (*nopHealth) Close() {}
func newNopHealth() (cell.Health, *nopHealth) {
h := &nopHealth{}
return h, h
}
var _ cell.Health = &nopHealth{}
func TestDerive(t *testing.T) {
var db *DB
inTable, err := NewTable("test", idIndex)
require.NoError(t, err)
outTable, err := NewTable("derived", derivedIdIndex)
require.NoError(t, err)
transform := func(obj testObject, deleted bool) (derived, DeriveResult) {
t.Logf("transform(%v, %v)", obj, deleted)
tags := slices.Collect(obj.Tags.All())
if obj.Tags.Len() > 0 && tags[0] == "skip" {
return derived{}, DeriveSkip
}
if deleted {
if obj.Tags.Len() > 0 && tags[0] == "delete" {
return derived{ID: obj.ID}, DeriveDelete
}
return derived{ID: obj.ID, Deleted: true}, DeriveUpdate
}
return derived{ID: obj.ID, Deleted: false}, DeriveInsert
}
h := hive.New(
Cell, // DB
job.Cell,
cell.Provide(newNopHealth),
cell.Module(
"test", "Test",
cell.Provide(func(db_ *DB) (Table[testObject], RWTable[derived], error) {
db = db_
if err := db.RegisterTable(inTable); err != nil {
return nil, nil, err
}
if err := db.RegisterTable(outTable); err != nil {
return nil, nil, err
}
return inTable, outTable, nil
}),
cell.Invoke(Derive("testObject-to-derived", transform)),
),
)
log := hivetest.Logger(t, hivetest.LogLevel(slog.LevelError))
require.NoError(t, h.Start(log, context.TODO()), "Start")
getDerived := func() []derived {
txn := db.ReadTxn()
objs := Collect(outTable.All(txn))
// Log so we can trace the failed eventually calls
t.Logf("derived: %+v", objs)
return objs
}
// Insert 1, 2 and 3 (skipped) and validate.
wtxn := db.WriteTxn(inTable)
_, _, err = inTable.Insert(wtxn, testObject{ID: 1})
require.NoError(t, err, "Insert failed")
_, _, err = inTable.Insert(wtxn, testObject{ID: 2})
require.NoError(t, err, "Insert failed")
_, _, err = inTable.Insert(wtxn, testObject{ID: 3, Tags: part.NewSet("skip")})
require.NoError(t, err, "Insert failed")
wtxn.Commit()
require.Eventually(t,
func() bool {
objs := getDerived()
return len(objs) == 2 && // 3 is skipped
objs[0].ID == 1 && objs[1].ID == 2
},
time.Second,
10*time.Millisecond,
"expected 1 & 2 to be derived",
)
// Delete 2 (testing DeriveUpdate)
wtxn = db.WriteTxn(inTable)
_, hadOld, err := inTable.Delete(wtxn, testObject{ID: 2})
require.NoError(t, err, "Delete failed")
require.True(t, hadOld, "Expected object to be deleted")
wtxn.Commit()
require.Eventually(t,
func() bool {
objs := getDerived()
return len(objs) == 2 && // 3 is skipped
objs[0].ID == 1 && !objs[0].Deleted &&
objs[1].ID == 2 && objs[1].Deleted
},
time.Second,
10*time.Millisecond,
"expected 1 & 2, with 2 marked deleted",
)
// Delete 1 (testing DeriveDelete)
wtxn = db.WriteTxn(inTable)
_, _, err = inTable.Insert(wtxn, testObject{ID: 1, Tags: part.NewSet("delete")})
require.NoError(t, err, "Insert failed")
wtxn.Commit()
wtxn = db.WriteTxn(inTable)
_, _, err = inTable.Delete(wtxn, testObject{ID: 1})
require.NoError(t, err, "Delete failed")
wtxn.Commit()
require.Eventually(t,
func() bool {
objs := getDerived()
return len(objs) == 1 &&
objs[0].ID == 2 && objs[0].Deleted
},
time.Second,
10*time.Millisecond,
"expected 1 to be gone, and 2 mark deleted",
)
require.NoError(t, h.Stop(log, context.TODO()), "Stop")
}