9
9
// '--> crowbar:4001--->crowbar:4002'
10
10
// client server
11
11
//
12
- // benchmarks don't use testing.B, instead use
13
- // go test -test.run=Bench
14
- //
15
- // tests use
16
- // go test -test.run=Request
17
- //
18
12
// crowbar and chisel binaries should be in your PATH
19
13
20
- package test
14
+ package main
21
15
22
16
import (
17
+ "flag"
23
18
"fmt"
24
19
"io/ioutil"
25
20
"log"
@@ -31,7 +26,6 @@ import (
31
26
32
27
"github.com/jpillora/chisel/share"
33
28
34
- "testing"
35
29
"time"
36
30
)
37
31
@@ -42,43 +36,55 @@ const (
42
36
GB = 1024 * MB
43
37
)
44
38
39
+ func run () {
40
+ flag .Parse ()
41
+ args := flag .Args ()
42
+ if len (args ) == 0 {
43
+ fatal ("go run main.go [test] or [bench]" )
44
+ }
45
+ for _ , a := range args {
46
+ switch a {
47
+ case "test" :
48
+ test ()
49
+ case "bench" :
50
+ bench ()
51
+ }
52
+ }
53
+ }
54
+
45
55
//test
46
- func TestRequestChisel ( t * testing. T ) {
47
- testTunnel ("2001" , 500 , t )
48
- testTunnel ("2001" , 50000 , t )
56
+ func test ( ) {
57
+ testTunnel ("2001" , 500 )
58
+ testTunnel ("2001" , 50000 )
49
59
}
50
60
51
61
//benchmark
52
- func TestBenchDirect (t * testing.T ) {
53
- benchSizes ("3000" , t )
54
- }
55
- func TestBenchChisel (t * testing.T ) {
56
- benchSizes ("2001" , t )
57
- }
58
- func TestBenchrowbar (t * testing.T ) {
59
- benchSizes ("4001" , t )
62
+ func bench () {
63
+ benchSizes ("3000" )
64
+ benchSizes ("2001" )
65
+ benchSizes ("4001" )
60
66
}
61
67
62
- func benchSizes (port string , t * testing. T ) {
68
+ func benchSizes (port string ) {
63
69
for size := 1 ; size < 100 * MB ; size *= 10 {
64
- testTunnel (port , size , t )
70
+ testTunnel (port , size )
65
71
}
66
72
}
67
73
68
- func testTunnel (port string , size int , t * testing. T ) {
74
+ func testTunnel (port string , size int ) {
69
75
t0 := time .Now ()
70
76
resp , err := requestFile (port , size )
71
77
if err != nil {
72
- t . Fatal (err )
78
+ fatal (err )
73
79
}
74
80
b , err := ioutil .ReadAll (resp .Body )
75
81
if err != nil {
76
- t . Fatal (err )
82
+ fatal (err )
77
83
}
78
84
t1 := time .Now ()
79
85
fmt .Printf (":%s => %d bytes in %s\n " , port , size , t1 .Sub (t0 ))
80
86
if len (b ) != size {
81
- t . Fatalf ("%d bytes expected, got %d" , size , len (b ))
87
+ fatalf ("%d bytes expected, got %d" , size , len (b ))
82
88
}
83
89
}
84
90
@@ -114,23 +120,30 @@ func makeFileServer() *chshare.HTTPServer {
114
120
115
121
//============================
116
122
123
+ func fatal (args ... interface {}) {
124
+ panic (fmt .Sprint (args ... ))
125
+ }
126
+ func fatalf (f string , args ... interface {}) {
127
+ panic (fmt .Sprintf (f , args ... ))
128
+ }
129
+
117
130
//global setup
118
- func TestMain ( m * testing. M ) {
131
+ func main ( ) {
119
132
120
133
fs := makeFileServer ()
121
134
go func () {
122
- log . Fatal (fs .Wait ())
135
+ fatal (fs .Wait ())
123
136
}()
124
137
125
138
dir , _ := os .Getwd ()
126
139
cd := exec .Command ("crowbard" ,
127
140
`-listen` , "0.0.0.0:4002" ,
128
141
`-userfile` , path .Join (dir , "userfile" ))
129
142
if err := cd .Start (); err != nil {
130
- log . Fatal (err )
143
+ fatal (err )
131
144
}
132
145
go func () {
133
- log . Fatalf ("crowbard: %s " , cd .Wait ())
146
+ fatalf ("crowbard: %v " , cd .Wait ())
134
147
}()
135
148
136
149
time .Sleep (100 * time .Millisecond )
@@ -142,35 +155,36 @@ func TestMain(m *testing.M) {
142
155
"-username" , "foo" ,
143
156
"-password" , "bar" )
144
157
if err := cf .Start (); err != nil {
145
- log . Fatal (err )
158
+ fatal (err )
146
159
}
147
160
148
161
time .Sleep (100 * time .Millisecond )
149
162
150
163
hd := exec .Command ("chisel" , "server" , "--port" , "2002" /*"--key", "foobar",*/ )
151
164
// hd.Stdout = os.Stdout
152
165
if err := hd .Start (); err != nil {
153
- log . Fatal (err )
166
+ fatal (err )
154
167
}
155
168
hf := exec .Command ("chisel" , "client" , /*"--key", "foobar",*/
156
169
"127.0.0.1:2002" ,
157
170
"2001:3000" )
158
171
// hf.Stdout = os.Stdout
159
172
if err := hf .Start (); err != nil {
160
- log . Fatal (err )
173
+ fatal (err )
161
174
}
162
175
163
176
time .Sleep (100 * time .Millisecond )
164
177
165
- fmt .Println ("Running!" )
166
- code := m .Run ()
167
- fmt .Println ("Done" )
178
+ defer func () {
179
+ if r := recover (); r != nil {
180
+ log .Print (r )
181
+ }
182
+ }()
183
+ run ()
168
184
169
185
cd .Process .Kill ()
170
186
cf .Process .Kill ()
171
187
hd .Process .Kill ()
172
188
hf .Process .Kill ()
173
189
fs .Close ()
174
-
175
- os .Exit (code )
176
190
}
0 commit comments