]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add tests for preferred network direction
authorMatt Joiner <anacrolix@gmail.com>
Wed, 12 Jan 2022 04:01:33 +0000 (15:01 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 12 Jan 2022 04:01:33 +0000 (15:01 +1100)
peerconn.go
peerconn_test.go
torrent.go

index 6866d450c42be68fef857e93b89dcac84bebab8a..d1ebf3a496d2cd0a350fe736b8a0919a93e932cc 100644 (file)
@@ -221,12 +221,12 @@ func (cn *PeerConn) isPreferredDirection() bool {
 // Returns whether the left connection should be preferred over the right one,
 // considering only their networking properties. If ok is false, we can't
 // decide.
-func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) (left, ok bool) {
+func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
        var ml multiLess
        ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection())
        ml.NextBool(!l.utp(), !r.utp())
        ml.NextBool(l.ipv6(), r.ipv6())
-       return ml.FinalOk()
+       return ml.Less()
 }
 
 func (cn *Peer) cumInterest() time.Duration {
index 2d5aa681c9462b10ea750b15bbac835c99eeeb4c..b1ac1dbc987dba423fe38aac7a8fe3d4bf4d258f 100644 (file)
@@ -1,7 +1,9 @@
 package torrent
 
 import (
+       "encoding/binary"
        "errors"
+       "fmt"
        "io"
        "net"
        "sync"
@@ -246,3 +248,36 @@ func TestApplyRequestStateWriteBufferConstraints(t *testing.T) {
        c.Check(maxLocalToRemoteRequests >= 8, qt.IsTrue)
        c.Logf("max local to remote requests: %v", maxLocalToRemoteRequests)
 }
+
+func peerConnForPreferredNetworkDirection(localPeerId, remotePeerId int, outgoing, utp, ipv6 bool) *PeerConn {
+       pc := PeerConn{}
+       pc.outgoing = outgoing
+       if utp {
+               pc.Network = "udp"
+       }
+       if ipv6 {
+               pc.RemoteAddr = &net.TCPAddr{IP: net.ParseIP(fmt.Sprintf("::420"))}
+       } else {
+               pc.RemoteAddr = &net.TCPAddr{IP: net.IPv4(1, 2, 3, 4)}
+       }
+       binary.BigEndian.PutUint64(pc.PeerID[:], uint64(remotePeerId))
+       cl := Client{}
+       binary.BigEndian.PutUint64(cl.peerID[:], uint64(localPeerId))
+       pc.t = &Torrent{cl: &cl}
+       return &pc
+}
+
+func TestPreferredNetworkDirection(t *testing.T) {
+       pc := peerConnForPreferredNetworkDirection
+       c := qt.New(t)
+       // Prefer outgoing to higher peer ID
+       c.Assert(pc(1, 2, true, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsTrue)
+       c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, true, false, false)), qt.IsFalse)
+       c.Assert(pc(2, 1, false, false, false).hasPreferredNetworkOver(pc(2, 1, true, false, false)), qt.IsTrue)
+       // Don't prefer uTP
+       c.Assert(pc(1, 2, false, true, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsFalse)
+       // Prefer IPv6
+       c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, true)), qt.IsFalse)
+       // No difference
+       c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsFalse)
+}
index fbb5f6b005256dbbee547b9d987117ee6cebd8a1..8e83d3d38a252361eca585629e07218fb87dafbe 100644 (file)
@@ -1866,7 +1866,7 @@ func (t *Torrent) addPeerConn(c *PeerConn) (err error) {
                if !t.cl.config.DropDuplicatePeerIds {
                        continue
                }
-               if left, ok := c.hasPreferredNetworkOver(c0); ok && left {
+               if c.hasPreferredNetworkOver(c0) {
                        c0.close()
                        t.deletePeerConn(c0)
                } else {