-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark_test.go
More file actions
376 lines (342 loc) · 8.72 KB
/
Copy pathmark_test.go
File metadata and controls
376 lines (342 loc) · 8.72 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
package pmark
import "testing"
func TestPreferProcessValueGenerationBeatsTimestamp(t *testing.T) {
old := ProcessValue{
HasMark: true,
Generation: 2,
Mark: 20,
Timestamp: 10,
}
next := ProcessValue{
HasMark: true,
Generation: 1,
Mark: 10,
Timestamp: 20,
}
got := preferProcessValue(old, next)
if got != old {
t.Fatalf("preferProcessValue() = %+v, want newer generation %+v", got, old)
}
}
func TestPreferProcessValueTombstoneBeatsGeneration(t *testing.T) {
old := ProcessValue{
Tombstone: true,
Generation: 1,
Timestamp: 10,
}
next := ProcessValue{
HasMark: true,
Generation: 2,
Timestamp: 20,
}
got := preferProcessValue(old, next)
if got != old {
t.Fatalf("preferProcessValue() = %+v, want tombstone %+v", got, old)
}
}
func TestPreferProcessValuePriorityBeatsInheritanceAndTimestamp(t *testing.T) {
old := ProcessValue{
Inheritance: true,
HasMark: true,
Priority: 1,
Generation: 2,
Mark: 20,
Timestamp: 10,
}
next := ProcessValue{
HasMark: true,
Priority: 2,
Generation: 2,
Mark: 10,
Timestamp: 1,
}
got := preferProcessValue(old, next)
if got != next {
t.Fatalf("preferProcessValue() = %+v, want higher priority %+v", got, next)
}
}
func TestCanInheritRequiresLiveMark(t *testing.T) {
cases := []struct {
name string
value ProcessValue
want bool
}{
{
name: "live mark",
value: ProcessValue{HasMark: true},
want: true,
},
{
name: "live no mark",
value: ProcessValue{HasMark: false},
want: false,
},
{
name: "tombstone with mark",
value: ProcessValue{Tombstone: true, HasMark: true},
want: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := canInherit(tc.value); got != tc.want {
t.Fatalf("canInherit() = %v, want %v", got, tc.want)
}
})
}
}
func TestCanInheritCurrentGenerationRejectsOldGeneration(t *testing.T) {
m := &marker{generation: 3}
cases := []struct {
name string
value ProcessValue
want bool
}{
{
name: "current generation live mark",
value: ProcessValue{HasMark: true, Generation: 3},
want: true,
},
{
name: "old generation live mark",
value: ProcessValue{HasMark: true, Generation: 2},
want: false,
},
{
name: "current generation no mark",
value: ProcessValue{HasMark: false, Generation: 3},
want: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := m.canInheritCurrentGeneration(tc.value); got != tc.want {
t.Fatalf("canInheritCurrentGeneration() = %v, want %v", got, tc.want)
}
})
}
}
func TestUpdateHooksReplaysExistingLiveProcessUpdates(t *testing.T) {
liveMarked := ProcessKey{Tgid: 101, StartTime: 1001}
liveUnmarked := ProcessKey{Tgid: 102, StartTime: 1002}
tombstone := ProcessKey{Tgid: 103, StartTime: 1003}
m := &marker{
mirror: map[ProcessKey]ProcessValue{
liveMarked: {HasMark: true},
liveUnmarked: {HasMark: false},
tombstone: {Tombstone: true, HasMark: true},
},
}
seen := make(map[ProcessKey]ProcessValue)
m.updateHooks(Callbacks{
ProcessUpdate: func(update ProcessUpdate) {
seen[update.Key] = update.Value
},
})
if len(seen) != 2 {
t.Fatalf("replayed updates = %d, want 2", len(seen))
}
if _, ok := seen[liveMarked]; !ok {
t.Fatalf("missing replay for live marked entry")
}
if _, ok := seen[liveUnmarked]; !ok {
t.Fatalf("missing replay for live unmarked entry")
}
if _, ok := seen[tombstone]; ok {
t.Fatalf("replayed tombstone entry")
}
}
func TestForceProcessTraversalKeepsGeneration(t *testing.T) {
checkCalls := 0
d := &Daemon{
marker: &marker{
mirror: make(map[ProcessKey]ProcessValue),
check: func(ProcessInfo) (int8, uint64, bool) { checkCalls++; return 0, 0, false },
generation: 7,
},
}
if err := d.ForceProcessTraversal(); err != nil {
t.Fatalf("ForceProcessTraversal() error = %v", err)
}
if d.marker.generation != 7 {
t.Fatalf("generation = %d, want 7", d.marker.generation)
}
if checkCalls == 0 {
t.Fatalf("checker was not called")
}
}
func TestForceBumpGenerationKeepsCheckerAndTraverses(t *testing.T) {
checkCalls := 0
d := &Daemon{
marker: &marker{
mirror: make(map[ProcessKey]ProcessValue),
check: func(ProcessInfo) (int8, uint64, bool) { checkCalls++; return 0, 0, false },
generation: 7,
},
}
generation, err := d.ForceBumpGeneration()
if err != nil {
t.Fatalf("ForceBumpGeneration() error = %v", err)
}
if generation != 8 {
t.Fatalf("returned generation = %d, want 8", generation)
}
if d.marker.generation != 8 {
t.Fatalf("generation = %d, want 8", d.marker.generation)
}
if checkCalls == 0 {
t.Fatalf("checker was not called")
}
}
func TestSetCheckerReturnsResultingGeneration(t *testing.T) {
checkCalls := 0
d := &Daemon{
marker: &marker{
mirror: make(map[ProcessKey]ProcessValue),
check: func(ProcessInfo) (int8, uint64, bool) { return 0, 0, false },
generation: 7,
},
}
generation, err := d.SetChecker(func(ProcessInfo) (int8, uint64, bool) {
checkCalls++
return 0, 0, false
})
if err != nil {
t.Fatalf("SetChecker() error = %v", err)
}
if generation != 8 {
t.Fatalf("returned generation = %d, want 8", generation)
}
if d.marker.generation != 8 {
t.Fatalf("generation = %d, want 8", d.marker.generation)
}
if checkCalls == 0 {
t.Fatalf("new checker was not called")
}
}
func TestSetProcessMarkWritesExplicitCurrentGenerationMark(t *testing.T) {
key := ProcessKey{Tgid: 301, StartTime: 3001}
d := &Daemon{
marker: &marker{
mirror: make(map[ProcessKey]ProcessValue),
generation: 9,
},
}
var seen *ProcessUpdate
d.marker.callbacks.ProcessUpdate = func(update ProcessUpdate) {
seen = &update
}
d.SetProcessMark(key, 4, 1234)
value, ok := d.marker.mirror[key]
if !ok {
t.Fatalf("missing explicit mark")
}
if value.Tombstone {
t.Fatalf("Tombstone = true, want false")
}
if !value.HasMark {
t.Fatalf("HasMark = false, want true")
}
if !value.Inheritance {
t.Fatalf("Inheritance = false, want true for explicit mark")
}
if value.Priority != 4 {
t.Fatalf("Priority = %d, want 4", value.Priority)
}
if value.Mark != 1234 {
t.Fatalf("Mark = %d, want 1234", value.Mark)
}
if value.Generation != 9 {
t.Fatalf("Generation = %d, want 9", value.Generation)
}
if value.Timestamp == 0 {
t.Fatalf("Timestamp = 0, want current boot timestamp")
}
if seen == nil {
t.Fatalf("missing ProcessUpdate")
}
if seen.Key != key || seen.Value != value {
t.Fatalf("ProcessUpdate = %+v, want key=%+v value=%+v", *seen, key, value)
}
}
func TestSetProcessMarkUsesProcessValueMergeRules(t *testing.T) {
key := ProcessKey{Tgid: 302, StartTime: 3002}
old := ProcessValue{
HasMark: true,
Priority: 10,
Generation: 9,
Mark: 9000,
Timestamp: 1,
}
d := &Daemon{
marker: &marker{
mirror: map[ProcessKey]ProcessValue{
key: old,
},
generation: 9,
},
}
updates := 0
d.marker.callbacks.ProcessUpdate = func(ProcessUpdate) {
updates++
}
d.SetProcessMark(key, 1, 1000)
if got := d.marker.mirror[key]; got != old {
t.Fatalf("mirror value = %+v, want existing higher-priority value %+v", got, old)
}
if updates != 0 {
t.Fatalf("ProcessUpdate calls = %d, want 0 for unchanged merge winner", updates)
}
}
func TestCurrentGeneration(t *testing.T) {
d := &Daemon{
marker: &marker{generation: 42},
}
if got := d.CurrentGeneration(); got != 42 {
t.Fatalf("CurrentGeneration() = %d, want 42", got)
}
}
func TestStopReplaysAllProcessUpdatesAsUnmarkedCopies(t *testing.T) {
marked := ProcessKey{Tgid: 201, StartTime: 2001}
unmarked := ProcessKey{Tgid: 202, StartTime: 2002}
unmarkedTombstone := ProcessKey{Tgid: 203, StartTime: 2003}
done := make(chan error, 1)
done <- nil
d := &Daemon{
done: done,
marker: &marker{
mirror: map[ProcessKey]ProcessValue{
marked: {HasMark: true},
unmarked: {HasMark: false},
unmarkedTombstone: {Tombstone: true, HasMark: false},
},
callbacks: Callbacks{
ProcessUpdate: func(update ProcessUpdate) {},
},
},
}
seen := make(map[ProcessKey]ProcessValue)
d.marker.callbacks.ProcessUpdate = func(update ProcessUpdate) {
seen[update.Key] = update.Value
}
if err := d.Stop(); err != nil {
t.Fatalf("Stop() error = %v", err)
}
if len(seen) != 3 {
t.Fatalf("replayed updates = %d, want 3", len(seen))
}
if value, ok := seen[marked]; !ok {
t.Fatalf("missing replay for marked entry")
} else if value.HasMark {
t.Fatalf("marked replay HasMark = true, want false")
}
if _, ok := seen[unmarked]; !ok {
t.Fatalf("missing replay for unmarked live entry")
}
if _, ok := seen[unmarkedTombstone]; !ok {
t.Fatalf("missing replay for unmarked tombstone entry")
}
if !d.marker.mirror[marked].HasMark {
t.Fatalf("Stop mutated marked mirror entry")
}
}