Add tests for torrent purging
This commit is contained in:
parent
ea72e9e10c
commit
84e1c169c0
2 changed files with 56 additions and 3 deletions
|
@ -5,9 +5,12 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/bencode"
|
"github.com/chihaya/bencode"
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya/config"
|
||||||
|
@ -52,6 +55,51 @@ func TestPublicAnnounce(t *testing.T) {
|
||||||
checkAnnounce(peer, expected, srv, t)
|
checkAnnounce(peer, expected, srv, 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)
|
||||||
|
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.Fatal("expected torrent to exist (got %s)", http.StatusText(status))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove seeder.
|
||||||
|
peer = makePeerParams("peer1", true)
|
||||||
|
peer["event"] = "stopped"
|
||||||
|
announce(peer, srv)
|
||||||
|
|
||||||
|
time.Sleep(1000 * time.Millisecond)
|
||||||
|
|
||||||
|
_, 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 makePeerParams(id string, seed bool) params {
|
func makePeerParams(id string, seed bool) params {
|
||||||
left := "1"
|
left := "1"
|
||||||
if seed {
|
if seed {
|
||||||
|
|
|
@ -40,14 +40,19 @@ func announce(p params, srv *httptest.Server) ([]byte, error) {
|
||||||
values.Add(k, v)
|
values.Add(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := http.Get(srv.URL + "/announce?" + values.Encode())
|
body, _, err := fetchPath(srv.URL + "/announce?" + values.Encode())
|
||||||
|
return body, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchPath(path string) ([]byte, int, error) {
|
||||||
|
response, err := http.Get(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(response.Body)
|
body, err := ioutil.ReadAll(response.Body)
|
||||||
response.Body.Close()
|
response.Body.Close()
|
||||||
return body, err
|
return body, response.StatusCode, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type peerList bencode.List
|
type peerList bencode.List
|
||||||
|
|
Loading…
Add table
Reference in a new issue