Add tests for preferred network direction
This commit is contained in:
parent
0c7754da63
commit
83a8284d6a
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user