]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix TestSetMaxEstablishedConn and allow it to be run with -count > 1
authorMatt Joiner <anacrolix@gmail.com>
Sun, 11 Feb 2018 04:14:31 +0000 (15:14 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 11 Feb 2018 04:14:31 +0000 (15:14 +1100)
Similar changes should occur to other tests exporting StatusWriters.

client_test.go
internal/testutil/status_writer.go
main_test.go

index edaa4ffb521d689b530b15dd2c4909b1127c6723..597785e223ed505b7a59742ac8f7e64f413504e7 100644 (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)
                }
        }
index 06e2f66286487d26b03a033c743c607d006c682f..0266e3eba856efee21e4765c3a358f94ddde79f2 100644 (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()
+}
index 2add01b53c43acfb8156bbf4c96310f2d910eaec..26433fb4c09f08a616a6fd1a77ffe7aca40556e6 100644 (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 {