Fix TestSetMaxEstablishedConn and allow it to be run with -count > 1

Similar changes should occur to other tests exporting StatusWriters.
This commit is contained in:
Matt Joiner 2018-02-11 15:14:31 +11:00
parent 57216bd299
commit 6773fa9a7e
3 changed files with 40 additions and 4 deletions

View File

@ -969,6 +969,8 @@ func totalConns(tts []*Torrent) (ret int) {
}
func TestSetMaxEstablishedConn(t *testing.T) {
ss := testutil.NewStatusServer(t)
defer ss.Close()
var tts []*Torrent
ih := testutil.GreetingMetaInfo().HashInfoBytes()
for i := range iter.N(3) {
@ -977,18 +979,21 @@ func TestSetMaxEstablishedConn(t *testing.T) {
defer cl.Close()
tt, _ := cl.AddTorrentInfoHash(ih)
tt.SetMaxEstablishedConns(2)
testutil.ExportStatusWriter(cl, fmt.Sprintf("%d", i))
ss.HandleStatusWriter(cl, fmt.Sprintf("/%d", i))
tts = append(tts, tt)
}
addPeers := func() {
for i, tt := range tts {
for _, _tt := range tts[:i] {
for _, tt := range tts {
for _, _tt := range tts {
// if tt != _tt {
addClientPeer(tt, _tt.cl)
// }
}
}
}
waitTotalConns := func(num int) {
for totalConns(tts) != num {
addPeers()
time.Sleep(time.Millisecond)
}
}

View File

@ -3,15 +3,20 @@ package testutil
import (
"fmt"
"io"
"log"
"net"
"net/http"
"testing"
"github.com/anacrolix/missinggo"
"github.com/stretchr/testify/require"
)
type StatusWriter interface {
WriteStatus(io.Writer)
}
// Use StatusServer instead to allow -count > 1 when testing.
func ExportStatusWriter(sw StatusWriter, path string) {
http.HandleFunc(
fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path),
@ -20,3 +25,29 @@ func ExportStatusWriter(sw StatusWriter, path string) {
},
)
}
type StatusServer struct {
sm http.ServeMux
l net.Listener
}
func NewStatusServer(t *testing.T) (ret *StatusServer) {
l, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
ret = &StatusServer{
l: l,
}
log.Printf("serving status at %q", l.Addr())
go http.Serve(l, &ret.sm)
return
}
func (me *StatusServer) HandleStatusWriter(sw StatusWriter, path string) {
me.sm.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
sw.WriteStatus(w)
})
}
func (me StatusServer) Close() {
me.l.Close()
}

View File

@ -12,7 +12,7 @@ import (
var pkgTempDir string
func init() {
log.SetFlags(log.LstdFlags | log.Llongfile)
log.SetFlags(log.LstdFlags | log.Lshortfile)
var err error
pkgTempDir, err = ioutil.TempDir("", "torrent.test")
if err != nil {