-
Notifications
You must be signed in to change notification settings - Fork 5
/
caching_test.go
101 lines (90 loc) · 2.23 KB
/
caching_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
package gbox
import (
"context"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestCaching_Cleanup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := &Caching{
ctxBackground: ctx,
ctxBackgroundCancel: cancel,
StoreDsn: "test",
logger: zap.NewNop(),
}
_, loaded := cachingStores.LoadOrStore(c.StoreDsn, "b")
require.False(t, loaded)
require.NoError(t, ctx.Err())
require.NoError(t, c.Cleanup())
require.Error(t, ctx.Err())
_, loaded = cachingStores.LoadOrStore(c.StoreDsn, "b")
require.False(t, loaded)
}
func TestCaching_Validate(t *testing.T) {
testCases := map[string]struct {
caching *Caching
expectedErrorMsg string
}{
"valid_rules_without_varies": {
caching: &Caching{
Rules: CachingRules{
"default": &CachingRule{
MaxAge: 1,
},
},
},
},
"valid_rules_with_varies": {
caching: &Caching{
Varies: map[string]*CachingVary{
"test": {},
},
Rules: CachingRules{
"default": &CachingRule{
MaxAge: 1,
Varies: []string{"test"},
},
},
},
},
"invalid_rules_max_age": {
expectedErrorMsg: "caching rule default, max age must greater than zero",
caching: &Caching{
Rules: CachingRules{
"default": &CachingRule{},
},
},
},
"rules_vary_name_not_exist": {
expectedErrorMsg: "caching rule default, configured vary: test does not exist",
caching: &Caching{
Rules: CachingRules{
"default": &CachingRule{
MaxAge: 1,
Varies: []string{"test"},
},
},
},
},
}
for name, testCase := range testCases {
err := testCase.caching.Validate()
if testCase.expectedErrorMsg != "" {
require.Errorf(t, err, "case %s: expected error but not", name)
require.Equalf(t, testCase.expectedErrorMsg, err.Error(), "case %s: unexpected error message", name)
} else {
require.NoErrorf(t, err, "case %s: should not error", name)
}
}
}
func TestCaching_Provision(t *testing.T) {
c := &Caching{
StoreDsn: "redis://test",
}
require.NoError(t, c.Provision(caddy.Context{}))
require.NotNil(t, c.store)
require.NotNil(t, c.ctxBackground)
require.NotNil(t, c.ctxBackgroundCancel)
}