-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampling_test.go
More file actions
68 lines (60 loc) · 1.75 KB
/
sampling_test.go
File metadata and controls
68 lines (60 loc) · 1.75 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
package hc
import (
"fmt"
"testing"
"time"
)
func TestChainSampler(t *testing.T) {
s := ChainSampler(
NeverSampler(),
KeepErrors(),
KeepPathPrefix("/admin"),
KeepSlowerThan(250*time.Millisecond),
)
if !s(SampleInput{Path: "/admin/users", StatusCode: 200, Duration: 10 * time.Millisecond}) {
t.Fatal("expected admin path to be kept")
}
if !s(SampleInput{Path: "/api/orders", StatusCode: 500, Duration: 10 * time.Millisecond}) {
t.Fatal("expected 5xx request to be kept")
}
if !s(SampleInput{Path: "/api/orders", StatusCode: 200, Duration: 300 * time.Millisecond}) {
t.Fatal("expected slow request to be kept")
}
if s(SampleInput{Path: "/api/orders", StatusCode: 200, Duration: 10 * time.Millisecond}) {
t.Fatal("expected fast healthy request to be dropped")
}
}
func TestKeepSlowerThanNegativeDuration(t *testing.T) {
s := ChainSampler(NeverSampler(), KeepSlowerThan(-1*time.Second))
if !s(SampleInput{StatusCode: 200, Duration: 0}) {
t.Fatal("expected negative threshold to behave like zero")
}
}
func TestRateSamplerBounds(t *testing.T) {
if RateSampler(0)(SampleInput{}) {
t.Fatal("rate 0 should always drop")
}
if RateSampler(-1)(SampleInput{}) {
t.Fatal("negative rate should always drop")
}
if !RateSampler(1)(SampleInput{}) {
t.Fatal("rate 1 should always keep")
}
if !RateSampler(2)(SampleInput{}) {
t.Fatal("rate >1 should always keep")
}
}
func ExampleChainSampler() {
sampler := ChainSampler(
RateSampler(0),
KeepErrors(),
KeepPathPrefix("/checkout"),
)
fmt.Println(sampler(SampleInput{Path: "/catalog", StatusCode: 200}))
fmt.Println(sampler(SampleInput{Path: "/checkout/start", StatusCode: 200}))
fmt.Println(sampler(SampleInput{Path: "/catalog", StatusCode: 503}))
// Output:
// false
// true
// true
}