Skip to content

Commit

Permalink
TestSetup: use automatic port number
Browse files Browse the repository at this point in the history
By giving the port number '0', we can get an available port selected by
the system.
  • Loading branch information
johningve committed Jul 8, 2020
1 parent fd597e2 commit cf73db2
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions testing_gorums.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package gorums

import (
"fmt"
"net"
"sync"
"testing"
)

Expand All @@ -28,7 +26,8 @@ func TestSetup(t testing.TB, numServers int, srvFunc func() interface{}) ([]stri
if srv, ok = srvFunc().(server); !ok {
t.Fatal("Incompatible server type. You should use a GorumsServer or grpc.Server")
}
lis, err := getListener()
// listen on any available port
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Failed to listen on port: %v", err)
}
Expand All @@ -43,22 +42,3 @@ func TestSetup(t testing.TB, numServers int, srvFunc func() interface{}) ([]stri
}
return addrs, stopFn
}

type portSupplier struct {
p int
sync.Mutex
}

func (p *portSupplier) get() int {
p.Lock()
newPort := p.p
p.p++
p.Unlock()
return newPort
}

var supplier = portSupplier{p: 22332}

func getListener() (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf(":%d", supplier.get()))
}

0 comments on commit cf73db2

Please sign in to comment.