Add peer purging test, and ensure the purging routine runs whenever a tracker is created

This commit is contained in:
Justin Li 2014-07-16 20:08:03 -04:00
parent 0f2ed791e5
commit a2e14f79c2
2 changed files with 41 additions and 21 deletions

View file

@ -56,13 +56,8 @@ func TestPublicAnnounce(t *testing.T) {
}
func TestTorrentPurging(t *testing.T) {
config := config.DefaultConfig
config.Tracker.Params = map[string]string{
"purge_inactive": "200ms",
"purge_interval": "100ms",
}
srv, err := setupTracker(&config)
cfg := config.DefaultConfig
srv, err := setupTracker(&cfg)
if err != nil {
t.Fatal(err)
}
@ -82,21 +77,46 @@ func TestTorrentPurging(t *testing.T) {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
}
time.Sleep(1010 * time.Millisecond)
// Remove seeder.
peer = makePeerParams("peer1", true)
peer["event"] = "stopped"
announce(peer, srv)
_, status, err = fetchPath(torrentApiPath)
if err != nil {
t.Fatal(err)
}
if status != http.StatusNotFound {
t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status))
}
}
func TestStalePeerPurging(t *testing.T) {
cfg := config.DefaultConfig
cfg.Announce = config.Duration{10 * time.Millisecond}
srv, err := setupTracker(&cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
torrentApiPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
// Add one seeder.
peer := makePeerParams("peer1", true)
announce(peer, srv)
_, status, err := fetchPath(torrentApiPath)
if err != nil {
t.Fatal(err)
}
if status != http.StatusOK {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
}
// Remove seeder.
peer = makePeerParams("peer1", true)
peer["event"] = "stopped"
announce(peer, srv)
time.Sleep(1010 * time.Millisecond)
// Let them expire.
time.Sleep(50 * time.Millisecond)
_, status, err = fetchPath(torrentApiPath)
if err != nil {

View file

@ -36,6 +36,13 @@ func NewTracker(cfg *config.Config) (*Tracker, error) {
return nil, err
}
go tracker.PurgeInactivePeers(
tp,
cfg.PurgeInactiveTorrents,
cfg.Announce.Duration*2,
cfg.Announce.Duration,
)
return &Tracker{
cfg: cfg,
pool: tp,
@ -99,13 +106,6 @@ func Serve(cfg *config.Config) {
glog.Fatal("New: ", err)
}
go tracker.PurgeInactivePeers(
t.pool,
cfg.PurgeInactiveTorrents,
cfg.Announce.Duration*2,
cfg.Announce.Duration,
)
glog.V(0).Info("Starting on ", cfg.Addr)
graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, NewRouter(t, cfg))
}