removed all references to "transaction"
This commit is contained in:
parent
31e618c8d9
commit
9382ac482e
10 changed files with 1698 additions and 1708 deletions
config
server
storage/tracker/redis
|
@ -58,8 +58,6 @@ type Config struct {
|
||||||
MinAnnounce Duration `json:"min_announce"`
|
MinAnnounce Duration `json:"min_announce"`
|
||||||
ReadTimeout Duration `json:"read_timeout"`
|
ReadTimeout Duration `json:"read_timeout"`
|
||||||
DefaultNumWant int `json:"default_num_want"`
|
DefaultNumWant int `json:"default_num_want"`
|
||||||
|
|
||||||
TxRetries int `json:"tx_retries"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open is a shortcut to open a file, read it, and generate a Config.
|
// Open is a shortcut to open a file, read it, and generate a Config.
|
||||||
|
|
|
@ -30,8 +30,6 @@
|
||||||
"announce": "30m",
|
"announce": "30m",
|
||||||
"min_announce": "15m",
|
"min_announce": "15m",
|
||||||
"read_timeout": "20s",
|
"read_timeout": "20s",
|
||||||
"default_num_want": 50,
|
"default_num_want": 50
|
||||||
|
|
||||||
"tx_retries": 3
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,25 +23,22 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry failed transactions a specified number of times
|
// Get a connection to the tracker db
|
||||||
for i := 0; i < s.conf.TxRetries; i++ {
|
conn, err := s.dbConnPool.Get()
|
||||||
|
|
||||||
// Start a transaction
|
|
||||||
tx, err := s.dbConnPool.Get()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the user's passkey
|
// Validate the user's passkey
|
||||||
passkey, _ := path.Split(r.URL.Path)
|
passkey, _ := path.Split(r.URL.Path)
|
||||||
user, err := validateUser(tx, passkey)
|
user, err := validateUser(conn, passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
fail(err, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the user's client is whitelisted
|
// Check if the user's client is whitelisted
|
||||||
whitelisted, err := tx.ClientWhitelisted(parsePeerID(peerID))
|
whitelisted, err := conn.ClientWhitelisted(parsePeerID(peerID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -51,7 +48,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the specified torrent
|
// Find the specified torrent
|
||||||
torrent, exists, err := tx.FindTorrent(infohash)
|
torrent, exists, err := conn.FindTorrent(infohash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -62,7 +59,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// If the torrent was pruned and the user is seeding, unprune it
|
// If the torrent was pruned and the user is seeding, unprune it
|
||||||
if !torrent.Active && left == 0 {
|
if !torrent.Active && left == 0 {
|
||||||
err := tx.MarkActive(torrent)
|
err := conn.MarkActive(torrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -89,13 +86,13 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
// Guarantee that no user is in both pools
|
// Guarantee that no user is in both pools
|
||||||
case seeder && leecher:
|
case seeder && leecher:
|
||||||
if left == 0 {
|
if left == 0 {
|
||||||
err := tx.RemoveLeecher(torrent, peer)
|
err := conn.RemoveLeecher(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
leecher = false
|
leecher = false
|
||||||
} else {
|
} else {
|
||||||
err := tx.RemoveSeeder(torrent, peer)
|
err := conn.RemoveSeeder(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -104,14 +101,14 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
case seeder:
|
case seeder:
|
||||||
// Update the peer with the stats from the request
|
// Update the peer with the stats from the request
|
||||||
err := tx.SetSeeder(torrent, peer)
|
err := conn.SetSeeder(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
case leecher:
|
case leecher:
|
||||||
// Update the peer with the stats from the request
|
// Update the peer with the stats from the request
|
||||||
err := tx.SetLeecher(torrent, peer)
|
err := conn.SetLeecher(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -127,17 +124,17 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if left == 0 {
|
if left == 0 {
|
||||||
// Save the peer as a new seeder
|
// Save the peer as a new seeder
|
||||||
err := tx.AddSeeder(torrent, peer)
|
err := conn.AddSeeder(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Save the peer as a new leecher and increment the user's slots
|
// Save the peer as a new leecher and increment the user's slots
|
||||||
err := tx.IncrementSlots(user)
|
err := conn.IncrementSlots(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
err = tx.AddLeecher(torrent, peer)
|
err = conn.AddLeecher(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -148,29 +145,29 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
case event == "stopped" || event == "paused":
|
case event == "stopped" || event == "paused":
|
||||||
if seeder {
|
if seeder {
|
||||||
err := tx.RemoveSeeder(torrent, peer)
|
err := conn.RemoveSeeder(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if leecher {
|
if leecher {
|
||||||
err := tx.RemoveLeecher(torrent, peer)
|
err := conn.RemoveLeecher(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
err = tx.DecrementSlots(user)
|
err = conn.DecrementSlots(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case event == "completed":
|
case event == "completed":
|
||||||
err := tx.RecordSnatch(user, torrent)
|
err := conn.RecordSnatch(user, torrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
if leecher {
|
if leecher {
|
||||||
err := tx.LeecherFinished(torrent, peer)
|
err := conn.LeecherFinished(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -178,7 +175,7 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
case leecher && left == 0:
|
case leecher && left == 0:
|
||||||
// A leecher completed but the event was never received
|
// A leecher completed but the event was never received
|
||||||
err := tx.LeecherFinished(torrent, peer)
|
err := conn.LeecherFinished(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -237,9 +234,6 @@ func (s Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeBencoded(w, "e")
|
writeBencoded(w, "e")
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
|
func (s Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
|
||||||
|
|
|
@ -22,15 +22,15 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a transaction
|
// Get a connection to the tracker db
|
||||||
tx, err := s.dbConnPool.Get()
|
conn, err := s.dbConnPool.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find and validate the user
|
// Find and validate the user
|
||||||
passkey, _ := path.Split(r.URL.Path)
|
passkey, _ := path.Split(r.URL.Path)
|
||||||
_, err = validateUser(tx, passkey)
|
_, err = validateUser(conn, passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err, w, r)
|
fail(err, w, r)
|
||||||
return
|
return
|
||||||
|
@ -40,7 +40,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||||
writeBencoded(w, "files")
|
writeBencoded(w, "files")
|
||||||
if pq.Infohashes != nil {
|
if pq.Infohashes != nil {
|
||||||
for _, infohash := range pq.Infohashes {
|
for _, infohash := range pq.Infohashes {
|
||||||
torrent, exists, err := tx.FindTorrent(infohash)
|
torrent, exists, err := conn.FindTorrent(infohash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
} else if infohash, exists := pq.Params["info_hash"]; exists {
|
||||||
torrent, exists, err := tx.FindTorrent(infohash)
|
torrent, exists, err := conn.FindTorrent(infohash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,13 +117,13 @@ func fail(err error, w http.ResponseWriter, r *http.Request) {
|
||||||
w.(http.Flusher).Flush()
|
w.(http.Flusher).Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateUser(tx tracker.Conn, dir string) (*storage.User, error) {
|
func validateUser(conn tracker.Conn, dir string) (*storage.User, error) {
|
||||||
if len(dir) != 34 {
|
if len(dir) != 34 {
|
||||||
return nil, errors.New("Passkey is invalid")
|
return nil, errors.New("Passkey is invalid")
|
||||||
}
|
}
|
||||||
passkey := dir[1:33]
|
passkey := dir[1:33]
|
||||||
|
|
||||||
user, exists, err := tx.FindUser(passkey)
|
user, exists, err := conn.FindUser(passkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
|
|
563
storage/tracker/redis/conn_test.go
Normal file
563
storage/tracker/redis/conn_test.go
Normal file
|
@ -0,0 +1,563 @@
|
||||||
|
// Copyright 2013 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 redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pushrax/chihaya/config"
|
||||||
|
"github.com/pushrax/chihaya/storage"
|
||||||
|
"github.com/pushrax/chihaya/storage/tracker"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createTestConn() tracker.Conn {
|
||||||
|
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
|
||||||
|
panicOnErr(err)
|
||||||
|
conf := &testConfig.Cache
|
||||||
|
|
||||||
|
testPool, err := tracker.Open(conf)
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
newConn, err := testPool.Get()
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
return newConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindUserSuccess(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testUser := createTestUser()
|
||||||
|
|
||||||
|
panicOnErr(conn.AddUser(testUser))
|
||||||
|
foundUser, found, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
if !found {
|
||||||
|
t.Error("user not found", testUser)
|
||||||
|
}
|
||||||
|
if *foundUser != *testUser {
|
||||||
|
t.Error("found user mismatch", *foundUser, testUser)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindUserFail(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testUser := createTestUser()
|
||||||
|
|
||||||
|
foundUser, found, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("user found", foundUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveUser(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testUser := createTestUser()
|
||||||
|
|
||||||
|
panicOnErr(conn.AddUser(testUser))
|
||||||
|
err := conn.RemoveUser(testUser)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundUser, found, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("removed user found", foundUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindTorrentSuccess(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if !found {
|
||||||
|
t.Error("torrent not found", testTorrent)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(foundTorrent, testTorrent) {
|
||||||
|
t.Error("found torrent mismatch", foundTorrent, testTorrent)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindTorrentFail(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("torrent found", foundTorrent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveTorrent(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("removed torrent found", foundTorrent)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientWhitelistSuccess(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testPeerID := "-lt0D30-"
|
||||||
|
|
||||||
|
panicOnErr(conn.WhitelistClient(testPeerID))
|
||||||
|
found, err := conn.ClientWhitelisted(testPeerID)
|
||||||
|
panicOnErr(err)
|
||||||
|
if !found {
|
||||||
|
t.Error("peerID not found", testPeerID)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.UnWhitelistClient(testPeerID))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientWhitelistFail(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testPeerID2 := "TIX0192"
|
||||||
|
|
||||||
|
found, err := conn.ClientWhitelisted(testPeerID2)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("peerID found", testPeerID2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRecordSnatch(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
testUser := createTestUser()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
panicOnErr(conn.AddUser(testUser))
|
||||||
|
|
||||||
|
userSnatches := testUser.Snatches
|
||||||
|
torrentSnatches := testTorrent.Snatches
|
||||||
|
|
||||||
|
panicOnErr(conn.RecordSnatch(testUser, testTorrent))
|
||||||
|
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundUser, _, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
if testUser.Snatches != userSnatches+1 {
|
||||||
|
t.Error("snatch not recorded to local user", testUser.Snatches, userSnatches+1)
|
||||||
|
}
|
||||||
|
if testTorrent.Snatches != torrentSnatches+1 {
|
||||||
|
t.Error("snatch not recorded to local torrent")
|
||||||
|
}
|
||||||
|
if foundUser.Snatches != userSnatches+1 {
|
||||||
|
t.Error("snatch not recorded to cached user", foundUser.Snatches, userSnatches+1)
|
||||||
|
}
|
||||||
|
if foundTorrent.Snatches != torrentSnatches+1 {
|
||||||
|
t.Error("snatch not recorded to cached torrent")
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkActive(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
testTorrent.Active = false
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
|
||||||
|
panicOnErr(conn.MarkActive(testTorrent))
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
if foundTorrent.Active != true {
|
||||||
|
t.Error("cached torrent not activated")
|
||||||
|
}
|
||||||
|
if testTorrent.Active != true {
|
||||||
|
t.Error("cached torrent not activated")
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientWhitelistRemove(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testPeerID := "-lt0D30-"
|
||||||
|
panicOnErr(conn.WhitelistClient(testPeerID))
|
||||||
|
panicOnErr(conn.UnWhitelistClient(testPeerID))
|
||||||
|
|
||||||
|
found, err := conn.ClientWhitelisted(testPeerID)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("removed peerID found", testPeerID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddSeeder(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
|
||||||
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundSeeder, found := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if found && foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not added to cache", testSeeder)
|
||||||
|
}
|
||||||
|
foundSeeder, found = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if found && foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not added to local", testSeeder)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddLeecher(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
|
||||||
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found && foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not added to cache", testLeecher)
|
||||||
|
}
|
||||||
|
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found && foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not added to local", testLeecher)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveSeeder(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
|
|
||||||
|
panicOnErr(conn.RemoveSeeder(testTorrent, testSeeder))
|
||||||
|
foundSeeder, found := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if found || foundSeeder == *testSeeder {
|
||||||
|
t.Error("seeder not removed from local", foundSeeder)
|
||||||
|
}
|
||||||
|
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundSeeder, found = foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if found || foundSeeder == *testSeeder {
|
||||||
|
t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveLeecher(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
|
|
||||||
|
panicOnErr(conn.RemoveLeecher(testTorrent, testLeecher))
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found || foundLeecher == *testLeecher {
|
||||||
|
t.Error("leecher not removed from cache", foundLeecher, *testLeecher)
|
||||||
|
}
|
||||||
|
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found || foundLeecher == *testLeecher {
|
||||||
|
t.Error("leecher not removed from local", foundLeecher, *testLeecher)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetSeeder(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
|
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
testSeeder.Uploaded += uint64(r.Int63())
|
||||||
|
|
||||||
|
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
|
||||||
|
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
|
||||||
|
}
|
||||||
|
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetLeecher(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
|
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
testLeecher.Uploaded += uint64(r.Int63())
|
||||||
|
|
||||||
|
panicOnErr(conn.SetLeecher(testTorrent, testLeecher))
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundLeecher, _ := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not updated in cache", testLeecher)
|
||||||
|
}
|
||||||
|
foundLeecher, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not updated in local", testLeecher)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIncrementSlots(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testUser := createTestUser()
|
||||||
|
panicOnErr(conn.AddUser(testUser))
|
||||||
|
numSlots := testUser.Slots
|
||||||
|
|
||||||
|
panicOnErr(conn.IncrementSlots(testUser))
|
||||||
|
foundUser, _, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
if foundUser.Slots != numSlots+1 {
|
||||||
|
t.Error("cached slots not incremented")
|
||||||
|
}
|
||||||
|
if testUser.Slots != numSlots+1 {
|
||||||
|
t.Error("local slots not incremented")
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecrementSlots(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testUser := createTestUser()
|
||||||
|
panicOnErr(conn.AddUser(testUser))
|
||||||
|
numSlots := testUser.Slots
|
||||||
|
|
||||||
|
panicOnErr(conn.DecrementSlots(testUser))
|
||||||
|
foundUser, _, err := conn.FindUser(testUser.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
|
||||||
|
if foundUser.Slots != numSlots-1 {
|
||||||
|
t.Error("cached slots not incremented")
|
||||||
|
}
|
||||||
|
if testUser.Slots != numSlots-1 {
|
||||||
|
t.Error("local slots not incremented")
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLeecherFinished(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
|
testLeecher.Left = 0
|
||||||
|
|
||||||
|
panicOnErr(conn.LeecherFinished(testTorrent, testLeecher))
|
||||||
|
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundSeeder != *testLeecher {
|
||||||
|
t.Error("seeder not added to cache", foundSeeder, *testLeecher)
|
||||||
|
}
|
||||||
|
foundSeeder, _ = foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundSeeder == *testLeecher {
|
||||||
|
t.Error("leecher not removed from cache", testLeecher)
|
||||||
|
}
|
||||||
|
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundSeeder != *testLeecher {
|
||||||
|
t.Error("seeder not added to local", testLeecher)
|
||||||
|
}
|
||||||
|
foundSeeder, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if foundSeeder == *testLeecher {
|
||||||
|
t.Error("leecher not removed from local", testLeecher)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add, update, verify remove
|
||||||
|
func TestUpdatePeer(t *testing.T) {
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
|
// Update a seeder, set it, then check to make sure it updated
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
testSeeder.Uploaded += uint64(r.Int63())
|
||||||
|
|
||||||
|
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
|
||||||
|
|
||||||
|
panicOnErr(conn.RemoveSeeder(testTorrent, testSeeder))
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if seeder, exists := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
|
||||||
|
t.Error("seeder not removed from cache", seeder)
|
||||||
|
}
|
||||||
|
if seeder, exists := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
|
||||||
|
t.Error("seeder not removed from local", seeder)
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParallelFindUser(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
conn := createTestConn()
|
||||||
|
testUserSuccess := createTestUser()
|
||||||
|
testUserFail := createTestUser()
|
||||||
|
panicOnErr(conn.AddUser(testUserSuccess))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
foundUser, found, err := conn.FindUser(testUserFail.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("user found", foundUser)
|
||||||
|
}
|
||||||
|
foundUser, found, err = conn.FindUser(testUserSuccess.Passkey)
|
||||||
|
panicOnErr(err)
|
||||||
|
if !found {
|
||||||
|
t.Error("user not found", testUserSuccess)
|
||||||
|
}
|
||||||
|
if *foundUser != *testUserSuccess {
|
||||||
|
t.Error("found user mismatch", *foundUser, testUserSuccess)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveUser(testUserSuccess))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParallelFindTorrent(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrentSuccess := createTestTorrent()
|
||||||
|
testTorrentFail := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrentSuccess))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrentSuccess.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if !found {
|
||||||
|
t.Error("torrent not found", testTorrentSuccess)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(foundTorrent, testTorrentSuccess) {
|
||||||
|
t.Error("found torrent mismatch", foundTorrent, testTorrentSuccess)
|
||||||
|
}
|
||||||
|
foundTorrent, found, err = conn.FindTorrent(testTorrentFail.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
if found {
|
||||||
|
t.Error("torrent found", foundTorrent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrentSuccess))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParallelSetSeeder(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
testSeeder.Uploaded += uint64(r.Int63())
|
||||||
|
|
||||||
|
panicOnErr(conn.SetSeeder(testTorrent, testSeeder))
|
||||||
|
|
||||||
|
foundTorrent, _, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
|
||||||
|
}
|
||||||
|
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
||||||
|
if foundSeeder != *testSeeder {
|
||||||
|
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParallelAddLeecher(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip()
|
||||||
|
}
|
||||||
|
conn := createTestConn()
|
||||||
|
testTorrent := createTestTorrent()
|
||||||
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
|
|
||||||
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
|
|
||||||
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
|
panicOnErr(err)
|
||||||
|
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found && foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not added to cache", testLeecher)
|
||||||
|
}
|
||||||
|
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
||||||
|
if found && foundLeecher != *testLeecher {
|
||||||
|
t.Error("leecher not added to local", testLeecher)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cleanup
|
||||||
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
|
}
|
|
@ -90,26 +90,26 @@ func (p *Pool) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Get() (tracker.Conn, error) {
|
func (p *Pool) Get() (tracker.Conn, error) {
|
||||||
retTx := &Tx{
|
newConn := &Conn{
|
||||||
conf: p.conf,
|
conf: p.conf,
|
||||||
done: false,
|
done: false,
|
||||||
Conn: p.pool.Get(),
|
Conn: p.pool.Get(),
|
||||||
}
|
}
|
||||||
return retTx, nil
|
return newConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tx struct {
|
type Conn struct {
|
||||||
conf *config.DataStore
|
conf *config.DataStore
|
||||||
done bool
|
done bool
|
||||||
redis.Conn
|
redis.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Tx) close() {
|
func (conn *Conn) close() {
|
||||||
if tx.done {
|
if conn.done {
|
||||||
panic("redis: transaction closed twice")
|
panic("redis: connection closed twice")
|
||||||
}
|
}
|
||||||
tx.done = true
|
conn.done = true
|
||||||
tx.Conn.Close()
|
conn.Conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// createUser takes a string slice of length 14 and returns a pointer to a new
|
// createUser takes a string slice of length 14 and returns a pointer to a new
|
||||||
|
@ -158,7 +158,7 @@ func createUser(userVals []string) (*storage.User, error) {
|
||||||
// If the field values cannot be converted to the correct type,
|
// If the field values cannot be converted to the correct type,
|
||||||
// createTorrent will return a nil user and the conversion error.
|
// createTorrent will return a nil user and the conversion error.
|
||||||
// After converting the torrent fields, the seeders and leechers are populated by redis.getPeers
|
// After converting the torrent fields, the seeders and leechers are populated by redis.getPeers
|
||||||
func (tx *Tx) createTorrent(torrentVals []string) (*storage.Torrent, error) {
|
func (conn *Conn) createTorrent(torrentVals []string) (*storage.Torrent, error) {
|
||||||
if len(torrentVals) != 14 {
|
if len(torrentVals) != 14 {
|
||||||
return nil, ErrCreateTorrent
|
return nil, ErrCreateTorrent
|
||||||
}
|
}
|
||||||
|
@ -185,11 +185,11 @@ func (tx *Tx) createTorrent(torrentVals []string) (*storage.Torrent, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
torrent.Seeders, err = tx.getPeers(torrent.ID, SeedersPrefix)
|
torrent.Seeders, err = conn.getPeers(torrent.ID, SeedersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
torrent.Leechers, err = tx.getPeers(torrent.ID, LeechersPrefix)
|
torrent.Leechers, err = conn.getPeers(torrent.ID, LeechersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -198,9 +198,9 @@ func (tx *Tx) createTorrent(torrentVals []string) (*storage.Torrent, error) {
|
||||||
|
|
||||||
// setPeer writes or overwrites peer information, stored as a Redis hash.
|
// setPeer writes or overwrites peer information, stored as a Redis hash.
|
||||||
// The hash fields names are the same as the JSON tags on the storage.Peer struct.
|
// The hash fields names are the same as the JSON tags on the storage.Peer struct.
|
||||||
func (tx *Tx) setPeer(peer *storage.Peer) error {
|
func (conn *Conn) setPeer(peer *storage.Peer) error {
|
||||||
hashKey := tx.conf.Prefix + getPeerHashKey(peer)
|
hashKey := conn.conf.Prefix + getPeerHashKey(peer)
|
||||||
_, err := tx.Do("HMSET", hashKey,
|
_, err := conn.Do("HMSET", hashKey,
|
||||||
"id", peer.ID,
|
"id", peer.ID,
|
||||||
"user_id", peer.UserID,
|
"user_id", peer.UserID,
|
||||||
"torrent_id", peer.TorrentID,
|
"torrent_id", peer.TorrentID,
|
||||||
|
@ -218,14 +218,14 @@ func (tx *Tx) setPeer(peer *storage.Peer) error {
|
||||||
// and removes the peer information.
|
// and removes the peer information.
|
||||||
// This function calls multiple redis commands, it's not internally atomic.
|
// This function calls multiple redis commands, it's not internally atomic.
|
||||||
// This function will not return an error if the peer to remove doesn't exist.
|
// This function will not return an error if the peer to remove doesn't exist.
|
||||||
func (tx *Tx) removePeer(peer *storage.Peer, peerTypePrefix string) error {
|
func (conn *Conn) removePeer(peer *storage.Peer, peerTypePrefix string) error {
|
||||||
setKey := tx.conf.Prefix + getPeerSetKey(peerTypePrefix, peer)
|
setKey := conn.conf.Prefix + getPeerSetKey(peerTypePrefix, peer)
|
||||||
_, err := tx.Do("SREM", setKey, getPeerHashKey(peer))
|
_, err := conn.Do("SREM", setKey, getPeerHashKey(peer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
hashKey := tx.conf.Prefix + getPeerHashKey(peer)
|
hashKey := conn.conf.Prefix + getPeerHashKey(peer)
|
||||||
_, err = tx.Do("DEL", hashKey)
|
_, err = conn.Do("DEL", hashKey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,17 +234,17 @@ func (tx *Tx) removePeer(peer *storage.Peer, peerTypePrefix string) error {
|
||||||
// This function will not return an error if the peer to remove doesn't exist.
|
// This function will not return an error if the peer to remove doesn't exist.
|
||||||
// This function will only delete the peer set if all the individual peer deletions were successful
|
// This function will only delete the peer set if all the individual peer deletions were successful
|
||||||
// This function calls multiple redis commands, it's not internally atomic.
|
// This function calls multiple redis commands, it's not internally atomic.
|
||||||
func (tx *Tx) removePeers(torrentID uint64, peers map[string]storage.Peer, peerTypePrefix string) error {
|
func (conn *Conn) removePeers(torrentID uint64, peers map[string]storage.Peer, peerTypePrefix string) error {
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
hashKey := tx.conf.Prefix + getPeerHashKey(&peer)
|
hashKey := conn.conf.Prefix + getPeerHashKey(&peer)
|
||||||
_, err := tx.Do("DEL", hashKey)
|
_, err := conn.Do("DEL", hashKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
delete(peers, storage.PeerMapKey(&peer))
|
delete(peers, storage.PeerMapKey(&peer))
|
||||||
}
|
}
|
||||||
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
setKey := conn.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
||||||
_, err := tx.Do("DEL", setKey)
|
_, err := conn.Do("DEL", setKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -268,14 +268,14 @@ func getPeerSetKey(typePrefix string, peer *storage.Peer) string {
|
||||||
// addPeers adds each peer's key to the specified peer set and saves the peer's information.
|
// addPeers adds each peer's key to the specified peer set and saves the peer's information.
|
||||||
// This function will not return an error if the peer already exists in the set.
|
// This function will not return an error if the peer already exists in the set.
|
||||||
// This function calls multiple redis commands, it's not internally atomic.
|
// This function calls multiple redis commands, it's not internally atomic.
|
||||||
func (tx *Tx) addPeers(peers map[string]storage.Peer, peerTypePrefix string) error {
|
func (conn *Conn) addPeers(peers map[string]storage.Peer, peerTypePrefix string) error {
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
setKey := tx.conf.Prefix + getPeerSetKey(peerTypePrefix, &peer)
|
setKey := conn.conf.Prefix + getPeerSetKey(peerTypePrefix, &peer)
|
||||||
_, err := tx.Do("SADD", setKey, getPeerHashKey(&peer))
|
_, err := conn.Do("SADD", setKey, getPeerHashKey(&peer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tx.setPeer(&peer)
|
conn.setPeer(&peer)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -322,17 +322,17 @@ func createPeer(peerVals []string) (*storage.Peer, error) {
|
||||||
|
|
||||||
// getPeers returns a map of peers from a specified torrent's peer set(seeders or leechers).
|
// getPeers returns a map of peers from a specified torrent's peer set(seeders or leechers).
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) getPeers(torrentID uint64, peerTypePrefix string) (peers map[string]storage.Peer, err error) {
|
func (conn *Conn) getPeers(torrentID uint64, peerTypePrefix string) (peers map[string]storage.Peer, err error) {
|
||||||
peers = make(map[string]storage.Peer)
|
peers = make(map[string]storage.Peer)
|
||||||
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
setKey := conn.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
||||||
peerStrings, err := redis.Strings(tx.Do("SMEMBERS", setKey))
|
peerStrings, err := redis.Strings(conn.Do("SMEMBERS", setKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Keys map to peer objects stored in hashes
|
// Keys map to peer objects stored in hashes
|
||||||
for _, peerHashKey := range peerStrings {
|
for _, peerHashKey := range peerStrings {
|
||||||
hashKey := tx.conf.Prefix + peerHashKey
|
hashKey := conn.conf.Prefix + peerHashKey
|
||||||
peerVals, err := redis.Strings(tx.Do("HGETALL", hashKey))
|
peerVals, err := redis.Strings(conn.Do("HGETALL", hashKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -351,9 +351,9 @@ func (tx *Tx) getPeers(torrentID uint64, peerTypePrefix string) (peers map[strin
|
||||||
// AddTorrent writes/overwrites torrent information and saves peers from both peer sets.
|
// AddTorrent writes/overwrites torrent information and saves peers from both peer sets.
|
||||||
// The hash fields names are the same as the JSON tags on the storage.Torrent struct.
|
// The hash fields names are the same as the JSON tags on the storage.Torrent struct.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) AddTorrent(t *storage.Torrent) error {
|
func (conn *Conn) AddTorrent(t *storage.Torrent) error {
|
||||||
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
|
hashkey := conn.conf.Prefix + TorrentPrefix + t.Infohash
|
||||||
_, err := tx.Do("HMSET", hashkey,
|
_, err := conn.Do("HMSET", hashkey,
|
||||||
"id", t.ID,
|
"id", t.ID,
|
||||||
"infohash", t.Infohash,
|
"infohash", t.Infohash,
|
||||||
"active", t.Active,
|
"active", t.Active,
|
||||||
|
@ -365,11 +365,11 @@ func (tx *Tx) AddTorrent(t *storage.Torrent) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tx.addPeers(t.Seeders, SeedersPrefix)
|
err = conn.addPeers(t.Seeders, SeedersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = tx.addPeers(t.Leechers, LeechersPrefix)
|
err = conn.addPeers(t.Leechers, LeechersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -379,18 +379,18 @@ func (tx *Tx) AddTorrent(t *storage.Torrent) error {
|
||||||
// RemoveTorrent deletes the torrent's Redis hash and then deletes all peers.
|
// RemoveTorrent deletes the torrent's Redis hash and then deletes all peers.
|
||||||
// This function will not return an error if the torrent has already been removed.
|
// This function will not return an error if the torrent has already been removed.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) RemoveTorrent(t *storage.Torrent) error {
|
func (conn *Conn) RemoveTorrent(t *storage.Torrent) error {
|
||||||
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
|
hashkey := conn.conf.Prefix + TorrentPrefix + t.Infohash
|
||||||
_, err := tx.Do("DEL", hashkey)
|
_, err := conn.Do("DEL", hashkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Remove seeders and leechers as well
|
// Remove seeders and leechers as well
|
||||||
err = tx.removePeers(t.ID, t.Seeders, SeedersPrefix)
|
err = conn.removePeers(t.ID, t.Seeders, SeedersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = tx.removePeers(t.ID, t.Leechers, LeechersPrefix)
|
err = conn.removePeers(t.ID, t.Leechers, LeechersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -399,9 +399,9 @@ func (tx *Tx) RemoveTorrent(t *storage.Torrent) error {
|
||||||
|
|
||||||
// AddUser writes/overwrites user information to a Redis hash.
|
// AddUser writes/overwrites user information to a Redis hash.
|
||||||
// The hash fields names are the same as the JSON tags on the storage.user struct.
|
// The hash fields names are the same as the JSON tags on the storage.user struct.
|
||||||
func (tx *Tx) AddUser(u *storage.User) error {
|
func (conn *Conn) AddUser(u *storage.User) error {
|
||||||
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
hashkey := conn.conf.Prefix + UserPrefix + u.Passkey
|
||||||
_, err := tx.Do("HMSET", hashkey,
|
_, err := conn.Do("HMSET", hashkey,
|
||||||
"id", u.ID,
|
"id", u.ID,
|
||||||
"passkey", u.Passkey,
|
"passkey", u.Passkey,
|
||||||
"up_multiplier", u.UpMultiplier,
|
"up_multiplier", u.UpMultiplier,
|
||||||
|
@ -417,9 +417,9 @@ func (tx *Tx) AddUser(u *storage.User) error {
|
||||||
|
|
||||||
// RemoveUser removes the user's hash from Redis.
|
// RemoveUser removes the user's hash from Redis.
|
||||||
// This function does not return an error if the user doesn't exist.
|
// This function does not return an error if the user doesn't exist.
|
||||||
func (tx *Tx) RemoveUser(u *storage.User) error {
|
func (conn *Conn) RemoveUser(u *storage.User) error {
|
||||||
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
hashkey := conn.conf.Prefix + UserPrefix + u.Passkey
|
||||||
_, err := tx.Do("DEL", hashkey)
|
_, err := conn.Do("DEL", hashkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -429,10 +429,10 @@ func (tx *Tx) RemoveUser(u *storage.User) error {
|
||||||
// FindUser returns a pointer to a new user struct and true if the user exists,
|
// FindUser returns a pointer to a new user struct and true if the user exists,
|
||||||
// or nil and false if the user doesn't exist.
|
// or nil and false if the user doesn't exist.
|
||||||
// This function does not return an error if the torrent doesn't exist.
|
// This function does not return an error if the torrent doesn't exist.
|
||||||
func (tx *Tx) FindUser(passkey string) (*storage.User, bool, error) {
|
func (conn *Conn) FindUser(passkey string) (*storage.User, bool, error) {
|
||||||
hashkey := tx.conf.Prefix + UserPrefix + passkey
|
hashkey := conn.conf.Prefix + UserPrefix + passkey
|
||||||
// Consider using HGETALL instead of HVALS here for robustness
|
// Consider using HGETALL instead of HVALS here for robustness
|
||||||
userStrings, err := redis.Strings(tx.Do("HGETALL", hashkey))
|
userStrings, err := redis.Strings(conn.Do("HGETALL", hashkey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
} else if len(userStrings) == 0 {
|
} else if len(userStrings) == 0 {
|
||||||
|
@ -448,16 +448,16 @@ func (tx *Tx) FindUser(passkey string) (*storage.User, bool, error) {
|
||||||
// FindTorrent returns a pointer to a new torrent struct and true if the torrent exists,
|
// FindTorrent returns a pointer to a new torrent struct and true if the torrent exists,
|
||||||
// or nil and false if the torrent doesn't exist.
|
// or nil and false if the torrent doesn't exist.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
|
func (conn *Conn) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
|
||||||
hashkey := tx.conf.Prefix + TorrentPrefix + infohash
|
hashkey := conn.conf.Prefix + TorrentPrefix + infohash
|
||||||
torrentStrings, err := redis.Strings(tx.Do("HGETALL", hashkey))
|
torrentStrings, err := redis.Strings(conn.Do("HGETALL", hashkey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
} else if len(torrentStrings) == 0 {
|
} else if len(torrentStrings) == 0 {
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
foundTorrent, err := tx.createTorrent(torrentStrings)
|
foundTorrent, err := conn.createTorrent(torrentStrings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
@ -467,41 +467,41 @@ func (tx *Tx) FindTorrent(infohash string) (*storage.Torrent, bool, error) {
|
||||||
// ClientWhitelisted returns true if the ClientID exists in the Client set.
|
// ClientWhitelisted returns true if the ClientID exists in the Client set.
|
||||||
// This function does not parse the client ID from the peer ID.
|
// This function does not parse the client ID from the peer ID.
|
||||||
// The clientID must match exactly to a member of the set.
|
// The clientID must match exactly to a member of the set.
|
||||||
func (tx *Tx) ClientWhitelisted(peerID string) (exists bool, err error) {
|
func (conn *Conn) ClientWhitelisted(peerID string) (exists bool, err error) {
|
||||||
key := tx.conf.Prefix + "whitelist"
|
key := conn.conf.Prefix + "whitelist"
|
||||||
return redis.Bool(tx.Do("SISMEMBER", key, peerID))
|
return redis.Bool(conn.Do("SISMEMBER", key, peerID))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WhitelistClient adds a client ID to the client whitelist set.
|
// WhitelistClient adds a client ID to the client whitelist set.
|
||||||
// This function does not return an error if the client ID is already in the set.
|
// This function does not return an error if the client ID is already in the set.
|
||||||
func (tx *Tx) WhitelistClient(peerID string) error {
|
func (conn *Conn) WhitelistClient(peerID string) error {
|
||||||
key := tx.conf.Prefix + "whitelist"
|
key := conn.conf.Prefix + "whitelist"
|
||||||
_, err := tx.Do("SADD", key, peerID)
|
_, err := conn.Do("SADD", key, peerID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnWhitelistClient removes a client ID from the client whitelist set
|
// UnWhitelistClient removes a client ID from the client whitelist set
|
||||||
// This function does not return an error if the client ID is not in the set.
|
// This function does not return an error if the client ID is not in the set.
|
||||||
func (tx *Tx) UnWhitelistClient(peerID string) error {
|
func (conn *Conn) UnWhitelistClient(peerID string) error {
|
||||||
key := tx.conf.Prefix + "whitelist"
|
key := conn.conf.Prefix + "whitelist"
|
||||||
_, err := tx.Do("SREM", key, peerID)
|
_, err := conn.Do("SREM", key, peerID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecordSnatch increments the snatch counter on the torrent and user by one.
|
// RecordSnatch increments the snatch counter on the torrent and user by one.
|
||||||
// This modifies the arguments as well as the hash field in Redis.
|
// This modifies the arguments as well as the hash field in Redis.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) RecordSnatch(user *storage.User, torrent *storage.Torrent) error {
|
func (conn *Conn) RecordSnatch(user *storage.User, torrent *storage.Torrent) error {
|
||||||
|
|
||||||
torrentKey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
torrentKey := conn.conf.Prefix + TorrentPrefix + torrent.Infohash
|
||||||
snatchCount, err := redis.Int(tx.Do("HINCRBY", torrentKey, "snatches", 1))
|
snatchCount, err := redis.Int(conn.Do("HINCRBY", torrentKey, "snatches", 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
torrent.Snatches = uint64(snatchCount)
|
torrent.Snatches = uint64(snatchCount)
|
||||||
|
|
||||||
userKey := tx.conf.Prefix + UserPrefix + user.Passkey
|
userKey := conn.conf.Prefix + UserPrefix + user.Passkey
|
||||||
snatchCount, err = redis.Int(tx.Do("HINCRBY", userKey, "snatches", 1))
|
snatchCount, err = redis.Int(conn.Do("HINCRBY", userKey, "snatches", 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -512,9 +512,9 @@ func (tx *Tx) RecordSnatch(user *storage.User, torrent *storage.Torrent) error {
|
||||||
// MarkActive sets the active field of the torrent to true.
|
// MarkActive sets the active field of the torrent to true.
|
||||||
// This modifies the argument as well as the hash field in Redis.
|
// This modifies the argument as well as the hash field in Redis.
|
||||||
// This function will return ErrMarkActive if the torrent does not exist.
|
// This function will return ErrMarkActive if the torrent does not exist.
|
||||||
func (tx *Tx) MarkActive(torrent *storage.Torrent) error {
|
func (conn *Conn) MarkActive(torrent *storage.Torrent) error {
|
||||||
hashkey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
hashkey := conn.conf.Prefix + TorrentPrefix + torrent.Infohash
|
||||||
activeExists, err := redis.Int(tx.Do("HSET", hashkey, "active", true))
|
activeExists, err := redis.Int(conn.Do("HSET", hashkey, "active", true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -529,9 +529,9 @@ func (tx *Tx) MarkActive(torrent *storage.Torrent) error {
|
||||||
// MarkInactive sets the active field of the torrent to false.
|
// MarkInactive sets the active field of the torrent to false.
|
||||||
// This modifies the argument as well as the hash field in Redis.
|
// This modifies the argument as well as the hash field in Redis.
|
||||||
// This function will return ErrMarkActive if the torrent does not exist.
|
// This function will return ErrMarkActive if the torrent does not exist.
|
||||||
func (tx *Tx) MarkInactive(torrent *storage.Torrent) error {
|
func (conn *Conn) MarkInactive(torrent *storage.Torrent) error {
|
||||||
hashkey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
hashkey := conn.conf.Prefix + TorrentPrefix + torrent.Infohash
|
||||||
activeExists, err := redis.Int(tx.Do("HSET", hashkey, "active", false))
|
activeExists, err := redis.Int(conn.Do("HSET", hashkey, "active", false))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -539,7 +539,7 @@ func (tx *Tx) MarkInactive(torrent *storage.Torrent) error {
|
||||||
// HSET returns 1 if hash didn't exist before
|
// HSET returns 1 if hash didn't exist before
|
||||||
if activeExists == 1 {
|
if activeExists == 1 {
|
||||||
// Clean-up incomplete torrent
|
// Clean-up incomplete torrent
|
||||||
_, err = tx.Do("DEL", hashkey)
|
_, err = conn.Do("DEL", hashkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -552,13 +552,13 @@ func (tx *Tx) MarkInactive(torrent *storage.Torrent) error {
|
||||||
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
||||||
// This function does not return an error if the leecher already exists.
|
// This function does not return an error if the leecher already exists.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) AddLeecher(torrent *storage.Torrent, peer *storage.Peer) error {
|
func (conn *Conn) AddLeecher(torrent *storage.Torrent, peer *storage.Peer) error {
|
||||||
setKey := tx.conf.Prefix + LeechersPrefix + strconv.FormatUint(torrent.ID, 36)
|
setKey := conn.conf.Prefix + LeechersPrefix + strconv.FormatUint(torrent.ID, 36)
|
||||||
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
|
_, err := conn.Do("SADD", setKey, getPeerHashKey(peer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = tx.setPeer(peer)
|
err = conn.setPeer(peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -573,8 +573,8 @@ func (tx *Tx) AddLeecher(torrent *storage.Torrent, peer *storage.Peer) error {
|
||||||
// This modifies the torrent argument, as well as the peer's hash in Redis.
|
// This modifies the torrent argument, as well as the peer's hash in Redis.
|
||||||
// Setting assumes that the peer is already a leecher, and only needs to be updated.
|
// Setting assumes that the peer is already a leecher, and only needs to be updated.
|
||||||
// This function does not return an error if the leecher does not exist or is not in the torrent's leecher set.
|
// This function does not return an error if the leecher does not exist or is not in the torrent's leecher set.
|
||||||
func (tx *Tx) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
|
func (conn *Conn) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
err := tx.setPeer(p)
|
err := conn.setPeer(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -585,8 +585,8 @@ func (tx *Tx) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
// RemoveLeecher removes the given peer from a torrent's leecher set.
|
// RemoveLeecher removes the given peer from a torrent's leecher set.
|
||||||
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
||||||
// This function does not return an error if the peer doesn't exist, or is not in the set.
|
// This function does not return an error if the peer doesn't exist, or is not in the set.
|
||||||
func (tx *Tx) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error {
|
func (conn *Conn) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
err := tx.removePeer(p, LeechersPrefix)
|
err := conn.removePeer(p, LeechersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -597,19 +597,19 @@ func (tx *Tx) RemoveLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
// LeecherFinished moves a peer's hashkey from a torrent's leecher set to the seeder set and updates the peer.
|
// LeecherFinished moves a peer's hashkey from a torrent's leecher set to the seeder set and updates the peer.
|
||||||
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
||||||
// This function does not return an error if the peer doesn't exist or is not in the torrent's leecher set.
|
// This function does not return an error if the peer doesn't exist or is not in the torrent's leecher set.
|
||||||
func (tx *Tx) LeecherFinished(torrent *storage.Torrent, peer *storage.Peer) error {
|
func (conn *Conn) LeecherFinished(torrent *storage.Torrent, peer *storage.Peer) error {
|
||||||
torrentIdKey := strconv.FormatUint(torrent.ID, 36)
|
torrentIdKey := strconv.FormatUint(torrent.ID, 36)
|
||||||
seederSetKey := tx.conf.Prefix + SeedersPrefix + torrentIdKey
|
seederSetKey := conn.conf.Prefix + SeedersPrefix + torrentIdKey
|
||||||
leecherSetKey := tx.conf.Prefix + LeechersPrefix + torrentIdKey
|
leecherSetKey := conn.conf.Prefix + LeechersPrefix + torrentIdKey
|
||||||
|
|
||||||
_, err := tx.Do("SMOVE", leecherSetKey, seederSetKey, getPeerHashKey(peer))
|
_, err := conn.Do("SMOVE", leecherSetKey, seederSetKey, getPeerHashKey(peer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
torrent.Seeders[storage.PeerMapKey(peer)] = *peer
|
torrent.Seeders[storage.PeerMapKey(peer)] = *peer
|
||||||
delete(torrent.Leechers, storage.PeerMapKey(peer))
|
delete(torrent.Leechers, storage.PeerMapKey(peer))
|
||||||
|
|
||||||
err = tx.setPeer(peer)
|
err = conn.setPeer(peer)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -617,13 +617,13 @@ func (tx *Tx) LeecherFinished(torrent *storage.Torrent, peer *storage.Peer) erro
|
||||||
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
||||||
// This function does not return an error if the seeder already exists.
|
// This function does not return an error if the seeder already exists.
|
||||||
// This is a multiple action command, it's not internally atomic.
|
// This is a multiple action command, it's not internally atomic.
|
||||||
func (tx *Tx) AddSeeder(torrent *storage.Torrent, peer *storage.Peer) error {
|
func (conn *Conn) AddSeeder(torrent *storage.Torrent, peer *storage.Peer) error {
|
||||||
setKey := tx.conf.Prefix + SeedersPrefix + strconv.FormatUint(torrent.ID, 36)
|
setKey := conn.conf.Prefix + SeedersPrefix + strconv.FormatUint(torrent.ID, 36)
|
||||||
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
|
_, err := conn.Do("SADD", setKey, getPeerHashKey(peer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = tx.setPeer(peer)
|
err = conn.setPeer(peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -638,8 +638,8 @@ func (tx *Tx) AddSeeder(torrent *storage.Torrent, peer *storage.Peer) error {
|
||||||
// This modifies the torrent argument, as well as the peer's hash in Redis.
|
// This modifies the torrent argument, as well as the peer's hash in Redis.
|
||||||
// Setting assumes that the peer is already a seeder, and only needs to be updated.
|
// Setting assumes that the peer is already a seeder, and only needs to be updated.
|
||||||
// This function does not return an error if the seeder does not exist or is not in the torrent's seeder set.
|
// This function does not return an error if the seeder does not exist or is not in the torrent's seeder set.
|
||||||
func (tx *Tx) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
|
func (conn *Conn) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
err := tx.setPeer(p)
|
err := conn.setPeer(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -650,8 +650,8 @@ func (tx *Tx) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
// RemoveSeeder removes the given peer from a torrent's seeder set.
|
// RemoveSeeder removes the given peer from a torrent's seeder set.
|
||||||
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
// This modifies the torrent argument, as well as the torrent's set and peer's hash in Redis.
|
||||||
// This function does not return an error if the peer doesn't exist, or is not in the set.
|
// This function does not return an error if the peer doesn't exist, or is not in the set.
|
||||||
func (tx *Tx) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error {
|
func (conn *Conn) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
err := tx.removePeer(p, SeedersPrefix)
|
err := conn.removePeer(p, SeedersPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -661,9 +661,9 @@ func (tx *Tx) RemoveSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
|
|
||||||
// IncrementSlots increment a user's Slots by one.
|
// IncrementSlots increment a user's Slots by one.
|
||||||
// This function modifies the argument as well as the hash field in Redis.
|
// This function modifies the argument as well as the hash field in Redis.
|
||||||
func (tx *Tx) IncrementSlots(u *storage.User) error {
|
func (conn *Conn) IncrementSlots(u *storage.User) error {
|
||||||
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
hashkey := conn.conf.Prefix + UserPrefix + u.Passkey
|
||||||
slotCount, err := redis.Int(tx.Do("HINCRBY", hashkey, "slots", 1))
|
slotCount, err := redis.Int(conn.Do("HINCRBY", hashkey, "slots", 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -673,9 +673,9 @@ func (tx *Tx) IncrementSlots(u *storage.User) error {
|
||||||
|
|
||||||
// IncrementSlots increment a user's Slots by one.
|
// IncrementSlots increment a user's Slots by one.
|
||||||
// This function modifies the argument as well as the hash field in Redis.
|
// This function modifies the argument as well as the hash field in Redis.
|
||||||
func (tx *Tx) DecrementSlots(u *storage.User) error {
|
func (conn *Conn) DecrementSlots(u *storage.User) error {
|
||||||
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
hashkey := conn.conf.Prefix + UserPrefix + u.Passkey
|
||||||
slotCount, err := redis.Int(tx.Do("HINCRBY", hashkey, "slots", -1))
|
slotCount, err := redis.Int(conn.Do("HINCRBY", hashkey, "slots", -1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,14 @@ import (
|
||||||
|
|
||||||
func BenchmarkSuccessfulFindUser(b *testing.B) {
|
func BenchmarkSuccessfulFindUser(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testUser := createTestUser()
|
testUser := createTestUser()
|
||||||
panicOnErr(tx.AddUser(testUser))
|
panicOnErr(conn.AddUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
|
|
||||||
foundUser, found, err := tx.FindUser(testUser.Passkey)
|
foundUser, found, err := conn.FindUser(testUser.Passkey)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if !found {
|
if !found {
|
||||||
b.Error("user not found", testUser)
|
b.Error("user not found", testUser)
|
||||||
|
@ -30,19 +30,19 @@ func BenchmarkSuccessfulFindUser(b *testing.B) {
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFailedFindUser(b *testing.B) {
|
func BenchmarkFailedFindUser(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testUser := createTestUser()
|
testUser := createTestUser()
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
|
|
||||||
_, found, err := tx.FindUser(testUser.Passkey)
|
_, found, err := conn.FindUser(testUser.Passkey)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if found {
|
if found {
|
||||||
b.Error("user not found", testUser)
|
b.Error("user not found", testUser)
|
||||||
|
@ -52,14 +52,14 @@ func BenchmarkFailedFindUser(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkSuccessfulFindTorrent(b *testing.B) {
|
func BenchmarkSuccessfulFindTorrent(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if !found {
|
if !found {
|
||||||
b.Error("torrent not found", testTorrent)
|
b.Error("torrent not found", testTorrent)
|
||||||
|
@ -71,18 +71,18 @@ func BenchmarkSuccessfulFindTorrent(b *testing.B) {
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFailFindTorrent(b *testing.B) {
|
func BenchmarkFailFindTorrent(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
foundTorrent, found, err := conn.FindTorrent(testTorrent.Infohash)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if found {
|
if found {
|
||||||
b.Error("torrent found", foundTorrent)
|
b.Error("torrent found", foundTorrent)
|
||||||
|
@ -92,13 +92,13 @@ func BenchmarkFailFindTorrent(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkSuccessfulClientWhitelisted(b *testing.B) {
|
func BenchmarkSuccessfulClientWhitelisted(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testPeerID := "-lt0D30-"
|
testPeerID := "-lt0D30-"
|
||||||
panicOnErr(tx.WhitelistClient(testPeerID))
|
panicOnErr(conn.WhitelistClient(testPeerID))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
found, err := tx.ClientWhitelisted(testPeerID)
|
found, err := conn.ClientWhitelisted(testPeerID)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if !found {
|
if !found {
|
||||||
b.Error("peerID not found", testPeerID)
|
b.Error("peerID not found", testPeerID)
|
||||||
|
@ -106,18 +106,18 @@ func BenchmarkSuccessfulClientWhitelisted(b *testing.B) {
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.UnWhitelistClient(testPeerID))
|
panicOnErr(conn.UnWhitelistClient(testPeerID))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFailClientWhitelisted(b *testing.B) {
|
func BenchmarkFailClientWhitelisted(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testPeerID2 := "TIX0192"
|
testPeerID2 := "TIX0192"
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
found, err := tx.ClientWhitelisted(testPeerID2)
|
found, err := conn.ClientWhitelisted(testPeerID2)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if found {
|
if found {
|
||||||
b.Error("peerID found", testPeerID2)
|
b.Error("peerID found", testPeerID2)
|
||||||
|
@ -127,45 +127,45 @@ func BenchmarkFailClientWhitelisted(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkRecordSnatch(b *testing.B) {
|
func BenchmarkRecordSnatch(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
testUser := createTestUser()
|
testUser := createTestUser()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
panicOnErr(tx.AddUser(testUser))
|
panicOnErr(conn.AddUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
panicOnErr(tx.RecordSnatch(testUser, testTorrent))
|
panicOnErr(conn.RecordSnatch(testUser, testTorrent))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkMarkActive(b *testing.B) {
|
func BenchmarkMarkActive(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
testTorrent.Active = false
|
testTorrent.Active = false
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
panicOnErr(tx.MarkActive(testTorrent))
|
panicOnErr(conn.MarkActive(testTorrent))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAddSeeder(b *testing.B) {
|
func BenchmarkAddSeeder(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
|
@ -173,42 +173,42 @@ func BenchmarkAddSeeder(b *testing.B) {
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkRemoveSeeder(b *testing.B) {
|
func BenchmarkRemoveSeeder(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx.AddSeeder(testTorrent, testSeeder)
|
conn.AddSeeder(testTorrent, testSeeder)
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
|
panicOnErr(conn.RemoveSeeder(testTorrent, testSeeder))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSetSeeder(b *testing.B) {
|
func BenchmarkSetSeeder(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
panicOnErr(conn.AddSeeder(testTorrent, testSeeder))
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
|
@ -217,72 +217,72 @@ func BenchmarkSetSeeder(b *testing.B) {
|
||||||
testSeeder.Uploaded += uint64(r.Int63())
|
testSeeder.Uploaded += uint64(r.Int63())
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
tx.SetSeeder(testTorrent, testSeeder)
|
conn.SetSeeder(testTorrent, testSeeder)
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkIncrementSlots(b *testing.B) {
|
func BenchmarkIncrementSlots(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testUser := createTestUser()
|
testUser := createTestUser()
|
||||||
panicOnErr(tx.AddUser(testUser))
|
panicOnErr(conn.AddUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
panicOnErr(tx.IncrementSlots(testUser))
|
panicOnErr(conn.IncrementSlots(testUser))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
panicOnErr(conn.RemoveUser(testUser))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLeecherFinished(b *testing.B) {
|
func BenchmarkLeecherFinished(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
testLeecher.Left = 0
|
testLeecher.Left = 0
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
panicOnErr(tx.LeecherFinished(testTorrent, testLeecher))
|
panicOnErr(conn.LeecherFinished(testTorrent, testLeecher))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
panicOnErr(conn.RemoveTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a comparision to the Leecher finished function
|
// This is a comparision to the Leecher finished function
|
||||||
func BenchmarkRemoveLeecherAddSeeder(b *testing.B) {
|
func BenchmarkRemoveLeecherAddSeeder(b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx := createTestTx()
|
conn := createTestConn()
|
||||||
testTorrent := createTestTorrent()
|
testTorrent := createTestTorrent()
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
panicOnErr(conn.AddTorrent(testTorrent))
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
for bCount := 0; bCount < b.N; bCount++ {
|
for bCount := 0; bCount < b.N; bCount++ {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
panicOnErr(conn.AddLeecher(testTorrent, testLeecher))
|
||||||
testLeecher.Left = 0
|
testLeecher.Left = 0
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
panicOnErr(tx.RemoveLeecher(testTorrent, testLeecher))
|
panicOnErr(conn.RemoveLeecher(testTorrent, testLeecher))
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testLeecher))
|
panicOnErr(conn.AddSeeder(testTorrent, testLeecher))
|
||||||
}
|
}
|
||||||
// Cleanup
|
// Cleanup
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
tx.RemoveTorrent(testTorrent)
|
conn.RemoveTorrent(testTorrent)
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ func panicOnErr(err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestRedisTx() *Tx {
|
func createTestRedisConn() *Conn {
|
||||||
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
|
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
|
||||||
conf := &testConfig.Cache
|
conf := &testConfig.Cache
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
|
@ -98,7 +98,7 @@ func createTestRedisTx() *Tx {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
txObj := &Tx{
|
newConn := &Conn{
|
||||||
conf: testPool.conf,
|
conf: testPool.conf,
|
||||||
done: false,
|
done: false,
|
||||||
Conn: testPool.pool.Get(),
|
Conn: testPool.pool.Get(),
|
||||||
|
@ -106,9 +106,9 @@ func createTestRedisTx() *Tx {
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
|
|
||||||
// Test connection before returning
|
// Test connection before returning
|
||||||
_, err = txObj.Do("PING")
|
_, err = newConn.Do("PING")
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
return txObj
|
return newConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestUser() *storage.User {
|
func createTestUser() *storage.User {
|
||||||
|
@ -145,39 +145,39 @@ func createTestTorrent() *storage.Torrent {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidPeers(t *testing.T) {
|
func TestValidPeers(t *testing.T) {
|
||||||
testTx := createTestRedisTx()
|
testConn := createTestRedisConn()
|
||||||
testTorrentID := createTestTorrentID()
|
testTorrentID := createTestTorrentID()
|
||||||
testPeers := createTestPeers(testTorrentID, 3)
|
testPeers := createTestPeers(testTorrentID, 3)
|
||||||
|
|
||||||
panicOnErr(testTx.addPeers(testPeers, "test:"))
|
panicOnErr(testConn.addPeers(testPeers, "test:"))
|
||||||
peerMap, err := testTx.getPeers(testTorrentID, "test:")
|
peerMap, err := testConn.getPeers(testTorrentID, "test:")
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
if len(peerMap) != len(testPeers) {
|
if len(peerMap) != len(testPeers) {
|
||||||
t.Error("Num Peers not equal ", len(peerMap), len(testPeers))
|
t.Error("Num Peers not equal ", len(peerMap), len(testPeers))
|
||||||
}
|
}
|
||||||
panicOnErr(testTx.removePeers(testTorrentID, testPeers, "test:"))
|
panicOnErr(testConn.removePeers(testTorrentID, testPeers, "test:"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidPeers(t *testing.T) {
|
func TestInvalidPeers(t *testing.T) {
|
||||||
testTx := createTestRedisTx()
|
testConn := createTestRedisConn()
|
||||||
testTorrentID := createTestTorrentID()
|
testTorrentID := createTestTorrentID()
|
||||||
testPeers := createTestPeers(testTorrentID, 3)
|
testPeers := createTestPeers(testTorrentID, 3)
|
||||||
tempPeer := createTestPeer(createTestUserID(), testTorrentID)
|
tempPeer := createTestPeer(createTestUserID(), testTorrentID)
|
||||||
testPeers[storage.PeerMapKey(tempPeer)] = *tempPeer
|
testPeers[storage.PeerMapKey(tempPeer)] = *tempPeer
|
||||||
|
|
||||||
panicOnErr(testTx.addPeers(testPeers, "test:"))
|
panicOnErr(testConn.addPeers(testPeers, "test:"))
|
||||||
// Imitate a peer being removed during get
|
// Imitate a peer being removed during get
|
||||||
hashKey := testTx.conf.Prefix + getPeerHashKey(tempPeer)
|
hashKey := testConn.conf.Prefix + getPeerHashKey(tempPeer)
|
||||||
_, err := testTx.Do("DEL", hashKey)
|
_, err := testConn.Do("DEL", hashKey)
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
|
|
||||||
peerMap, err := testTx.getPeers(testTorrentID, "test:")
|
peerMap, err := testConn.getPeers(testTorrentID, "test:")
|
||||||
panicOnErr(err)
|
panicOnErr(err)
|
||||||
// Expect 1 less peer due to delete
|
// Expect 1 less peer due to delete
|
||||||
if len(peerMap) != len(testPeers)-1 {
|
if len(peerMap) != len(testPeers)-1 {
|
||||||
t.Error("Num Peers not equal ", len(peerMap), len(testPeers)-1)
|
t.Error("Num Peers not equal ", len(peerMap), len(testPeers)-1)
|
||||||
}
|
}
|
||||||
panicOnErr(testTx.removePeers(testTorrentID, testPeers, "test:"))
|
panicOnErr(testConn.removePeers(testTorrentID, testPeers, "test:"))
|
||||||
if len(testPeers) != 0 {
|
if len(testPeers) != 0 {
|
||||||
t.Errorf("All peers not removed, %d peers remain!", len(testPeers))
|
t.Errorf("All peers not removed, %d peers remain!", len(testPeers))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,563 +0,0 @@
|
||||||
// Copyright 2013 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 redis
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pushrax/chihaya/config"
|
|
||||||
"github.com/pushrax/chihaya/storage"
|
|
||||||
"github.com/pushrax/chihaya/storage/tracker"
|
|
||||||
)
|
|
||||||
|
|
||||||
func createTestTx() tracker.Conn {
|
|
||||||
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
|
|
||||||
panicOnErr(err)
|
|
||||||
conf := &testConfig.Cache
|
|
||||||
|
|
||||||
testPool, err := tracker.Open(conf)
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
txObj, err := testPool.Get()
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
return txObj
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFindUserSuccess(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testUser := createTestUser()
|
|
||||||
|
|
||||||
panicOnErr(tx.AddUser(testUser))
|
|
||||||
foundUser, found, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
if !found {
|
|
||||||
t.Error("user not found", testUser)
|
|
||||||
}
|
|
||||||
if *foundUser != *testUser {
|
|
||||||
t.Error("found user mismatch", *foundUser, testUser)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFindUserFail(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testUser := createTestUser()
|
|
||||||
|
|
||||||
foundUser, found, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("user found", foundUser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveUser(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testUser := createTestUser()
|
|
||||||
|
|
||||||
panicOnErr(tx.AddUser(testUser))
|
|
||||||
err := tx.RemoveUser(testUser)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundUser, found, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("removed user found", foundUser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFindTorrentSuccess(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if !found {
|
|
||||||
t.Error("torrent not found", testTorrent)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(foundTorrent, testTorrent) {
|
|
||||||
t.Error("found torrent mismatch", foundTorrent, testTorrent)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFindTorrentFail(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("torrent found", foundTorrent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveTorrent(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("removed torrent found", foundTorrent)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestClientWhitelistSuccess(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testPeerID := "-lt0D30-"
|
|
||||||
|
|
||||||
panicOnErr(tx.WhitelistClient(testPeerID))
|
|
||||||
found, err := tx.ClientWhitelisted(testPeerID)
|
|
||||||
panicOnErr(err)
|
|
||||||
if !found {
|
|
||||||
t.Error("peerID not found", testPeerID)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.UnWhitelistClient(testPeerID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestClientWhitelistFail(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testPeerID2 := "TIX0192"
|
|
||||||
|
|
||||||
found, err := tx.ClientWhitelisted(testPeerID2)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("peerID found", testPeerID2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRecordSnatch(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
testUser := createTestUser()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
panicOnErr(tx.AddUser(testUser))
|
|
||||||
|
|
||||||
userSnatches := testUser.Snatches
|
|
||||||
torrentSnatches := testTorrent.Snatches
|
|
||||||
|
|
||||||
panicOnErr(tx.RecordSnatch(testUser, testTorrent))
|
|
||||||
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundUser, _, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
if testUser.Snatches != userSnatches+1 {
|
|
||||||
t.Error("snatch not recorded to local user", testUser.Snatches, userSnatches+1)
|
|
||||||
}
|
|
||||||
if testTorrent.Snatches != torrentSnatches+1 {
|
|
||||||
t.Error("snatch not recorded to local torrent")
|
|
||||||
}
|
|
||||||
if foundUser.Snatches != userSnatches+1 {
|
|
||||||
t.Error("snatch not recorded to cached user", foundUser.Snatches, userSnatches+1)
|
|
||||||
}
|
|
||||||
if foundTorrent.Snatches != torrentSnatches+1 {
|
|
||||||
t.Error("snatch not recorded to cached torrent")
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMarkActive(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
testTorrent.Active = false
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
|
|
||||||
panicOnErr(tx.MarkActive(testTorrent))
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
if foundTorrent.Active != true {
|
|
||||||
t.Error("cached torrent not activated")
|
|
||||||
}
|
|
||||||
if testTorrent.Active != true {
|
|
||||||
t.Error("cached torrent not activated")
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestClientWhitelistRemove(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testPeerID := "-lt0D30-"
|
|
||||||
panicOnErr(tx.WhitelistClient(testPeerID))
|
|
||||||
panicOnErr(tx.UnWhitelistClient(testPeerID))
|
|
||||||
|
|
||||||
found, err := tx.ClientWhitelisted(testPeerID)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("removed peerID found", testPeerID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddSeeder(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundSeeder, found := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if found && foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not added to cache", testSeeder)
|
|
||||||
}
|
|
||||||
foundSeeder, found = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if found && foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not added to local", testSeeder)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddLeecher(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
|
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found && foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not added to cache", testLeecher)
|
|
||||||
}
|
|
||||||
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found && foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not added to local", testLeecher)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveSeeder(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
|
||||||
|
|
||||||
panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
|
|
||||||
foundSeeder, found := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if found || foundSeeder == *testSeeder {
|
|
||||||
t.Error("seeder not removed from local", foundSeeder)
|
|
||||||
}
|
|
||||||
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundSeeder, found = foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if found || foundSeeder == *testSeeder {
|
|
||||||
t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveLeecher(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
|
||||||
|
|
||||||
panicOnErr(tx.RemoveLeecher(testTorrent, testLeecher))
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found || foundLeecher == *testLeecher {
|
|
||||||
t.Error("leecher not removed from cache", foundLeecher, *testLeecher)
|
|
||||||
}
|
|
||||||
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found || foundLeecher == *testLeecher {
|
|
||||||
t.Error("leecher not removed from local", foundLeecher, *testLeecher)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetSeeder(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
|
||||||
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
testSeeder.Uploaded += uint64(r.Int63())
|
|
||||||
|
|
||||||
panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
|
|
||||||
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
|
|
||||||
}
|
|
||||||
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetLeecher(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
|
||||||
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
testLeecher.Uploaded += uint64(r.Int63())
|
|
||||||
|
|
||||||
panicOnErr(tx.SetLeecher(testTorrent, testLeecher))
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundLeecher, _ := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not updated in cache", testLeecher)
|
|
||||||
}
|
|
||||||
foundLeecher, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not updated in local", testLeecher)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIncrementSlots(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testUser := createTestUser()
|
|
||||||
panicOnErr(tx.AddUser(testUser))
|
|
||||||
numSlots := testUser.Slots
|
|
||||||
|
|
||||||
panicOnErr(tx.IncrementSlots(testUser))
|
|
||||||
foundUser, _, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
if foundUser.Slots != numSlots+1 {
|
|
||||||
t.Error("cached slots not incremented")
|
|
||||||
}
|
|
||||||
if testUser.Slots != numSlots+1 {
|
|
||||||
t.Error("local slots not incremented")
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDecrementSlots(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testUser := createTestUser()
|
|
||||||
panicOnErr(tx.AddUser(testUser))
|
|
||||||
numSlots := testUser.Slots
|
|
||||||
|
|
||||||
panicOnErr(tx.DecrementSlots(testUser))
|
|
||||||
foundUser, _, err := tx.FindUser(testUser.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
|
|
||||||
if foundUser.Slots != numSlots-1 {
|
|
||||||
t.Error("cached slots not incremented")
|
|
||||||
}
|
|
||||||
if testUser.Slots != numSlots-1 {
|
|
||||||
t.Error("local slots not incremented")
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveUser(testUser))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLeecherFinished(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
|
||||||
testLeecher.Left = 0
|
|
||||||
|
|
||||||
panicOnErr(tx.LeecherFinished(testTorrent, testLeecher))
|
|
||||||
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundSeeder != *testLeecher {
|
|
||||||
t.Error("seeder not added to cache", foundSeeder, *testLeecher)
|
|
||||||
}
|
|
||||||
foundSeeder, _ = foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundSeeder == *testLeecher {
|
|
||||||
t.Error("leecher not removed from cache", testLeecher)
|
|
||||||
}
|
|
||||||
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundSeeder != *testLeecher {
|
|
||||||
t.Error("seeder not added to local", testLeecher)
|
|
||||||
}
|
|
||||||
foundSeeder, _ = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if foundSeeder == *testLeecher {
|
|
||||||
t.Error("leecher not removed from local", testLeecher)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add, update, verify remove
|
|
||||||
func TestUpdatePeer(t *testing.T) {
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
|
||||||
// Update a seeder, set it, then check to make sure it updated
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
testSeeder.Uploaded += uint64(r.Int63())
|
|
||||||
|
|
||||||
panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
|
|
||||||
|
|
||||||
panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if seeder, exists := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
|
|
||||||
t.Error("seeder not removed from cache", seeder)
|
|
||||||
}
|
|
||||||
if seeder, exists := testTorrent.Seeders[storage.PeerMapKey(testSeeder)]; exists {
|
|
||||||
t.Error("seeder not removed from local", seeder)
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParallelFindUser(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
tx := createTestTx()
|
|
||||||
testUserSuccess := createTestUser()
|
|
||||||
testUserFail := createTestUser()
|
|
||||||
panicOnErr(tx.AddUser(testUserSuccess))
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
foundUser, found, err := tx.FindUser(testUserFail.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("user found", foundUser)
|
|
||||||
}
|
|
||||||
foundUser, found, err = tx.FindUser(testUserSuccess.Passkey)
|
|
||||||
panicOnErr(err)
|
|
||||||
if !found {
|
|
||||||
t.Error("user not found", testUserSuccess)
|
|
||||||
}
|
|
||||||
if *foundUser != *testUserSuccess {
|
|
||||||
t.Error("found user mismatch", *foundUser, testUserSuccess)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveUser(testUserSuccess))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParallelFindTorrent(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrentSuccess := createTestTorrent()
|
|
||||||
testTorrentFail := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrentSuccess))
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrentSuccess.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if !found {
|
|
||||||
t.Error("torrent not found", testTorrentSuccess)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(foundTorrent, testTorrentSuccess) {
|
|
||||||
t.Error("found torrent mismatch", foundTorrent, testTorrentSuccess)
|
|
||||||
}
|
|
||||||
foundTorrent, found, err = tx.FindTorrent(testTorrentFail.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
if found {
|
|
||||||
t.Error("torrent found", foundTorrent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrentSuccess))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParallelSetSeeder(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
testSeeder.Uploaded += uint64(r.Int63())
|
|
||||||
|
|
||||||
panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
|
|
||||||
|
|
||||||
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundSeeder, _ := foundTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
|
|
||||||
}
|
|
||||||
foundSeeder, _ = testTorrent.Seeders[storage.PeerMapKey(testSeeder)]
|
|
||||||
if foundSeeder != *testSeeder {
|
|
||||||
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParallelAddLeecher(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
tx := createTestTx()
|
|
||||||
testTorrent := createTestTorrent()
|
|
||||||
panicOnErr(tx.AddTorrent(testTorrent))
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
|
|
||||||
|
|
||||||
panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
|
|
||||||
|
|
||||||
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
|
|
||||||
panicOnErr(err)
|
|
||||||
foundLeecher, found := foundTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found && foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not added to cache", testLeecher)
|
|
||||||
}
|
|
||||||
foundLeecher, found = testTorrent.Leechers[storage.PeerMapKey(testLeecher)]
|
|
||||||
if found && foundLeecher != *testLeecher {
|
|
||||||
t.Error("leecher not added to local", testLeecher)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Cleanup
|
|
||||||
panicOnErr(tx.RemoveTorrent(testTorrent))
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue