-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_test.go
159 lines (133 loc) · 3.6 KB
/
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cache
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
redis "gopkg.in/redis.v3"
"github.com/kataras/iris"
// you could use that library now to do http testing: "github.com/kataras/iris/httptest"
)
const (
irisSrvWithMemoryStore = "127.0.0.1:1234"
irisSrvWithRedisStore = "127.0.0.1:1235"
redisService = "127.0.0.1:6379"
sleepTime = time.Duration(2) * time.Second
)
var cacheConfig = Config{
AutoRemove: false,
CacheTimeDuration: time.Duration(5) * time.Minute,
ContentType: ContentTypeJSON,
IrisGzipEnabled: false,
}
type dummy struct {
Name string `json:"name"`
}
func setupMemoryStoreIrisSrv() {
engine := iris.New()
c := NewCacheHF(cacheConfig, NewInMemoryStore())
engine.Use(c)
engine.Get("/json", func(ctx iris.Context) {
<-time.After(sleepTime)
ctx.JSON(dummy{"test"})
})
go engine.Run(iris.Addr(irisSrvWithMemoryStore), iris.WithoutStartupLog, iris.WithoutVersionChecker, iris.WithoutServerError(iris.ErrServerClosed))
<-time.After(time.Duration(1350 * time.Millisecond))
}
func setupRedisStoreIrisSrv() {
engine := iris.New()
redisClient := redis.NewClient(&redis.Options{Addr: redisService})
if err := redisClient.Ping().Err(); err != nil {
panic(err)
}
redisClient.FlushDb()
c := NewCacheHF(cacheConfig, NewRedisStore(redisClient))
engine.Use(c)
engine.Get("/json", func(ctx iris.Context) {
<-time.After(sleepTime)
ctx.JSON(dummy{"test"})
})
go engine.Run(iris.Addr(irisSrvWithRedisStore), iris.WithoutStartupLog, iris.WithoutVersionChecker, iris.WithoutServerError(iris.ErrServerClosed))
<-time.After(time.Duration(1350 * time.Millisecond))
}
func TestMain(m *testing.M) {
// spawn web servers
setupMemoryStoreIrisSrv()
setupRedisStoreIrisSrv()
// run the tests
os.Exit(m.Run())
}
func TestNonCachedMemoryStoreRequest(t *testing.T) {
if err := doRequest(irisSrvWithMemoryStore); err != nil {
t.Fatal(err)
return
}
}
func TestCachedMemoryStoreRequest(t *testing.T) {
s := time.Now()
if err := doRequest(irisSrvWithMemoryStore); err != nil {
t.Fatal(err)
return
}
// check if the request was slower than the sleep time
if time.Now().Sub(s) > sleepTime+200*time.Millisecond {
t.Fatal("cached request was slower than non cached request")
return
}
}
func TestNonCachedRedisStoreRequest(t *testing.T) {
if err := doRequest(irisSrvWithRedisStore); err != nil {
t.Fatal(err)
return
}
}
func TestCachedRedisStoreRequest(t *testing.T) {
s := time.Now()
if err := doRequest(irisSrvWithRedisStore); err != nil {
t.Fatal(err)
return
}
// check if the request was slower than the sleep time,
// these numbers are not always percise, especially in services like travis, so give it time.
if time.Now().Sub(s) > sleepTime+200*time.Millisecond {
t.Fatal("cached request was slower than non cached request")
return
}
}
func BenchmarkCachedMemoryStoreResponse(b *testing.B) {
for i := 0; i < b.N; i++ {
doRequestWithoutParsing(irisSrvWithMemoryStore)
}
}
func BenchmarkCachedRedisStoreResponse(b *testing.B) {
for i := 0; i < b.N; i++ {
doRequestWithoutParsing(irisSrvWithRedisStore)
}
}
func doRequest(url string) error {
res, err := http.Get(fmt.Sprintf("http://%s/json", url))
if err != nil {
return err
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
obj := &dummy{}
err = json.Unmarshal(buf, obj)
if err != nil {
return err
}
if obj.Name != "test" {
return errors.New("name doesn't match origin name")
}
return nil
}
func doRequestWithoutParsing(url string) error {
_, err := http.Get(fmt.Sprintf("http://%s/json", url))
return err
}