-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
utils_test.go
154 lines (118 loc) · 3.56 KB
/
utils_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
package atreugo
import (
"errors"
"io"
"log"
"net"
"runtime"
"testing"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"github.com/valyala/fasthttp/prefork"
)
var testLog = log.New(io.Discard, "", log.LstdFlags)
var testConfig = Config{
Logger: testLog,
ErrorView: defaultErrorView,
}
type preforkServerMock struct {
server *Atreugo
ln net.Listener
errListenAndServe error
}
func newPreforkServerMock(s *Atreugo, errListenAndServe error) *preforkServerMock {
return &preforkServerMock{
server: s,
ln: fasthttputil.NewInmemoryListener(),
errListenAndServe: errListenAndServe,
}
}
func (s *preforkServerMock) ListenAndServe(_ string) error {
if s.errListenAndServe != nil {
return s.errListenAndServe
}
return s.server.Serve(s.ln)
}
func Test_viewToHandler(t *testing.T) {
called := false
err := errors.New("error")
view := func(_ *RequestCtx) error {
called = true
return err
}
ctx := new(fasthttp.RequestCtx)
handler := viewToHandler(view, defaultErrorView)
handler(ctx)
if !called {
t.Error("View is not called")
}
if ctx.Response.StatusCode() != fasthttp.StatusInternalServerError {
t.Errorf("Status code == %d, want %d", ctx.Response.StatusCode(), fasthttp.StatusInternalServerError)
}
if string(ctx.Response.Body()) != err.Error() {
t.Errorf("Response body == %s, want %s", ctx.Response.Body(), err.Error())
}
}
func Test_isEqual(t *testing.T) {
v1 := func() {} // nolint:ifshort
v2 := func() {} // nolint:ifshort
if !isEqual(v1, v1) {
t.Errorf("Values are equals")
}
if isEqual(v1, v2) {
t.Errorf("Values are not equals")
}
}
func Test_middlewaresInclude(t *testing.T) {
fnIncluded := func(_ *RequestCtx) error { return nil }
fnNotIncluded := func(_ *RequestCtx) error { return nil }
ms := []Middleware{fnIncluded}
if !middlewaresInclude(ms, fnIncluded) {
t.Errorf("The middleware '%p' is included in '%p'", fnIncluded, ms)
}
if middlewaresInclude(ms, fnNotIncluded) {
t.Errorf("The middleware '%p' is not included in '%p'", fnNotIncluded, ms)
}
}
func Test_appendMiddlewares(t *testing.T) {
fn := func(_ *RequestCtx) error { return nil }
fnSkip := func(_ *RequestCtx) error { return nil }
dst := []Middleware{}
src := []Middleware{fn, fnSkip}
skip := []Middleware{fnSkip}
dst = appendMiddlewares(dst, src, skip...)
if middlewaresInclude(dst, fnSkip) {
t.Errorf("The middleware '%p' must not be appended in '%p'", fnSkip, dst)
}
if !middlewaresInclude(dst, fn) {
t.Errorf("The middleware '%p' must be appended in '%p'", fn, dst)
}
}
func testPerforkServer(t *testing.T, s *Atreugo, sPrefork *prefork.Prefork) {
t.Helper()
if sPrefork.Network != s.cfg.Network {
t.Errorf("Prefork.Network == %s, want %s", sPrefork.Network, s.cfg.Network)
}
if sPrefork.Reuseport != s.cfg.Reuseport {
t.Errorf("Prefork.Reuseport == %v, want %v", sPrefork.Reuseport, s.cfg.Reuseport)
}
recoverThreshold := runtime.GOMAXPROCS(0) / 2
if sPrefork.RecoverThreshold != recoverThreshold {
t.Errorf("Prefork.RecoverThreshold == %d, want %d", sPrefork.RecoverThreshold, recoverThreshold)
}
if !isEqual(sPrefork.Logger, s.cfg.Logger) {
t.Errorf("Prefork.Logger == %p, want %p", sPrefork.Logger, s.cfg.Logger)
}
}
func Test_newPreforkServerBase(t *testing.T) {
cfg := Config{
Logger: testLog,
GracefulShutdown: false,
}
s := New(cfg)
sPrefork := newPreforkServerBase(s)
testPerforkServer(t, s, sPrefork)
if !isEqual(sPrefork.ServeFunc, s.Serve) {
t.Errorf("Prefork.ServeFunc == %p, want %p", sPrefork.ServeFunc, s.Serve)
}
}