]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add more thorough tests for Torrent.SetMaxEstablishedConns
authorMatt Joiner <anacrolix@gmail.com>
Tue, 5 Jul 2016 22:30:34 +0000 (08:30 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 5 Jul 2016 22:30:34 +0000 (08:30 +1000)
client_test.go

index c9046ece4a04b5e738513e16966e443bbeaa68e3..6e774a2974a8345d53df720ebef3e71e79e216b3 100644 (file)
@@ -952,3 +952,67 @@ func TestClientDynamicListenPortNoProtocols(t *testing.T) {
        t.Log(cl.listenAddr)
        assert.Nil(t, cl.ListenAddr())
 }
+
+func addClientPeer(t *Torrent, cl *Client) {
+       t.AddPeers([]Peer{
+               Peer{
+                       IP:   missinggo.AddrIP(cl.ListenAddr()),
+                       Port: missinggo.AddrPort(cl.ListenAddr()),
+               },
+       })
+}
+
+func printConnPeerCounts(t *Torrent) {
+       t.cl.mu.Lock()
+       log.Println(len(t.conns), len(t.peers))
+       t.cl.mu.Unlock()
+}
+
+func totalConns(tts []*Torrent) (ret int) {
+       for _, tt := range tts {
+               tt.cl.mu.Lock()
+               ret += len(tt.conns)
+               tt.cl.mu.Unlock()
+       }
+       return
+}
+
+func TestSetMaxEstablishedConn(t *testing.T) {
+       var tts []*Torrent
+       ih := testutil.GreetingMetaInfo().Info.Hash()
+       cfg := TestingConfig
+       cfg.DisableUTP = true
+       for i := range iter.N(3) {
+               cl, err := NewClient(&cfg)
+               require.NoError(t, err)
+               defer cl.Close()
+               tt, _ := cl.AddTorrentInfoHash(ih)
+               tt.SetMaxEstablishedConns(2)
+               testutil.ExportStatusWriter(cl, fmt.Sprintf("%d", i))
+               tts = append(tts, tt)
+       }
+       addPeers := func() {
+               for i, tt := range tts {
+                       for _, _tt := range tts[:i] {
+                               addClientPeer(tt, _tt.cl)
+                       }
+               }
+       }
+       waitTotalConns := func(num int) {
+               for totalConns(tts) != num {
+                       time.Sleep(time.Millisecond)
+               }
+       }
+       addPeers()
+       waitTotalConns(6)
+       tts[0].SetMaxEstablishedConns(1)
+       waitTotalConns(4)
+       tts[0].SetMaxEstablishedConns(0)
+       waitTotalConns(2)
+       tts[0].SetMaxEstablishedConns(1)
+       addPeers()
+       waitTotalConns(4)
+       tts[0].SetMaxEstablishedConns(2)
+       addPeers()
+       waitTotalConns(6)
+}