Skip to content

Commit

Permalink
Merge pull request #37 from dmcgowan/random-test-ports
Browse files Browse the repository at this point in the history
Use random ports in unit tests
  • Loading branch information
dmcgowan committed Jan 22, 2015
2 parents cf6cfc2 + b5d2660 commit d716217
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
22 changes: 9 additions & 13 deletions spdy_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,28 @@ import (
"testing"
)

const (
LISTEN_ADDRESS = "127.0.0.1:7777"
)

func configureServer() (io.Closer, *sync.WaitGroup) {
func configureServer() (io.Closer, string, *sync.WaitGroup) {
authenticated = true
wg := &sync.WaitGroup{}
server, serverErr := runServer(LISTEN_ADDRESS, wg)
server, listen, serverErr := runServer(wg)

if serverErr != nil {
panic(serverErr)
}

return server, wg
return server, listen, wg
}

func BenchmarkDial10000(b *testing.B) {
server, wg := configureServer()
server, addr, wg := configureServer()

defer func() {
server.Close()
wg.Wait()
}()

for i := 0; i < b.N; i++ {
conn, dialErr := net.Dial("tcp", LISTEN_ADDRESS)
conn, dialErr := net.Dial("tcp", addr)
if dialErr != nil {
panic(fmt.Sprintf("Error dialing server: %s", dialErr))
}
Expand All @@ -43,15 +39,15 @@ func BenchmarkDial10000(b *testing.B) {
}

func BenchmarkDialWithSPDYStream10000(b *testing.B) {
server, wg := configureServer()
server, addr, wg := configureServer()

defer func() {
server.Close()
wg.Wait()
}()

for i := 0; i < b.N; i++ {
conn, dialErr := net.Dial("tcp", LISTEN_ADDRESS)
conn, dialErr := net.Dial("tcp", addr)
if dialErr != nil {
b.Fatalf("Error dialing server: %s", dialErr)
}
Expand All @@ -70,15 +66,15 @@ func BenchmarkDialWithSPDYStream10000(b *testing.B) {
}

func benchmarkStreamWithDataAndSize(size uint64, b *testing.B) {
server, wg := configureServer()
server, addr, wg := configureServer()

defer func() {
server.Close()
wg.Wait()
}()

for i := 0; i < b.N; i++ {
conn, dialErr := net.Dial("tcp", LISTEN_ADDRESS)
conn, dialErr := net.Dial("tcp", addr)
if dialErr != nil {
b.Fatalf("Error dialing server: %s", dialErr)
}
Expand Down
23 changes: 9 additions & 14 deletions spdy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spdystream

import (
"bytes"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -13,8 +12,7 @@ import (

func TestSpdyStreams(t *testing.T) {
var wg sync.WaitGroup
listen := "localhost:7443"
server, serverErr := runServer(listen, &wg)
server, listen, serverErr := runServer(&wg)
if serverErr != nil {
t.Fatalf("Error initializing server: %s", serverErr)
}
Expand Down Expand Up @@ -156,8 +154,7 @@ func TestSpdyStreams(t *testing.T) {

func TestPing(t *testing.T) {
var wg sync.WaitGroup
listen := "localhost:7543"
server, serverErr := runServer(listen, &wg)
server, listen, serverErr := runServer(&wg)
if serverErr != nil {
t.Fatalf("Error initializing server: %s", serverErr)
}
Expand Down Expand Up @@ -190,8 +187,7 @@ func TestPing(t *testing.T) {

func TestHalfClose(t *testing.T) {
var wg sync.WaitGroup
listen := "localhost:7643"
server, serverErr := runServer(listen, &wg)
server, listen, serverErr := runServer(&wg)
if serverErr != nil {
t.Fatalf("Error initializing server: %s", serverErr)
}
Expand Down Expand Up @@ -263,8 +259,7 @@ func TestUnexpectedRemoteConnectionClosed(t *testing.T) {
{closeReceiver: false, closeSender: false},
}
for tix, tc := range tt {
listen := fmt.Sprintf("localhost:774%d", tix)
listener, listenErr := net.Listen("tcp", listen)
listener, listenErr := net.Listen("tcp", "localhost:0")
if listenErr != nil {
t.Fatalf("Error listening: %v", listenErr)
}
Expand All @@ -283,7 +278,7 @@ func TestUnexpectedRemoteConnectionClosed(t *testing.T) {
})
}()

conn, dialErr := net.Dial("tcp", listen)
conn, dialErr := net.Dial("tcp", listener.Addr().String())
if dialErr != nil {
t.Fatalf("Error dialing server: %s", dialErr)
}
Expand Down Expand Up @@ -352,10 +347,10 @@ func authStreamHandler(stream *Stream) {
MirrorStreamHandler(stream)
}

func runServer(listen string, wg *sync.WaitGroup) (io.Closer, error) {
listener, listenErr := net.Listen("tcp", listen)
func runServer(wg *sync.WaitGroup) (io.Closer, string, error) {
listener, listenErr := net.Listen("tcp", "localhost:0")
if listenErr != nil {
return nil, listenErr
return nil, "", listenErr
}
wg.Add(1)
go func() {
Expand All @@ -371,5 +366,5 @@ func runServer(listen string, wg *sync.WaitGroup) (io.Closer, error) {
}
wg.Done()
}()
return listener, nil
return listener, listener.Addr().String(), nil
}

0 comments on commit d716217

Please sign in to comment.