-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver_interceptor_test.go
219 lines (177 loc) · 4.27 KB
/
server_interceptor_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package gogo
import (
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"github.com/dolab/gogo/pkgs/interceptors"
"github.com/dolab/httptesting"
"github.com/golib/assert"
)
type stubInterceptor struct {
name string
apply interceptors.Interceptor
}
func (stub *stubInterceptor) Name() string {
return stub.name
}
func (stub *stubInterceptor) Config() []byte {
return nil
}
func (stub *stubInterceptor) Priority() int {
return 0
}
func (stub *stubInterceptor) Register(config interceptors.Configer) (interceptors.Interceptor, error) {
return stub.apply, nil
}
func (stub *stubInterceptor) Reload(config interceptors.Configer) error {
return nil
}
func (stub *stubInterceptor) Shutdown() error {
return nil
}
type testService struct {
triggered int64
counter int64
v1 Grouper
}
func (svc *testService) Init(config Configer, group Grouper) {
svc.triggered = 0
svc.v1 = group
}
func (svc *testService) Middlewares() {
svc.v1.Use(func(ctx *Context) {
atomic.AddInt64(&svc.counter, 1)
ctx.Next()
})
}
func (svc *testService) Resources() {
svc.v1.GET("/server/service", func(ctx *Context) {
ctx.AddHeader("x-gogo-interceptor", strings.Join(ctx.Request.Header["X-Gogo-Interceptor"], ","))
ctx.Text("Hello, service!")
})
}
func (svc *testService) RequestReceived() []interceptors.Interface {
return []interceptors.Interface{
&stubInterceptor{
name: "request_receved@testing",
apply: func(w http.ResponseWriter, r *http.Request) bool {
r.Header.Add("x-gogo-interceptor", "Received")
atomic.AddInt64(&svc.triggered, 1)
return true
},
},
}
}
func (svc *testService) RequestRouted() []interceptors.Interface {
return []interceptors.Interface{
&stubInterceptor{
name: "request_routed@testing",
apply: func(w http.ResponseWriter, r *http.Request) bool {
r.Header.Add("x-gogo-interceptor", "Routed")
atomic.AddInt64(&svc.triggered, 1)
return true
},
},
}
}
func (svc *testService) ResponseReady() []interceptors.Interface {
return []interceptors.Interface{
&stubInterceptor{
name: "response_ready@testing",
apply: func(w http.ResponseWriter, r *http.Request) bool {
r.Header.Add("x-gogo-interceptor", "Ready")
atomic.AddInt64(&svc.triggered, 1)
return true
},
},
}
}
func (svc *testService) ResponseAlways() []interceptors.Interface {
return []interceptors.Interface{
&stubInterceptor{
name: "response_always@testing",
apply: func(w http.ResponseWriter, r *http.Request) bool {
r.Header.Add("x-gogo-interceptor", "Always")
atomic.AddInt64(&svc.triggered, 1)
return true
},
},
}
}
func Test_Server_NewService(t *testing.T) {
it := assert.New(t)
service := &testService{}
server := fakeServer()
server.NewService(service)
go server.Run()
for {
if len(server.Address()) > 0 {
break
}
}
client := httptesting.New(server.Address(), false)
request := client.New(t)
request.Get("/server/service", nil)
request.AssertOK()
request.AssertHeader("x-gogo-interceptor", "Received,Routed")
request.AssertContains("Hello, service!")
it.EqualValues(1, service.counter)
it.EqualValues(4, service.triggered)
}
func Test_Server_NewServiceWithConcurrency(t *testing.T) {
it := assert.New(t)
service := &testService{}
server := fakeServer()
server.NewService(service)
go server.Run()
for {
if len(server.Address()) > 0 {
break
}
}
client := httptesting.New(server.Address(), false)
var (
max = 10
wg sync.WaitGroup
)
wg.Add(max)
for i := 0; i < max; i++ {
go func() {
defer wg.Done()
request := client.New(t)
request.Get("/server/service", nil)
request.AssertOK()
request.AssertHeader("x-gogo-interceptor", "Received,Routed")
request.AssertContains("Hello, service!")
}()
}
wg.Wait()
it.EqualValues(1*max, service.counter)
it.EqualValues(4*max, service.triggered)
}
var benchmarkServiceOnce sync.Once
func Benchmark_Server_Service(b *testing.B) {
service := &testService{}
server := fakeServer()
server.NewService(service)
var (
endpoint string
)
benchmarkServiceOnce.Do(func() {
go server.Run()
for {
if len(server.Address()) > 0 {
break
}
}
endpoint = "http://" + server.Address() + "/server/service"
})
client := &http.Client{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
client.Get(endpoint)
}
}