Update tests and fix a couple failures

This commit is contained in:
Justin Li 2014-07-17 00:41:59 -04:00
parent da19ed3e21
commit ed126dda29
5 changed files with 52 additions and 57 deletions

View file

@ -10,13 +10,14 @@ import (
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
)
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
tkr, err := NewTracker(&cfg)
tkr, err := tracker.New(&cfg)
if err != nil {
t.Fatal(err)
}
@ -63,7 +64,7 @@ func TestPrivateAnnounce(t *testing.T) {
checkAnnounce(peer, expected, srv, t)
}
func loadTestData(tkr *Tracker) error {
func loadTestData(tkr *tracker.Tracker) error {
conn, err := tkr.Pool.Get()
if err != nil {
return err
@ -76,7 +77,7 @@ func loadTestData(tkr *Tracker) error {
}
for i, passkey := range users {
err = conn.PutUser(&tracker.User{
err = conn.PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
@ -91,11 +92,11 @@ func loadTestData(tkr *Tracker) error {
return err
}
torrent := &tracker.Torrent{
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
Seeders: make(map[string]tracker.Peer),
Leechers: make(map[string]tracker.Peer),
Seeders: make(map[string]models.Peer),
Leechers: make(map[string]models.Peer),
}
return conn.PutTorrent(torrent)

View file

@ -13,6 +13,7 @@ import (
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
_ "github.com/chihaya/chihaya/drivers/backend/noop"
_ "github.com/chihaya/chihaya/tracker/memory"
@ -30,8 +31,12 @@ func setupTracker(cfg *config.Config) (*httptest.Server, error) {
return createServer(tkr, cfg)
}
func createServer(tkr *Tracker, cfg *config.Config) (*httptest.Server, error) {
return httptest.NewServer(NewRouter(tkr, cfg)), nil
func createServer(tkr *tracker.Tracker, cfg *config.Config) (*httptest.Server, error) {
srv := &Server{
config: cfg,
tracker: tkr,
}
return httptest.NewServer(NewRouter(srv)), nil
}
func announce(p params, srv *httptest.Server) ([]byte, error) {

View file

@ -32,12 +32,14 @@ func (w *Writer) WriteAnnounce(res *tracker.AnnounceResponse) error {
dict["interval"] = res.Interval
dict["min interval"] = res.MinInterval
if res.IPv4Peers != nil || res.IPv4Peers != nil {
if res.Compact {
dict["peers"] = compactPeers(false, res.IPv4Peers)
dict["peers6"] = compactPeers(true, res.IPv6Peers)
} else {
dict["peers"] = peersList(res.IPv6Peers, res.IPv4Peers)
}
}
bencoder := bencode.NewEncoder(w)
return bencoder.Encode(dict)
@ -75,10 +77,10 @@ func compactPeers(ipv6 bool, peers tracker.PeerList) []byte {
func peersList(ipv4s, ipv6s tracker.PeerList) (peers []bencode.Dict) {
for _, peer := range ipv4s {
peers = append(peers, peerDict(peer))
peers = append(peers, peerDict(&peer))
}
for _, peer := range ipv6s {
peers = append(peers, peerDict(peer))
peers = append(peers, peerDict(&peer))
}
return peers
}

View file

@ -82,10 +82,10 @@ func (t *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
conn.PurgeInactiveTorrent(torrent.Infohash)
}
return w.WriteAnnounce(newAnnounceResponse(ann, user, torrent))
return w.WriteAnnounce(newAnnounceResponse(ann, peer, torrent))
}
func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
func updateTorrent(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent) (created bool, err error) {
c.TouchTorrent(t.Infohash)
switch {
@ -104,7 +104,7 @@ func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent
t.Leechers[p.Key()] = *p
default:
if a.Left == 0 {
if ann.Left == 0 {
err = c.PutSeeder(t.Infohash, p)
if err != nil {
return
@ -123,9 +123,9 @@ func updateTorrent(c Conn, a *models.Announce, p *models.Peer, t *models.Torrent
return
}
func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t *models.Torrent) (snatched bool, err error) {
switch {
case a.Event == "stopped" || a.Event == "paused":
case ann.Event == "stopped" || ann.Event == "paused":
if t.InSeederPool(p) {
err = c.DeleteSeeder(t.Infohash, p.Key())
if err != nil {
@ -140,7 +140,7 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
delete(t.Leechers, p.Key())
}
case a.Event == "completed":
case ann.Event == "completed":
err = c.IncrementSnatches(t.Infohash)
if err != nil {
return
@ -155,7 +155,7 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
}
}
case t.InLeecherPool(p) && a.Left == 0:
case t.InLeecherPool(p) && ann.Left == 0:
// A leecher completed but the event was never received.
err = LeecherFinished(c, t.Infohash, p)
if err != nil {
@ -166,44 +166,39 @@ func handleEvent(c Conn, a *models.Announce, p *models.Peer, u *models.User, t *
return
}
func newAnnounceResponse(a *models.Announce, u *models.User, t *models.Torrent) *AnnounceResponse {
func newAnnounceResponse(ann *models.Announce, announcer *models.Peer, t *models.Torrent) *AnnounceResponse {
seedCount := len(t.Seeders)
leechCount := len(t.Leechers)
var peerCount int
if a.Left == 0 {
peerCount = minInt(a.NumWant, leechCount)
} else {
peerCount = minInt(a.NumWant, leechCount+seedCount-1)
}
res := &AnnounceResponse{
Complete: seedCount,
Incomplete: leechCount,
Interval: a.Config.Announce.Duration,
MinInterval: a.Config.MinAnnounce.Duration,
Compact: a.Compact,
Interval: ann.Config.Announce.Duration,
MinInterval: ann.Config.MinAnnounce.Duration,
Compact: ann.Compact,
}
if a.NumWant > 0 && a.Event != "stopped" && a.Event != "paused" {
res.IPv4Peers, res.IPv6Peers = getPeers(a, u, t, peerCount)
if ann.NumWant > 0 && ann.Event != "stopped" && ann.Event != "paused" {
res.IPv4Peers, res.IPv6Peers = getPeers(ann, announcer, t, ann.NumWant)
}
return res
}
func getPeers(a *models.Announce, u *models.User, t *models.Torrent, wanted int) (ipv4s, ipv6s PeerList) {
if a.Left == 0 {
func getPeers(ann *models.Announce, announcer *models.Peer, t *models.Torrent, wanted int) (ipv4s, ipv6s PeerList) {
ipv4s, ipv6s = PeerList{}, PeerList{}
if ann.Left == 0 {
// If they're seeding, give them only leechers.
return appendPeers(nil, nil, u, t.Leechers, wanted)
} else {
// If they're leeching, prioritize giving them seeders.
ipv4s, ipv6s = appendPeers(nil, nil, u, t.Seeders, wanted)
return appendPeers(ipv4s, ipv6s, u, t.Leechers, wanted-len(ipv4s)-len(ipv6s))
}
return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted)
}
func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.Peer, wanted int) (PeerList, PeerList) {
// If they're leeching, prioritize giving them seeders.
ipv4s, ipv6s = appendPeers(ipv4s, ipv6s, announcer, t.Seeders, wanted)
return appendPeers(ipv4s, ipv6s, announcer, t.Leechers, wanted-len(ipv4s)-len(ipv6s))
}
func appendPeers(ipv4s, ipv6s PeerList, announcer *models.Peer, peers map[string]models.Peer, wanted int) (PeerList, PeerList) {
count := 0
for _, peer := range peers {
@ -211,14 +206,14 @@ func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.
break
}
if u != nil && peer.UserID == u.ID {
if peer.ID == announcer.ID || peer.UserID != 0 && peer.UserID == announcer.UserID {
continue
}
if ip := peer.IP.To4(); len(ip) == 4 {
ipv4s = append(ipv4s, &peer)
} else if ip := peer.IP.To16(); len(ip) == 16 {
ipv6s = append(ipv6s, &peer)
if ip := peer.IP.To4(); ip != nil {
ipv4s = append(ipv4s, peer)
} else if ip := peer.IP.To16(); ip != nil {
ipv6s = append(ipv6s, peer)
}
count++
@ -226,11 +221,3 @@ func appendPeers(ipv4s, ipv6s PeerList, u *models.User, peers map[string]models.
return ipv4s, ipv6s
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}

View file

@ -10,7 +10,7 @@ import (
"github.com/chihaya/chihaya/tracker/models"
)
type PeerList []*models.Peer
type PeerList []models.Peer
type AnnounceResponse struct {
Complete, Incomplete int