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

View file

@ -13,6 +13,7 @@ import (
"github.com/chihaya/bencode" "github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
_ "github.com/chihaya/chihaya/drivers/backend/noop" _ "github.com/chihaya/chihaya/drivers/backend/noop"
_ "github.com/chihaya/chihaya/tracker/memory" _ "github.com/chihaya/chihaya/tracker/memory"
@ -30,8 +31,12 @@ func setupTracker(cfg *config.Config) (*httptest.Server, error) {
return createServer(tkr, cfg) return createServer(tkr, cfg)
} }
func createServer(tkr *Tracker, cfg *config.Config) (*httptest.Server, error) { func createServer(tkr *tracker.Tracker, cfg *config.Config) (*httptest.Server, error) {
return httptest.NewServer(NewRouter(tkr, cfg)), nil srv := &Server{
config: cfg,
tracker: tkr,
}
return httptest.NewServer(NewRouter(srv)), nil
} }
func announce(p params, srv *httptest.Server) ([]byte, error) { 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["interval"] = res.Interval
dict["min interval"] = res.MinInterval dict["min interval"] = res.MinInterval
if res.IPv4Peers != nil || res.IPv4Peers != nil {
if res.Compact { if res.Compact {
dict["peers"] = compactPeers(false, res.IPv4Peers) dict["peers"] = compactPeers(false, res.IPv4Peers)
dict["peers6"] = compactPeers(true, res.IPv6Peers) dict["peers6"] = compactPeers(true, res.IPv6Peers)
} else { } else {
dict["peers"] = peersList(res.IPv6Peers, res.IPv4Peers) dict["peers"] = peersList(res.IPv6Peers, res.IPv4Peers)
} }
}
bencoder := bencode.NewEncoder(w) bencoder := bencode.NewEncoder(w)
return bencoder.Encode(dict) 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) { func peersList(ipv4s, ipv6s tracker.PeerList) (peers []bencode.Dict) {
for _, peer := range ipv4s { for _, peer := range ipv4s {
peers = append(peers, peerDict(peer)) peers = append(peers, peerDict(&peer))
} }
for _, peer := range ipv6s { for _, peer := range ipv6s {
peers = append(peers, peerDict(peer)) peers = append(peers, peerDict(&peer))
} }
return peers return peers
} }

View file

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