since test isn't really a go test, converted to a main package

This commit is contained in:
Jaime Pillora
2015-03-17 09:19:16 +11:00
parent 5dd999f0ad
commit 7043d06be8
+51 -37
View File
@@ -9,17 +9,12 @@
// '--> crowbar:4001--->crowbar:4002'
// client server
//
// benchmarks don't use testing.B, instead use
// go test -test.run=Bench
//
// tests use
// go test -test.run=Request
//
// crowbar and chisel binaries should be in your PATH
package test
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
@@ -31,7 +26,6 @@ import (
"github.com/jpillora/chisel/share"
"testing"
"time"
)
@@ -42,43 +36,55 @@ const (
GB = 1024 * MB
)
func run() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fatal("go run main.go [test] or [bench]")
}
for _, a := range args {
switch a {
case "test":
test()
case "bench":
bench()
}
}
}
//test
func TestRequestChisel(t *testing.T) {
testTunnel("2001", 500, t)
testTunnel("2001", 50000, t)
func test() {
testTunnel("2001", 500)
testTunnel("2001", 50000)
}
//benchmark
func TestBenchDirect(t *testing.T) {
benchSizes("3000", t)
}
func TestBenchChisel(t *testing.T) {
benchSizes("2001", t)
}
func TestBenchrowbar(t *testing.T) {
benchSizes("4001", t)
func bench() {
benchSizes("3000")
benchSizes("2001")
benchSizes("4001")
}
func benchSizes(port string, t *testing.T) {
func benchSizes(port string) {
for size := 1; size < 100*MB; size *= 10 {
testTunnel(port, size, t)
testTunnel(port, size)
}
}
func testTunnel(port string, size int, t *testing.T) {
func testTunnel(port string, size int) {
t0 := time.Now()
resp, err := requestFile(port, size)
if err != nil {
t.Fatal(err)
fatal(err)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
fatal(err)
}
t1 := time.Now()
fmt.Printf(":%s => %d bytes in %s\n", port, size, t1.Sub(t0))
if len(b) != size {
t.Fatalf("%d bytes expected, got %d", size, len(b))
fatalf("%d bytes expected, got %d", size, len(b))
}
}
@@ -114,12 +120,19 @@ func makeFileServer() *chshare.HTTPServer {
//============================
func fatal(args ...interface{}) {
panic(fmt.Sprint(args...))
}
func fatalf(f string, args ...interface{}) {
panic(fmt.Sprintf(f, args...))
}
//global setup
func TestMain(m *testing.M) {
func main() {
fs := makeFileServer()
go func() {
log.Fatal(fs.Wait())
fatal(fs.Wait())
}()
dir, _ := os.Getwd()
@@ -127,10 +140,10 @@ func TestMain(m *testing.M) {
`-listen`, "0.0.0.0:4002",
`-userfile`, path.Join(dir, "userfile"))
if err := cd.Start(); err != nil {
log.Fatal(err)
fatal(err)
}
go func() {
log.Fatalf("crowbard: %s", cd.Wait())
fatalf("crowbard: %v", cd.Wait())
}()
time.Sleep(100 * time.Millisecond)
@@ -142,7 +155,7 @@ func TestMain(m *testing.M) {
"-username", "foo",
"-password", "bar")
if err := cf.Start(); err != nil {
log.Fatal(err)
fatal(err)
}
time.Sleep(100 * time.Millisecond)
@@ -150,27 +163,28 @@ func TestMain(m *testing.M) {
hd := exec.Command("chisel", "server", "--port", "2002" /*"--key", "foobar",*/)
// hd.Stdout = os.Stdout
if err := hd.Start(); err != nil {
log.Fatal(err)
fatal(err)
}
hf := exec.Command("chisel", "client", /*"--key", "foobar",*/
"127.0.0.1:2002",
"2001:3000")
// hf.Stdout = os.Stdout
if err := hf.Start(); err != nil {
log.Fatal(err)
fatal(err)
}
time.Sleep(100 * time.Millisecond)
fmt.Println("Running!")
code := m.Run()
fmt.Println("Done")
defer func() {
if r := recover(); r != nil {
log.Print(r)
}
}()
run()
cd.Process.Kill()
cf.Process.Kill()
hd.Process.Kill()
hf.Process.Kill()
fs.Close()
os.Exit(code)
}