-
Notifications
You must be signed in to change notification settings - Fork 3
/
rule_test.go
58 lines (48 loc) · 1.06 KB
/
rule_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
package throttleplugin
import (
"testing"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/stretchr/testify/assert"
)
func TestNewRule(t *testing.T) {
fields := map[string]string{
"a": "1",
"b": "2",
"c": "3",
"d": "4",
"e": "5",
}
r := NewRule(fields, 100)
assert.Equal(t, []string{"a", "b", "c", "d", "e"}, r.keys)
assert.Equal(t, []string{"1", "2", "3", "4", "5"}, r.values)
}
func TestMatch(t *testing.T) {
event := &beat.Event{Fields: common.MapStr{}}
event.PutValue("a", "1")
event.PutValue("b", "2")
t.Run("match", func(t *testing.T) {
r := NewRule(map[string]string{"a": "1"}, 10)
ok, key := r.Match(event)
assert.True(t, ok)
assert.Equal(t, "10:1:", key)
})
}
func BenchmarkMatch(b *testing.B) {
fields := map[string]string{
"a": "1",
"b": "2",
"c": "3",
"d": "4",
"e": "5",
}
r := NewRule(fields, 100)
event := &beat.Event{Fields: common.MapStr{}}
event.PutValue("a", "1")
for k, v := range fields {
event.PutValue(k, v)
}
for i := 0; i < b.N; i++ {
r.Match(event)
}
}