-
Notifications
You must be signed in to change notification settings - Fork 12
/
plan_test.go
79 lines (64 loc) · 1.31 KB
/
plan_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
package redfi
import (
"fmt"
"testing"
)
func TestSelectRule(t *testing.T) {
p := &Plan{
Rules: []*Rule{},
}
// // test ip matching
p.Rules = append(p.Rules, &Rule{
Delay: 1e3,
ClientAddr: "192.0.0.1:8001",
})
rule := p.SelectRule("192.0.0.1", []byte(""))
if rule == nil {
t.Fatal("rule must not be nil")
}
// test command matching
p.Rules = []*Rule{}
p.Rules = append(p.Rules, &Rule{
Delay: 1e3,
Command: "GET",
})
p.MarshalCommands()
rule = p.SelectRule("192.0.0.1", []byte("\r\nGET\r\nfff"))
if rule == nil {
t.Fatal("rule must not be nil")
}
rule = p.SelectRule("172.0.0.1", []byte("\r\nKEYS\r\nfff"))
if rule != nil {
fmt.Println(rule)
t.Fatal("rule must BE nil")
}
}
func TestAddDeleteGetRule(t *testing.T) {
p := NewPlan()
r := Rule{
Name: "clients_delay",
Delay: 50,
Percentage: 20,
}
p.AddRule(r)
if len(p.Rules) != 1 {
t.Fatal("rule wasn't added")
}
if !(p.Rules[0].Delay == r.Delay && p.Rules[0].Percentage == r.Percentage) {
t.Fatal("rule added doesn't match")
}
fetchedRule, err := p.GetRule("clients_delay")
if err != nil {
t.Fatal(err)
}
fmt.Println(fetchedRule)
err = p.DeleteRule("clients_delay")
if err != nil {
t.Fatal(err)
}
_, err = p.GetRule("clients_delay")
if err == nil {
t.Fatal(err)
}
fmt.Println(fetchedRule)
}