-
Notifications
You must be signed in to change notification settings - Fork 16
/
route_cache_test.go
92 lines (70 loc) · 1.78 KB
/
route_cache_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
package rux
import (
"fmt"
"testing"
"github.com/gookit/goutil/testutil/assert"
)
func TestCachedRoutes_Delete(t *testing.T) {
is := assert.New(t)
c := NewCachedRoutes(3)
c.Set("cache1", NewRoute("/cache1", nil))
c.Delete("cache1")
is.Eq(0, c.Len())
is.False(c.Delete("cache2"))
c.hashMap = nil
is.False(c.Delete("cache1"))
}
func TestCachedRoutes_Has(t *testing.T) {
is := assert.New(t)
c := NewCachedRoutes(3)
c.Set("cache1", NewRoute("/cache1", nil))
is.True(c.Has("cache1"))
is.False(c.Has("not-exists"))
}
func TestCacheRoutes(t *testing.T) {
is := assert.New(t)
r := New(CachingWithNum(3))
r.GET("/cache1/{id}", func(c *Context) {})
r.GET("/cache2/{id}", func(c *Context) {})
r.GET("/cache3/{id}", func(c *Context) {
c.WriteString("cache3")
})
r.GET("/cache4/{id}", func(c *Context) {
c.WriteString("cache4")
})
w1 := mockRequest(r, "GET", "/cache3/1234", nil)
is.Eq("cache3", w1.Body.String())
w2 := mockRequest(r, "GET", "/cache4/1234", nil)
is.Eq("cache4", w2.Body.String())
is.Eq(2, r.cachedRoutes.Len())
}
func TestCachedRoutes_Set(t *testing.T) {
is := assert.New(t)
c := NewCachedRoutes(3)
ok := c.Set("cache1", NewRoute("/cache1", nil))
is.True(ok)
is.Eq(1, c.Len())
// repeat set same key
ok = c.Set("cache1", NewRoute("/cache1", nil))
is.True(ok)
is.Eq(1, c.Len())
// test delete elements
cr := NewCachedRoutes(3)
for i := 0; i < 5; i++ {
key := fmt.Sprint("key", i)
cr.Set(key, NewRoute("/"+key, emptyHandler))
}
is.Eq(3, cr.Len())
}
func TestCachedRoutes_Get(t *testing.T) {
is := assert.New(t)
c := NewCachedRoutes(3)
ok := c.Set("cache1", NewRoute("/cache1", nil))
if is.True(ok).IsOk() {
is.True(c.Has("cache1"))
route, ok := c.Get("cache1")
is.True(ok)
is.Eq("/cache1", route.Path())
}
is.Nil(c.Get("not-exists"))
}