forked from birkirb/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
148 lines (126 loc) · 3.69 KB
/
server_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
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// This tries to benchmark the usual request-serving pipeline to get an overall
// performance metric.
//
// Each iteration runs one mock request to display a hotel's detail page by id.
//
// Contributing parts:
// - Routing
// - Controller lookup / invocation
// - Parameter binding
// - Session, flash, i18n cookies
// - Render() call magic
// - Template rendering
func BenchmarkServeAction(b *testing.B) {
benchmarkRequest(b, showRequest)
}
func BenchmarkServeJson(b *testing.B) {
benchmarkRequest(b, jsonRequest)
}
func BenchmarkServePlaintext(b *testing.B) {
benchmarkRequest(b, plaintextRequest)
}
// This tries to benchmark the static serving overhead when serving an "average
// size" 7k file.
func BenchmarkServeStatic(b *testing.B) {
benchmarkRequest(b, staticRequest)
}
func benchmarkRequest(b *testing.B, req *http.Request) {
startFakeBookingApp()
b.ResetTimer()
resp := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
CurrentEngine.(*GoHttpServer).Handle(resp, req)
}
}
// Test that the booking app can be successfully run for a test.
func TestFakeServer(t *testing.T) {
startFakeBookingApp()
resp := httptest.NewRecorder()
// First, test that the expected responses are actually generated
CurrentEngine.(*GoHttpServer).Handle(resp, showRequest)
if !strings.Contains(resp.Body.String(), "300 Main St.") {
t.Errorf("Failed to find hotel address in action response:\n%s", resp.Body)
t.FailNow()
}
resp.Body.Reset()
CurrentEngine.(*GoHttpServer).Handle(resp, staticRequest)
sessvarsSize := getFileSize(t, filepath.Join(BasePath, "public", "js", "sessvars.js"))
if int64(resp.Body.Len()) != sessvarsSize {
t.Errorf("Expected sessvars.js to have %d bytes, got %d:\n%s", sessvarsSize, resp.Body.Len(), resp.Body)
t.FailNow()
}
resp.Body.Reset()
CurrentEngine.(*GoHttpServer).Handle(resp, jsonRequest)
if !strings.Contains(resp.Body.String(), `"Address":"300 Main St."`) {
t.Errorf("Failed to find hotel address in JSON response:\n%s", resp.Body)
t.FailNow()
}
resp.Body.Reset()
CurrentEngine.(*GoHttpServer).Handle(resp, plaintextRequest)
if resp.Body.String() != "Hello, World!" {
t.Errorf("Failed to find greeting in plaintext response:\n%s", resp.Body)
t.FailNow()
}
resp.Body = nil
}
func getFileSize(t *testing.T, name string) int64 {
fi, err := os.Stat(name)
if err != nil {
t.Errorf("Unable to stat file:\n%s", name)
t.FailNow()
}
return fi.Size()
}
// Ensure on app start runs in order
func TestOnAppStart(t *testing.T) {
str := ""
a := assert.New(t)
OnAppStart(func() {
str += " World"
}, 2)
OnAppStart(func() {
str += "Hello"
}, 1)
startFakeBookingApp()
a.Equal("Hello World", str, "Failed to order OnAppStart")
}
// Ensure on app stop runs in order
func TestOnAppStop(t *testing.T) {
a := assert.New(t)
startFakeBookingApp()
i := ""
OnAppStop(func() {
i += "cruel world"
t.Logf("i: %v \n", i)
}, 2)
OnAppStop(func() {
i += "goodbye "
t.Logf("i: %v \n", i)
}, 1)
go func() {
time.Sleep(2 * time.Second)
RaiseEvent(ENGINE_SHUTDOWN_REQUEST, nil)
}()
Run(0)
a.Equal("goodbye cruel world", i, "Did not get shutdown events")
}
var (
showRequest, _ = http.NewRequest("GET", "/hotels/3", nil)
staticRequest, _ = http.NewRequest("GET", "/public/js/sessvars.js", nil)
jsonRequest, _ = http.NewRequest("GET", "/hotels/3/booking", nil)
plaintextRequest, _ = http.NewRequest("GET", "/hotels", nil)
)