Skip to content

Commit 7043d06

Browse files
committed
since test isn't really a go test, converted to a main package
1 parent 5dd999f commit 7043d06

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

test/chisel_test.go renamed to test/main.go

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@
99
// '--> crowbar:4001--->crowbar:4002'
1010
// client server
1111
//
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-
//
1812
// crowbar and chisel binaries should be in your PATH
1913

20-
package test
14+
package main
2115

2216
import (
17+
"flag"
2318
"fmt"
2419
"io/ioutil"
2520
"log"
@@ -31,7 +26,6 @@ import (
3126

3227
"github.com/jpillora/chisel/share"
3328

34-
"testing"
3529
"time"
3630
)
3731

@@ -42,43 +36,55 @@ const (
4236
GB = 1024 * MB
4337
)
4438

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+
4555
//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)
4959
}
5060

5161
//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")
6066
}
6167

62-
func benchSizes(port string, t *testing.T) {
68+
func benchSizes(port string) {
6369
for size := 1; size < 100*MB; size *= 10 {
64-
testTunnel(port, size, t)
70+
testTunnel(port, size)
6571
}
6672
}
6773

68-
func testTunnel(port string, size int, t *testing.T) {
74+
func testTunnel(port string, size int) {
6975
t0 := time.Now()
7076
resp, err := requestFile(port, size)
7177
if err != nil {
72-
t.Fatal(err)
78+
fatal(err)
7379
}
7480
b, err := ioutil.ReadAll(resp.Body)
7581
if err != nil {
76-
t.Fatal(err)
82+
fatal(err)
7783
}
7884
t1 := time.Now()
7985
fmt.Printf(":%s => %d bytes in %s\n", port, size, t1.Sub(t0))
8086
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))
8288
}
8389
}
8490

@@ -114,23 +120,30 @@ func makeFileServer() *chshare.HTTPServer {
114120

115121
//============================
116122

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+
117130
//global setup
118-
func TestMain(m *testing.M) {
131+
func main() {
119132

120133
fs := makeFileServer()
121134
go func() {
122-
log.Fatal(fs.Wait())
135+
fatal(fs.Wait())
123136
}()
124137

125138
dir, _ := os.Getwd()
126139
cd := exec.Command("crowbard",
127140
`-listen`, "0.0.0.0:4002",
128141
`-userfile`, path.Join(dir, "userfile"))
129142
if err := cd.Start(); err != nil {
130-
log.Fatal(err)
143+
fatal(err)
131144
}
132145
go func() {
133-
log.Fatalf("crowbard: %s", cd.Wait())
146+
fatalf("crowbard: %v", cd.Wait())
134147
}()
135148

136149
time.Sleep(100 * time.Millisecond)
@@ -142,35 +155,36 @@ func TestMain(m *testing.M) {
142155
"-username", "foo",
143156
"-password", "bar")
144157
if err := cf.Start(); err != nil {
145-
log.Fatal(err)
158+
fatal(err)
146159
}
147160

148161
time.Sleep(100 * time.Millisecond)
149162

150163
hd := exec.Command("chisel", "server", "--port", "2002" /*"--key", "foobar",*/)
151164
// hd.Stdout = os.Stdout
152165
if err := hd.Start(); err != nil {
153-
log.Fatal(err)
166+
fatal(err)
154167
}
155168
hf := exec.Command("chisel", "client", /*"--key", "foobar",*/
156169
"127.0.0.1:2002",
157170
"2001:3000")
158171
// hf.Stdout = os.Stdout
159172
if err := hf.Start(); err != nil {
160-
log.Fatal(err)
173+
fatal(err)
161174
}
162175

163176
time.Sleep(100 * time.Millisecond)
164177

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()
168184

169185
cd.Process.Kill()
170186
cf.Process.Kill()
171187
hd.Process.Kill()
172188
hf.Process.Kill()
173189
fs.Close()
174-
175-
os.Exit(code)
176190
}

0 commit comments

Comments
 (0)