Move backend to root level, combine announce tests, rename files

This commit is contained in:
Justin Li 2014-07-17 01:26:34 -04:00
parent 67a8473f6e
commit 199496bfa6
8 changed files with 95 additions and 107 deletions

View file

@ -8,7 +8,7 @@ package noop
import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/backend"
"github.com/chihaya/chihaya/backend"
"github.com/chihaya/chihaya/tracker/models"
)

View file

@ -17,7 +17,7 @@ import (
"github.com/chihaya/chihaya/tracker"
// See the README for how to import custom drivers.
_ "github.com/chihaya/chihaya/drivers/backend/noop"
_ "github.com/chihaya/chihaya/backend/noop"
_ "github.com/chihaya/chihaya/tracker/memory"
)

View file

@ -1,103 +0,0 @@
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"testing"
"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 := tracker.New(&cfg)
if err != nil {
t.Fatal(err)
}
err = loadTestData(tkr)
if err != nil {
t.Fatal(err)
}
srv, err := createServer(tkr, &cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
baseURL := srv.URL
peer := makePeerParams("-TR2820-peer1", false)
expected := makeResponse(0, 1, bencode.List{})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer2", false)
expected = makeResponse(0, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer3", true)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
makePeerResponse("-TR2820-peer2"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer1", false)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer2"),
makePeerResponse("-TR2820-peer3"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t)
}
func loadTestData(tkr *tracker.Tracker) error {
conn, err := tkr.Pool.Get()
if err != nil {
return err
}
users := []string{
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
}
for i, passkey := range users {
err = conn.PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
if err != nil {
return err
}
}
err = conn.PutClient("TR2820")
if err != nil {
return err
}
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
Seeders: models.PeerMap{},
Leechers: models.PeerMap{},
}
return conn.PutTorrent(torrent)
}

View file

@ -14,6 +14,8 @@ import (
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
)
func TestPublicAnnounce(t *testing.T) {
@ -131,6 +133,57 @@ func TestStalePeerPurging(t *testing.T) {
}
}
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
tkr, err := tracker.New(&cfg)
if err != nil {
t.Fatal(err)
}
err = loadPrivateTestData(tkr)
if err != nil {
t.Fatal(err)
}
srv, err := createServer(tkr, &cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
baseURL := srv.URL
peer := makePeerParams("-TR2820-peer1", false)
expected := makeResponse(0, 1, bencode.List{})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer2", false)
expected = makeResponse(0, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer3", true)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
makePeerResponse("-TR2820-peer2"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer1", false)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer2"),
makePeerResponse("-TR2820-peer3"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t)
}
func makePeerParams(id string, seed bool) params {
left := "1"
if seed {
@ -193,3 +246,41 @@ func checkAnnounce(p params, expected interface{}, srv *httptest.Server, t *test
}
return true
}
func loadPrivateTestData(tkr *tracker.Tracker) error {
conn, err := tkr.Pool.Get()
if err != nil {
return err
}
users := []string{
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
}
for i, passkey := range users {
err = conn.PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
if err != nil {
return err
}
}
err = conn.PutClient("TR2820")
if err != nil {
return err
}
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
Seeders: models.PeerMap{},
Leechers: models.PeerMap{},
}
return conn.PutTorrent(torrent)
}

View file

@ -15,7 +15,7 @@ import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
_ "github.com/chihaya/chihaya/drivers/backend/noop"
_ "github.com/chihaya/chihaya/backend/noop"
_ "github.com/chihaya/chihaya/tracker/memory"
)

View file

@ -8,7 +8,7 @@ package tracker
import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/backend"
"github.com/chihaya/chihaya/backend"
"github.com/chihaya/chihaya/tracker/models"
)