From: Matt Joiner Date: Sun, 11 Feb 2018 04:14:31 +0000 (+1100) Subject: Fix TestSetMaxEstablishedConn and allow it to be run with -count > 1 X-Git-Tag: v1.0.0~186 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=6773fa9a7e04f8d0c103b49f7aa5c85b413783d7;p=btrtrc.git Fix TestSetMaxEstablishedConn and allow it to be run with -count > 1 Similar changes should occur to other tests exporting StatusWriters. --- diff --git a/client_test.go b/client_test.go index edaa4ffb..597785e2 100644 --- a/client_test.go +++ b/client_test.go @@ -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) } } diff --git a/internal/testutil/status_writer.go b/internal/testutil/status_writer.go index 06e2f662..0266e3eb 100644 --- a/internal/testutil/status_writer.go +++ b/internal/testutil/status_writer.go @@ -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() +} diff --git a/main_test.go b/main_test.go index 2add01b5..26433fb4 100644 --- a/main_test.go +++ b/main_test.go @@ -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 {