-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_queue_test.go
118 lines (101 loc) · 2.3 KB
/
map_queue_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
//go:build bpftests
// +build bpftests
package gobpfld
import (
"math/rand"
"testing"
"github.com/dylandreimerink/gobpfld/bpftypes"
"github.com/dylandreimerink/gobpfld/kernelsupport"
)
func TestQueueMap(t *testing.T) {
if !kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapQueue) {
t.Skip("skipping test, current kernel version has no support for queue maps")
}
const maxEntries = 20
queue := QueueMap{
AbstractMap: AbstractMap{
Name: MustNewObjName("test"),
Definition: BPFMapDef{
Type: bpftypes.BPF_MAP_TYPE_QUEUE,
KeySize: 0,
ValueSize: sizeOfUint64,
MaxEntries: maxEntries,
},
},
}
err := queue.Load()
if err != nil {
t.Fatal(err)
}
defer queue.Close()
validationMap := make([]int64, maxEntries)
for i := 0; i < maxEntries; i++ {
val := rand.Int63()
err = queue.Enqueue(&val)
validationMap[i] = int64(val)
if err != nil {
t.Fatal(err)
}
}
var v int64
err = queue.Peek(&v)
if err != nil {
t.Fatal(err)
}
if v != validationMap[0] {
t.Fatalf("invalid peek, got: %d, expected: %d", v, validationMap[0])
}
for i := 0; i >= maxEntries; i-- {
var v int64
err = queue.Dequeue(&v)
if err != nil {
t.Fatal(err)
}
if v != validationMap[i] {
t.Fatalf("invalid dequeue, got: %d, expected: %d", v, validationMap[i])
}
}
}
func TestQueueMapIterator(t *testing.T) {
if !kernelsupport.CurrentFeatures.Map.Has(kernelsupport.KFeatMapQueue) {
t.Skip("skipping test, current kernel version has no support for queue maps")
}
const maxEntries = 20
queue := QueueMap{
AbstractMap: AbstractMap{
Name: MustNewObjName("test"),
Definition: BPFMapDef{
Type: bpftypes.BPF_MAP_TYPE_QUEUE,
KeySize: 0,
ValueSize: sizeOfUint64,
MaxEntries: maxEntries,
},
},
}
err := queue.Load()
if err != nil {
t.Fatal(err)
}
defer queue.Close()
validationMap := make([]int64, maxEntries)
for i := 0; i < maxEntries; i++ {
val := rand.Int63()
err = queue.Enqueue(&val)
validationMap[i] = int64(val)
if err != nil {
t.Fatal(err)
}
}
i := 0
var v int64
err = MapIterForEach(queue.Iterator(), nil, &v, func(_, _ interface{}) error {
if v != validationMap[i] {
t.Fatalf("invalid dequeue, got: %d, expected: %d", v, validationMap[i])
}
i++
return nil
})
if err != nil {
t.Fatal(err)
}
}