1 Star 0 Fork 1

lsj/go-torrent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pexconn_test.go 1.45 KB
一键复制 编辑 原始数据 按行查看 历史
Matt Joiner 提交于 2021-12-16 11:07 . Fix race in TextPexConnState
package torrent
import (
"net"
"testing"
"github.com/anacrolix/dht/v2/krpc"
"github.com/stretchr/testify/require"
"github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol"
)
func TestPexConnState(t *testing.T) {
var cl Client
cl.init(TestingConfig(t))
cl.initLogger()
torrent := cl.newTorrent(metainfo.Hash{}, nil)
addr := &net.TCPAddr{IP: net.IPv6loopback, Port: 4747}
c := cl.newConnection(nil, false, addr, addr.Network(), "")
c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber)
c.PeerExtensionIDs[pp.ExtensionNamePex] = pexExtendedId
c.messageWriter.mu.Lock()
c.setTorrent(torrent)
if err := torrent.addPeerConn(c); err != nil {
t.Log(err)
}
connWriteCond := c.messageWriter.writeCond.Signaled()
c.pex.Init(c)
require.True(t, c.pex.IsEnabled(), "should get enabled")
defer c.pex.Close()
var out pp.Message
writerCalled := false
testWriter := func(m pp.Message) bool {
writerCalled = true
out = m
return true
}
<-connWriteCond
c.pex.Share(testWriter)
require.True(t, writerCalled)
require.EqualValues(t, pp.Extended, out.Type)
require.EqualValues(t, pexExtendedId, out.ExtendedID)
x, err := pp.LoadPexMsg(out.ExtendedPayload)
require.NoError(t, err)
targx := pp.PexMsg{
Added: krpc.CompactIPv4NodeAddrs(nil),
AddedFlags: []pp.PexPeerFlags{},
Added6: krpc.CompactIPv6NodeAddrs{
mustNodeAddr(addr),
},
Added6Flags: []pp.PexPeerFlags{0},
}
require.EqualValues(t, targx, x)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leesj/go-torrent.git
git@gitee.com:leesj/go-torrent.git
leesj
go-torrent
go-torrent
master

搜索帮助