This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rack_handler_test.go
139 lines (103 loc) · 2.81 KB
/
rack_handler_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
package gorack
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strings"
"sync"
"testing"
)
var testBody = "OMG\x00, test body\nhalps"
// var echoBody = `{"REQUEST_METHOD"=>"POST", "SCRIPT_NAME"=>"", "PATH_INFO"=>"/", "QUERY_STRING"=>"", "SERVER_NAME"=>"host", "SERVER_PORT"=>"port", "HTTP_ACCEPT_ENCODING"=>"gzip", "HTTP_CONTENT_LENGTH"=>"21", "HTTP_CONTENT_TYPE"=>"text/plain", "HTTP_USER_AGENT"=>"Go 1.1 package http"}` + testBody
var echoBody = `{"HTTP_ACCEPT_ENCODING"=>"gzip", "HTTP_CONTENT_LENGTH"=>"21", "HTTP_CONTENT_TYPE"=>"text/plain", "HTTP_USER_AGENT"=>"Go 1.1 package http", "PATH_INFO"=>"/", "QUERY_STRING"=>"", "REQUEST_METHOD"=>"POST", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"host", "SERVER_PORT"=>"port"}` + testBody
func TestRackHandler(t *testing.T) {
var cases = []struct{ in, exp, script string }{
{testBody, testBody, "./ruby/test/config_test.ru"},
{testBody, echoBody, "./ruby/test/echo.ru"},
}
for _, v := range cases {
exp := v.exp
got, host, port, err := submit(v.in, v.script)
if err != nil {
t.Error(err)
}
if echoBody == v.exp {
exp = strings.Replace(v.exp, `=>"port"`, `=>"`+port+`"`, -1)
exp = strings.Replace(exp, `=>"host"`, `=>"`+host+`"`, -1)
}
if exp != got {
t.Errorf("\nExp:%s\nGot:%s", exp, got)
}
}
}
func submit(body string, rackScript string) (string, string, string, error) {
handler := NewRackHandler(rackScript)
if err := handler.StartRackProcess(); err != nil {
return "", "", "", err
}
ts := httptest.NewServer(handler)
log.Println(ts.URL)
defer handler.StopRackProcess()
defer ts.Close()
res, err := http.Post(ts.URL, "text/plain", strings.NewReader(body))
if err != nil {
return "", "", "", err
}
got, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return "", "", "", err
}
u, _ := url.Parse(ts.URL)
host, port, _ := net.SplitHostPort(u.Host)
return string(got), host, port, nil
}
func BenchmarkRackHandler(b *testing.B) {
// runtime.GOMAXPROCS(2)
handler := NewRackHandler("./ruby/test/echo.ru")
if err := handler.StartRackProcess(); err != nil {
b.Error(err)
}
ts := httptest.NewServer(handler)
log.Println(ts.URL)
defer handler.StopRackProcess()
defer ts.Close()
req := make(chan int)
wg := sync.WaitGroup{}
worker := func(i int) {
for rn := range req {
wg.Add(1)
body := fmt.Sprintf("worker %d:%d", i, rn)
// log.Println(body)
_, err := http.Post(ts.URL, "text/plain", strings.NewReader(body))
if err != nil {
b.Error(err)
}
wg.Done()
}
}
for i := 0; i < 10; i += 1 {
go worker(i)
}
b.ResetTimer()
printed := true
for i := 0; i < 100; {
select {
case req <- i:
i += 1
printed = false
default:
if !printed {
log.Println("Waiting")
printed = true
}
runtime.Gosched()
}
}
wg.Wait()
}