This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadshed_test.go
123 lines (110 loc) · 2.72 KB
/
loadshed_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
package loadshed
import (
"sync/atomic"
"testing"
"time"
"github.com/asecurityteam/rolling"
)
func TestCPUOption(t *testing.T) {
var o = CPU(50, 80, time.Second, 10)
var l = &Loadshed{}
l = o(l)
if len(l.aggregators) != 1 {
t.Fatal("cpu option did not add aggregate")
}
}
func TestConcurrencyOption(t *testing.T) {
var o = Concurrency(5000, 10000, nil)
var m = &Loadshed{}
m = o(m)
if len(m.aggregators) != 1 {
t.Fatal("concurrency option did not add aggregate")
}
if len(m.chain) != 1 {
t.Fatal("percentile latency option did not add chain")
}
}
func TestAverageLatencyOption(t *testing.T) {
var o = AverageLatency(.1, 1.0, time.Millisecond, 1000, 0, 0)
var m = &Loadshed{}
m = o(m)
if len(m.aggregators) != 1 {
t.Fatal("avg latency option did not add aggregate")
}
if len(m.chain) != 1 {
t.Fatal("percentile latency option did not add chain")
}
}
func TestPercentileLatencyOption(t *testing.T) {
var o = PercentileLatency(.1, 1.0, time.Millisecond, 1000, 0, 0, 99.9)
var m = &Loadshed{}
m = o(m)
if len(m.aggregators) != 1 {
t.Fatal("percentile latency option did not add aggregate")
}
if len(m.chain) != 1 {
t.Fatal("percentile latency option did not add chain")
}
}
func TestLoadshedAggregator(t *testing.T) {
var w = rolling.NewPointWindow(1)
var a = rolling.NewSumRollup(w, "")
var o = Aggregator(a)
var m = &Loadshed{}
m = o(m)
if len(m.aggregators) != 1 {
t.Fatal("percentile latency option did not add aggregate")
}
if m.aggregators[0] != a {
t.Fatalf("aggregator options installed wrong aggregate: %t", m.aggregators[0])
}
}
func TestErrorRateOption(t *testing.T) {
var o = ErrorRate(50, 75, time.Millisecond, 10, 10, 1)
var m = &Loadshed{}
m = o(m)
if len(m.aggregators) != 1 {
t.Fatal("percentile latency option did not add aggregate")
}
if len(m.chain) != 1 {
t.Fatal("percentile latency option did not add chain")
}
}
func TestLoadshed(t *testing.T) {
var option = &fakeOption{}
var l = New(option.Option())
if option.Counter != 1 {
t.Fatal("Option not installed")
}
var e = l.Do(func() error { return nil })
if e != nil {
t.Fatalf("Unexpected error %s", e)
}
}
func TestLoadshedRequestRejected(t *testing.T) {
var option = &fakeOption{err: true}
var l = New(option.Option())
var e = l.Do(func() error { return nil })
switch e.(type) {
case Rejected:
//pass
default:
t.Fatal("Did not get expected error")
}
}
type fakeOption struct {
Counter int32
err bool
}
func (f *fakeOption) Option() Option {
return func(m *Loadshed) *Loadshed {
var w = rolling.NewPointWindow(1)
var zeroAggregator = rolling.NewSumRollup(w, "Zero")
if f.err {
w.Feed(1)
}
m.aggregators = append(m.aggregators, zeroAggregator)
atomic.AddInt32(&f.Counter, 1)
return m
}
}