Skip to content

Commit

Permalink
since test isn't really a go test, converted to a main package
Browse files Browse the repository at this point in the history
  • Loading branch information
jpillora committed Mar 16, 2015
1 parent 5dd999f commit 7043d06
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions test/chisel_test.go → test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,7 +26,6 @@ import (

"github.com/jpillora/chisel/share"

"testing"
"time"
)

Expand All @@ -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))
}
}

Expand Down Expand Up @@ -114,23 +120,30 @@ 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()
cd := exec.Command("crowbard",
`-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)
Expand All @@ -142,35 +155,36 @@ 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)

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)
}

0 comments on commit 7043d06

Please sign in to comment.