Fixes & format changes from pull request comments

This commit is contained in:
cpb8010 2013-09-24 23:10:48 -04:00
parent 19900991a7
commit 2dd810d2ee
5 changed files with 195 additions and 253 deletions

161
cache/redis/redis.go vendored
View file

@ -31,11 +31,11 @@ var (
ErrCreatePeer = errors.New("redis: Incorrect reply length for peer") ErrCreatePeer = errors.New("redis: Incorrect reply length for peer")
ErrMarkActive = errors.New("redis: Torrent doesn't exist") ErrMarkActive = errors.New("redis: Torrent doesn't exist")
SeederPrefix = "seeders:" SeedersPrefix = "seeders:"
LeecherPrefix = "leechers:" LeechersPrefix = "leechers:"
TorrentPrefix = "torrent:" TorrentPrefix = "torrent:"
UserPrefix = "user:" UserPrefix = "user:"
PeerPrefix = "peer:" PeerPrefix = "peer:"
) )
type driver struct{} type driver struct{}
@ -82,10 +82,7 @@ func (p *Pool) Get() (cache.Tx, error) {
done: false, done: false,
Conn: p.pool.Get(), Conn: p.pool.Get(),
} }
// Test valid connection before returning return retTx, nil
_, err := retTx.Do("PING")
return retTx, err
} }
type Tx struct { type Tx struct {
@ -106,76 +103,47 @@ func createUser(userVals []string) (*models.User, error) {
if len(userVals) != 7 { if len(userVals) != 7 {
return nil, ErrCreateUser return nil, ErrCreateUser
} }
ID, err := strconv.ParseUint(userVals[0], 10, 64) var user models.User
if err != nil { convErrors := make([]error, 7)
return nil, err user.ID, convErrors[0] = strconv.ParseUint(userVals[0], 10, 64)
user.Passkey = userVals[1]
user.UpMultiplier, convErrors[2] = strconv.ParseFloat(userVals[2], 64)
user.DownMultiplier, convErrors[3] = strconv.ParseFloat(userVals[3], 64)
user.Slots, convErrors[4] = strconv.ParseInt(userVals[4], 10, 64)
user.SlotsUsed, convErrors[5] = strconv.ParseInt(userVals[5], 10, 64)
user.Snatches, convErrors[6] = strconv.ParseUint(userVals[6], 10, 64)
for i := 0; i < 7; i++ {
if convErrors[i] != nil {
return nil, convErrors[i]
}
} }
Passkey := userVals[1] return &user, nil
UpMultiplier, err := strconv.ParseFloat(userVals[2], 64)
if err != nil {
return nil, err
}
DownMultiplier, err := strconv.ParseFloat(userVals[3], 64)
if err != nil {
return nil, err
}
Slots, err := strconv.ParseInt(userVals[4], 10, 64)
if err != nil {
return nil, err
}
SlotsUsed, err := strconv.ParseInt(userVals[5], 10, 64)
if err != nil {
return nil, err
}
Snatches, err := strconv.ParseUint(userVals[6], 10, 64)
if err != nil {
return nil, err
}
return &models.User{ID: ID, Passkey: Passkey, UpMultiplier: UpMultiplier,
DownMultiplier: DownMultiplier, Slots: Slots, SlotsUsed: SlotsUsed, Snatches: uint(Snatches)}, nil
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) createTorrent(torrentVals []string) (*models.Torrent, error) { func (tx *Tx) createTorrent(torrentVals []string) (*models.Torrent, error) {
if len(torrentVals) != 7 { if len(torrentVals) != 7 {
return nil, ErrCreateTorrent return nil, ErrCreateTorrent
} }
ID, err := strconv.ParseUint(torrentVals[0], 10, 64) var torrent models.Torrent
if err != nil { convErrors := make([]error, 9)
return nil, err torrent.ID, convErrors[0] = strconv.ParseUint(torrentVals[0], 10, 64)
} torrent.Infohash = torrentVals[1]
Infohash := torrentVals[1] torrent.Active, convErrors[2] = strconv.ParseBool(torrentVals[2])
Active, err := strconv.ParseBool(torrentVals[2]) torrent.Snatches, convErrors[3] = strconv.ParseUint(torrentVals[3], 10, 32)
if err != nil { torrent.UpMultiplier, convErrors[4] = strconv.ParseFloat(torrentVals[4], 64)
return nil, err torrent.DownMultiplier, convErrors[5] = strconv.ParseFloat(torrentVals[5], 64)
} torrent.LastAction, convErrors[6] = strconv.ParseInt(torrentVals[6], 10, 64)
Snatches, err := strconv.ParseUint(torrentVals[3], 10, 32) torrent.Seeders, convErrors[7] = tx.getPeers(torrent.ID, SeedersPrefix)
if err != nil { torrent.Leechers, convErrors[8] = tx.getPeers(torrent.ID, LeechersPrefix)
return nil, err
}
UpMultiplier, err := strconv.ParseFloat(torrentVals[4], 64)
if err != nil {
return nil, err
}
DownMultiplier, err := strconv.ParseFloat(torrentVals[5], 64)
if err != nil {
return nil, err
}
LastAction, err := strconv.ParseInt(torrentVals[6], 10, 64)
if err != nil {
return nil, err
}
seeders, err := tx.getPeers(ID, SeederPrefix)
if err != nil {
return nil, err
}
leechers, err := tx.getPeers(ID, LeecherPrefix)
if err != nil {
return nil, err
}
return &models.Torrent{ID: ID, Infohash: Infohash, Active: Active, Seeders: seeders, Leechers: leechers, for i := 0; i < 9; i++ {
Snatches: uint(Snatches), UpMultiplier: UpMultiplier, DownMultiplier: DownMultiplier, LastAction: LastAction}, nil if convErrors[i] != nil {
return nil, convErrors[i]
}
}
return &torrent, nil
} }
// The peer hashkey relies on the combination of peerID, userID, and torrentID being unique // The peer hashkey relies on the combination of peerID, userID, and torrentID being unique
@ -210,7 +178,7 @@ func (tx *Tx) removePeer(peer *models.Peer, peerTypePrefix string) error {
return nil return nil
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) removePeers(torrentID uint64, peers map[string]models.Peer, peerTypePrefix string) error { func (tx *Tx) removePeers(torrentID uint64, peers map[string]models.Peer, peerTypePrefix string) error {
for _, peer := range peers { for _, peer := range peers {
hashKey := tx.conf.Prefix + getPeerHashKey(&peer) hashKey := tx.conf.Prefix + getPeerHashKey(&peer)
@ -218,7 +186,7 @@ func (tx *Tx) removePeers(torrentID uint64, peers map[string]models.Peer, peerTy
if err != nil { if err != nil {
return err return err
} }
delete(peers, peer.ID) delete(peers, models.PeerMapKey(&peer))
} }
// Will only delete the set if all the peer deletions were successful // Will only delete the set if all the peer deletions were successful
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36) setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
@ -238,7 +206,7 @@ func getPeerSetKey(typePrefix string, peer *models.Peer) string {
return typePrefix + strconv.FormatUint(peer.TorrentID, 36) return typePrefix + strconv.FormatUint(peer.TorrentID, 36)
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) addPeers(peers map[string]models.Peer, peerTypePrefix string) error { func (tx *Tx) addPeers(peers map[string]models.Peer, peerTypePrefix string) error {
for _, peer := range peers { for _, peer := range peers {
setKey := tx.conf.Prefix + getPeerSetKey(peerTypePrefix, &peer) setKey := tx.conf.Prefix + getPeerSetKey(peerTypePrefix, &peer)
@ -291,7 +259,7 @@ func createPeer(peerVals []string) (*models.Peer, error) {
} }
// This is a mulple 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]models.Peer, err error) { func (tx *Tx) getPeers(torrentID uint64, peerTypePrefix string) (peers map[string]models.Peer, err error) {
peers = make(map[string]models.Peer) peers = make(map[string]models.Peer)
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36) setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
@ -318,7 +286,7 @@ func (tx *Tx) getPeers(torrentID uint64, peerTypePrefix string) (peers map[strin
return return
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) AddTorrent(t *models.Torrent) error { func (tx *Tx) AddTorrent(t *models.Torrent) error {
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
_, err := tx.Do("HMSET", hashkey, _, err := tx.Do("HMSET", hashkey,
@ -333,18 +301,18 @@ func (tx *Tx) AddTorrent(t *models.Torrent) error {
return err return err
} }
err = tx.addPeers(t.Seeders, SeederPrefix) err = tx.addPeers(t.Seeders, SeedersPrefix)
if err != nil { if err != nil {
return err return err
} }
err = tx.addPeers(t.Leechers, LeecherPrefix) err = tx.addPeers(t.Leechers, LeechersPrefix)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) RemoveTorrent(t *models.Torrent) error { func (tx *Tx) RemoveTorrent(t *models.Torrent) error {
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
_, err := tx.Do("DEL", hashkey) _, err := tx.Do("DEL", hashkey)
@ -352,11 +320,11 @@ func (tx *Tx) RemoveTorrent(t *models.Torrent) error {
return err return err
} }
// Remove seeders and leechers as well // Remove seeders and leechers as well
err = tx.removePeers(t.ID, t.Seeders, SeederPrefix) err = tx.removePeers(t.ID, t.Seeders, SeedersPrefix)
if err != nil { if err != nil {
return err return err
} }
err = tx.removePeers(t.ID, t.Leechers, LeecherPrefix) err = tx.removePeers(t.ID, t.Leechers, LeechersPrefix)
if err != nil { if err != nil {
return err return err
} }
@ -403,7 +371,7 @@ func (tx *Tx) FindUser(passkey string) (*models.User, bool, error) {
return foundUser, true, nil return foundUser, true, nil
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) FindTorrent(infohash string) (*models.Torrent, bool, error) { func (tx *Tx) FindTorrent(infohash string) (*models.Torrent, bool, error) {
hashkey := tx.conf.Prefix + TorrentPrefix + infohash hashkey := tx.conf.Prefix + TorrentPrefix + infohash
torrentStrings, err := redis.Strings(tx.Do("HVALS", hashkey)) torrentStrings, err := redis.Strings(tx.Do("HVALS", hashkey))
@ -437,7 +405,7 @@ func (tx *Tx) UnWhitelistClient(peerID string) error {
return err return err
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) RecordSnatch(user *models.User, torrent *models.Torrent) error { func (tx *Tx) RecordSnatch(user *models.User, torrent *models.Torrent) error {
torrentKey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash torrentKey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
@ -445,14 +413,14 @@ func (tx *Tx) RecordSnatch(user *models.User, torrent *models.Torrent) error {
if err != nil { if err != nil {
return err return err
} }
torrent.Snatches = uint(snatchCount) torrent.Snatches = uint64(snatchCount)
userKey := tx.conf.Prefix + UserPrefix + user.Passkey userKey := tx.conf.Prefix + UserPrefix + user.Passkey
snatchCount, err = redis.Int(tx.Do("HINCRBY", userKey, "snatches", 1)) snatchCount, err = redis.Int(tx.Do("HINCRBY", userKey, "snatches", 1))
if err != nil { if err != nil {
return err return err
} }
user.Snatches = uint(snatchCount) user.Snatches = uint64(snatchCount)
return nil return nil
} }
@ -470,7 +438,7 @@ func (tx *Tx) MarkActive(torrent *models.Torrent) error {
return nil return nil
} }
func (tx *Tx) MarkInActive(torrent *models.Torrent) error { func (tx *Tx) MarkInactive(torrent *models.Torrent) error {
hashkey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash hashkey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
activeExists, err := redis.Int(tx.Do("HSET", hashkey, "active", false)) activeExists, err := redis.Int(tx.Do("HSET", hashkey, "active", false))
if err != nil { if err != nil {
@ -479,14 +447,19 @@ func (tx *Tx) MarkInActive(torrent *models.Torrent) error {
torrent.Active = false torrent.Active = false
// 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
_, err = tx.Do("DEL", hashkey)
if err != nil {
return err
}
return ErrMarkActive return ErrMarkActive
} }
return nil return nil
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) AddLeecher(torrent *models.Torrent, peer *models.Peer) error { func (tx *Tx) AddLeecher(torrent *models.Torrent, peer *models.Peer) error {
setKey := tx.conf.Prefix + LeecherPrefix + strconv.FormatUint(torrent.ID, 36) setKey := tx.conf.Prefix + LeechersPrefix + strconv.FormatUint(torrent.ID, 36)
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer)) _, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
if err != nil { if err != nil {
return err return err
@ -514,7 +487,7 @@ func (tx *Tx) SetLeecher(t *models.Torrent, p *models.Peer) error {
} }
func (tx *Tx) RemoveLeecher(t *models.Torrent, p *models.Peer) error { func (tx *Tx) RemoveLeecher(t *models.Torrent, p *models.Peer) error {
err := tx.removePeer(p, LeecherPrefix) err := tx.removePeer(p, LeechersPrefix)
if err != nil { if err != nil {
return err return err
} }
@ -524,8 +497,8 @@ func (tx *Tx) RemoveLeecher(t *models.Torrent, p *models.Peer) error {
func (tx *Tx) LeecherFinished(torrent *models.Torrent, peer *models.Peer) error { func (tx *Tx) LeecherFinished(torrent *models.Torrent, peer *models.Peer) error {
torrentIdKey := strconv.FormatUint(torrent.ID, 36) torrentIdKey := strconv.FormatUint(torrent.ID, 36)
seederSetKey := tx.conf.Prefix + SeederPrefix + torrentIdKey seederSetKey := tx.conf.Prefix + SeedersPrefix + torrentIdKey
leecherSetKey := tx.conf.Prefix + LeecherPrefix + torrentIdKey leecherSetKey := tx.conf.Prefix + LeechersPrefix + torrentIdKey
_, err := tx.Do("SMOVE", leecherSetKey, seederSetKey, getPeerHashKey(peer)) _, err := tx.Do("SMOVE", leecherSetKey, seederSetKey, getPeerHashKey(peer))
if err != nil { if err != nil {
@ -538,9 +511,9 @@ func (tx *Tx) LeecherFinished(torrent *models.Torrent, peer *models.Peer) error
return err return err
} }
// This is a mulple action command, it's not internally atomic // This is a multiple action command, it's not internally atomic
func (tx *Tx) AddSeeder(torrent *models.Torrent, peer *models.Peer) error { func (tx *Tx) AddSeeder(torrent *models.Torrent, peer *models.Peer) error {
setKey := tx.conf.Prefix + SeederPrefix + strconv.FormatUint(torrent.ID, 36) setKey := tx.conf.Prefix + SeedersPrefix + strconv.FormatUint(torrent.ID, 36)
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer)) _, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
if err != nil { if err != nil {
return err return err
@ -566,7 +539,7 @@ func (tx *Tx) SetSeeder(t *models.Torrent, p *models.Peer) error {
} }
func (tx *Tx) RemoveSeeder(t *models.Torrent, p *models.Peer) error { func (tx *Tx) RemoveSeeder(t *models.Torrent, p *models.Peer) error {
err := tx.removePeer(p, SeederPrefix) err := tx.removePeer(p, SeedersPrefix)
if err != nil { if err != nil {
return err return err
} }

View file

@ -15,13 +15,13 @@ func BenchmarkSuccessfulFindUser(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.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 := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
b.Error("user not found", testUser) b.Error("user not found", testUser)
} }
@ -40,7 +40,7 @@ func BenchmarkFailedFindUser(b *testing.B) {
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
_, found, err := tx.FindUser(testUser.Passkey) _, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if found { if found {
b.Error("user not found", testUser) b.Error("user not found", testUser)
} }
@ -52,12 +52,12 @@ func BenchmarkSuccessfulFindTorrent(b *testing.B) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.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 := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
b.Error("torrent not found", testTorrent) b.Error("torrent not found", testTorrent)
} }
@ -76,7 +76,7 @@ func BenchmarkFailFindTorrent(b *testing.B) {
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if found { if found {
b.Error("torrent found", foundTorrent) b.Error("torrent found", foundTorrent)
} }
@ -87,12 +87,12 @@ func BenchmarkSuccessfulClientWhitelisted(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testPeerID := "-lt0D30-" testPeerID := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID)) panicOnErr(tx.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 := tx.ClientWhitelisted(testPeerID)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
b.Error("peerID not found", testPeerID) b.Error("peerID not found", testPeerID)
} }
@ -107,7 +107,7 @@ func BenchmarkFailClientWhitelisted(b *testing.B) {
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
found, err := tx.ClientWhitelisted(testPeerID2) found, err := tx.ClientWhitelisted(testPeerID2)
panicErrNil(err) panicOnErr(err)
if found { if found {
b.Error("peerID found", testPeerID2) b.Error("peerID found", testPeerID2)
} }
@ -119,12 +119,12 @@ func BenchmarkRecordSnatch(b *testing.B) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
b.StartTimer() b.StartTimer()
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
panicErrNil(tx.RecordSnatch(testUser, testTorrent)) panicOnErr(tx.RecordSnatch(testUser, testTorrent))
} }
} }
@ -133,11 +133,11 @@ func BenchmarkMarkActive(b *testing.B) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
testTorrent.Active = false testTorrent.Active = false
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
b.StartTimer() b.StartTimer()
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
panicErrNil(tx.MarkActive(testTorrent)) panicOnErr(tx.MarkActive(testTorrent))
} }
} }
@ -145,7 +145,7 @@ func BenchmarkAddSeeder(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
b.StartTimer() b.StartTimer()
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
@ -153,7 +153,7 @@ func BenchmarkAddSeeder(b *testing.B) {
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
b.StartTimer() b.StartTimer()
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
} }
} }
@ -161,7 +161,7 @@ func BenchmarkRemoveSeeder(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
b.StartTimer() b.StartTimer()
@ -170,7 +170,7 @@ func BenchmarkRemoveSeeder(b *testing.B) {
tx.AddSeeder(testTorrent, testSeeder) tx.AddSeeder(testTorrent, testSeeder)
b.StartTimer() b.StartTimer()
panicErrNil(tx.RemoveSeeder(testTorrent, testSeeder)) panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
} }
} }
@ -178,9 +178,9 @@ func BenchmarkSetSeeder(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
b.StartTimer() b.StartTimer()
@ -197,11 +197,11 @@ func BenchmarkIncrementSlots(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
b.StartTimer() b.StartTimer()
for bCount := 0; bCount < b.N; bCount++ { for bCount := 0; bCount < b.N; bCount++ {
panicErrNil(tx.IncrementSlots(testUser)) panicOnErr(tx.IncrementSlots(testUser))
} }
} }
@ -209,17 +209,17 @@ func BenchmarkLeecherFinished(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.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)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
testLeecher.Left = 0 testLeecher.Left = 0
b.StartTimer() b.StartTimer()
panicErrNil(tx.LeecherFinished(testTorrent, testLeecher)) panicOnErr(tx.LeecherFinished(testTorrent, testLeecher))
} }
} }
@ -228,18 +228,18 @@ func BenchmarkRemoveLeecherAddSeeder(b *testing.B) {
b.StopTimer() b.StopTimer()
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.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)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
testLeecher.Left = 0 testLeecher.Left = 0
b.StartTimer() b.StartTimer()
panicErrNil(tx.RemoveLeecher(testTorrent, testLeecher)) panicOnErr(tx.RemoveLeecher(testTorrent, testLeecher))
panicErrNil(tx.AddSeeder(testTorrent, testLeecher)) panicOnErr(tx.AddSeeder(testTorrent, testLeecher))
} }
} }

View file

@ -76,7 +76,7 @@ func createTestPasskey() string {
return string(uuid) return string(uuid)
} }
func panicErrNil(err error) { func panicOnErr(err error) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
panic(err) panic(err)
@ -86,7 +86,7 @@ func panicErrNil(err error) {
func createTestRedisTx() *Tx { func createTestRedisTx() *Tx {
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
conf := &testConfig.Cache conf := &testConfig.Cache
panicErrNil(err) panicOnErr(err)
testPool := &Pool{ testPool := &Pool{
conf: conf, conf: conf,
@ -103,11 +103,11 @@ func createTestRedisTx() *Tx {
done: false, done: false,
Conn: testPool.pool.Get(), Conn: testPool.pool.Get(),
} }
panicErrNil(err) panicOnErr(err)
// Test connection before returning // Test connection before returning
_, err = txObj.Do("PING") _, err = txObj.Do("PING")
panicErrNil(err) panicOnErr(err)
return txObj return txObj
} }
@ -144,53 +144,18 @@ func createTestTorrent() *models.Torrent {
return &testTorrent return &testTorrent
} }
func comparePeers(lhPeers map[string]models.Peer, rhPeers map[string]models.Peer) bool {
if len(lhPeers) != len(rhPeers) {
return false
}
for rhKey, rhValue := range rhPeers {
lhValue, lhExists := lhPeers[rhKey]
if !lhExists || lhValue != rhValue {
return false
}
}
for lhKey, lhValue := range lhPeers {
rhValue, rhExists := rhPeers[lhKey]
if !rhExists || rhValue != lhValue {
return false
}
}
return true
}
func torrentsEqual(lhTorrent *models.Torrent, rhTorrent *models.Torrent) bool {
fieldsEqual := lhTorrent.Infohash == rhTorrent.Infohash &&
lhTorrent.ID == rhTorrent.ID &&
lhTorrent.Active == rhTorrent.Active &&
lhTorrent.Snatches == rhTorrent.Snatches &&
lhTorrent.UpMultiplier == rhTorrent.UpMultiplier &&
lhTorrent.DownMultiplier == rhTorrent.DownMultiplier &&
lhTorrent.LastAction == rhTorrent.LastAction
if !fieldsEqual {
return false
}
return comparePeers(lhTorrent.Seeders, rhTorrent.Seeders) && comparePeers(lhTorrent.Leechers, rhTorrent.Leechers)
}
func TestValidPeers(t *testing.T) { func TestValidPeers(t *testing.T) {
testTx := createTestRedisTx() testTx := createTestRedisTx()
testTorrentID := createTestTorrentID() testTorrentID := createTestTorrentID()
testPeers := createTestPeers(testTorrentID, 3) testPeers := createTestPeers(testTorrentID, 3)
panicErrNil(testTx.addPeers(testPeers, "test:")) panicOnErr(testTx.addPeers(testPeers, "test:"))
peerMap, err := testTx.getPeers(testTorrentID, "test:") peerMap, err := testTx.getPeers(testTorrentID, "test:")
panicErrNil(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))
} }
panicErrNil(testTx.removePeers(testTorrentID, testPeers, "test:")) panicOnErr(testTx.removePeers(testTorrentID, testPeers, "test:"))
} }
func TestInvalidPeers(t *testing.T) { func TestInvalidPeers(t *testing.T) {
@ -200,17 +165,20 @@ func TestInvalidPeers(t *testing.T) {
tempPeer := createTestPeer(createTestUserID(), testTorrentID) tempPeer := createTestPeer(createTestUserID(), testTorrentID)
testPeers[models.PeerMapKey(tempPeer)] = *tempPeer testPeers[models.PeerMapKey(tempPeer)] = *tempPeer
panicErrNil(testTx.addPeers(testPeers, "test:")) panicOnErr(testTx.addPeers(testPeers, "test:"))
// Imitate a peer being removed during get // Imitate a peer being removed during get
hashKey := testTx.conf.Prefix + getPeerHashKey(tempPeer) hashKey := testTx.conf.Prefix + getPeerHashKey(tempPeer)
_, err := testTx.Do("DEL", hashKey) _, err := testTx.Do("DEL", hashKey)
panicErrNil(err) panicOnErr(err)
peerMap, err := testTx.getPeers(testTorrentID, "test:") peerMap, err := testTx.getPeers(testTorrentID, "test:")
panicErrNil(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)) t.Error("Num Peers not equal ", len(peerMap), len(testPeers)-1)
}
panicOnErr(testTx.removePeers(testTorrentID, testPeers, "test:"))
if len(testPeers) != 0 {
t.Errorf("All peers not removed, %d peers remain!", len(testPeers))
} }
panicErrNil(testTx.removePeers(testTorrentID, testPeers, "test:"))
} }

163
cache/redis/tx_test.go vendored
View file

@ -7,6 +7,7 @@ package redis
import ( import (
"math/rand" "math/rand"
"os" "os"
"reflect"
"testing" "testing"
"time" "time"
@ -17,14 +18,14 @@ import (
func createTestTx() cache.Tx { func createTestTx() cache.Tx {
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
panicErrNil(err) panicOnErr(err)
conf := &testConfig.Cache conf := &testConfig.Cache
testPool, err := cache.Open(conf) testPool, err := cache.Open(conf)
panicErrNil(err) panicOnErr(err)
txObj, err := testPool.Get() txObj, err := testPool.Get()
panicErrNil(err) panicOnErr(err)
return txObj return txObj
} }
@ -33,9 +34,9 @@ func TestFindUserSuccess(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
foundUser, found, err := tx.FindUser(testUser.Passkey) foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
t.Error("user not found", testUser) t.Error("user not found", testUser)
} }
@ -49,7 +50,7 @@ func TestFindUserFail(t *testing.T) {
testUser := createTestUser() testUser := createTestUser()
foundUser, found, err := tx.FindUser(testUser.Passkey) foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("user found", foundUser) t.Error("user found", foundUser)
} }
@ -59,11 +60,11 @@ func TestRemoveUser(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
err := tx.RemoveUser(testUser) err := tx.RemoveUser(testUser)
panicErrNil(err) panicOnErr(err)
foundUser, found, err := tx.FindUser(testUser.Passkey) foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("removed user found", foundUser) t.Error("removed user found", foundUser)
} }
@ -72,14 +73,14 @@ func TestRemoveUser(t *testing.T) {
func TestFindTorrentSuccess(t *testing.T) { func TestFindTorrentSuccess(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
t.Error("torrent not found", testTorrent) t.Error("torrent not found", testTorrent)
} }
if !torrentsEqual(foundTorrent, testTorrent) { if !reflect.DeepEqual(foundTorrent, testTorrent) {
t.Error("found torrent mismatch", foundTorrent, testTorrent) t.Error("found torrent mismatch", foundTorrent, testTorrent)
} }
} }
@ -89,7 +90,7 @@ func TestFindTorrentFail(t *testing.T) {
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("torrent found", foundTorrent) t.Error("torrent found", foundTorrent)
} }
@ -98,11 +99,11 @@ func TestFindTorrentFail(t *testing.T) {
func TestRemoveTorrent(t *testing.T) { func TestRemoveTorrent(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
panicErrNil(tx.RemoveTorrent(testTorrent)) panicOnErr(tx.RemoveTorrent(testTorrent))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("removed torrent found", foundTorrent) t.Error("removed torrent found", foundTorrent)
} }
@ -112,9 +113,9 @@ func TestClientWhitelistSuccess(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testPeerID := "-lt0D30-" testPeerID := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID)) panicOnErr(tx.WhitelistClient(testPeerID))
found, err := tx.ClientWhitelisted(testPeerID) found, err := tx.ClientWhitelisted(testPeerID)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
t.Error("peerID not found", testPeerID) t.Error("peerID not found", testPeerID)
} }
@ -125,7 +126,7 @@ func TestClientWhitelistFail(t *testing.T) {
testPeerID2 := "TIX0192" testPeerID2 := "TIX0192"
found, err := tx.ClientWhitelisted(testPeerID2) found, err := tx.ClientWhitelisted(testPeerID2)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("peerID found", testPeerID2) t.Error("peerID found", testPeerID2)
} }
@ -135,18 +136,18 @@ func TestRecordSnatch(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
userSnatches := testUser.Snatches userSnatches := testUser.Snatches
torrentSnatches := testTorrent.Snatches torrentSnatches := testTorrent.Snatches
panicErrNil(tx.RecordSnatch(testUser, testTorrent)) panicOnErr(tx.RecordSnatch(testUser, testTorrent))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundUser, _, err := tx.FindUser(testUser.Passkey) foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if testUser.Snatches != userSnatches+1 { if testUser.Snatches != userSnatches+1 {
t.Error("snatch not recorded to local user", testUser.Snatches, userSnatches+1) t.Error("snatch not recorded to local user", testUser.Snatches, userSnatches+1)
@ -166,11 +167,11 @@ func TestMarkActive(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
testTorrent.Active = false testTorrent.Active = false
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
panicErrNil(tx.MarkActive(testTorrent)) panicOnErr(tx.MarkActive(testTorrent))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if foundTorrent.Active != true { if foundTorrent.Active != true {
t.Error("cached torrent not activated") t.Error("cached torrent not activated")
@ -183,11 +184,11 @@ func TestMarkActive(t *testing.T) {
func TestClientWhitelistRemove(t *testing.T) { func TestClientWhitelistRemove(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testPeerID := "-lt0D30-" testPeerID := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID)) panicOnErr(tx.WhitelistClient(testPeerID))
panicErrNil(tx.UnWhitelistClient(testPeerID)) panicOnErr(tx.UnWhitelistClient(testPeerID))
found, err := tx.ClientWhitelisted(testPeerID) found, err := tx.ClientWhitelisted(testPeerID)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("removed peerID found", testPeerID) t.Error("removed peerID found", testPeerID)
} }
@ -196,12 +197,12 @@ func TestClientWhitelistRemove(t *testing.T) {
func TestAddSeeder(t *testing.T) { func TestAddSeeder(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundSeeder, found := foundTorrent.Seeders[models.PeerMapKey(testSeeder)] foundSeeder, found := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found && foundSeeder != *testSeeder { if found && foundSeeder != *testSeeder {
t.Error("seeder not added to cache", testSeeder) t.Error("seeder not added to cache", testSeeder)
@ -215,12 +216,12 @@ func TestAddSeeder(t *testing.T) {
func TestAddLeecher(t *testing.T) { func TestAddLeecher(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID) testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)] foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher { if found && foundLeecher != *testLeecher {
t.Error("leecher not added to cache", testLeecher) t.Error("leecher not added to cache", testLeecher)
@ -234,18 +235,18 @@ func TestAddLeecher(t *testing.T) {
func TestRemoveSeeder(t *testing.T) { func TestRemoveSeeder(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
panicErrNil(tx.RemoveSeeder(testTorrent, testSeeder)) panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
foundSeeder, found := testTorrent.Seeders[models.PeerMapKey(testSeeder)] foundSeeder, found := testTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder { if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from local", foundSeeder) t.Error("seeder not removed from local", foundSeeder)
} }
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundSeeder, found = foundTorrent.Seeders[models.PeerMapKey(testSeeder)] foundSeeder, found = foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder { if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from cache", foundSeeder, *testSeeder) t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
@ -255,13 +256,13 @@ func TestRemoveSeeder(t *testing.T) {
func TestRemoveLeecher(t *testing.T) { func TestRemoveLeecher(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID) testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
panicErrNil(tx.RemoveLeecher(testTorrent, testLeecher)) panicOnErr(tx.RemoveLeecher(testTorrent, testLeecher))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)] foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found || foundLeecher == *testLeecher { if found || foundLeecher == *testLeecher {
t.Error("leecher not removed from cache", foundLeecher, *testLeecher) t.Error("leecher not removed from cache", foundLeecher, *testLeecher)
@ -275,17 +276,17 @@ func TestRemoveLeecher(t *testing.T) {
func TestSetSeeder(t *testing.T) { func TestSetSeeder(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder.Uploaded += uint64(r.Int63()) testSeeder.Uploaded += uint64(r.Int63())
panicErrNil(tx.SetSeeder(testTorrent, testSeeder)) panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testSeeder)] foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder { if foundSeeder != *testSeeder {
t.Error("seeder not updated in cache", foundSeeder, *testSeeder) t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
@ -299,16 +300,16 @@ func TestSetSeeder(t *testing.T) {
func TestSetLeecher(t *testing.T) { func TestSetLeecher(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID) testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
testLeecher.Uploaded += uint64(r.Int63()) testLeecher.Uploaded += uint64(r.Int63())
panicErrNil(tx.SetLeecher(testTorrent, testLeecher)) panicOnErr(tx.SetLeecher(testTorrent, testLeecher))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundLeecher, _ := foundTorrent.Leechers[models.PeerMapKey(testLeecher)] foundLeecher, _ := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if foundLeecher != *testLeecher { if foundLeecher != *testLeecher {
t.Error("leecher not updated in cache", testLeecher) t.Error("leecher not updated in cache", testLeecher)
@ -322,12 +323,12 @@ func TestSetLeecher(t *testing.T) {
func TestIncrementSlots(t *testing.T) { func TestIncrementSlots(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
numSlots := testUser.Slots numSlots := testUser.Slots
panicErrNil(tx.IncrementSlots(testUser)) panicOnErr(tx.IncrementSlots(testUser))
foundUser, _, err := tx.FindUser(testUser.Passkey) foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if foundUser.Slots != numSlots+1 { if foundUser.Slots != numSlots+1 {
t.Error("cached slots not incremented") t.Error("cached slots not incremented")
@ -340,12 +341,12 @@ func TestIncrementSlots(t *testing.T) {
func TestDecrementSlots(t *testing.T) { func TestDecrementSlots(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testUser := createTestUser() testUser := createTestUser()
panicErrNil(tx.AddUser(testUser)) panicOnErr(tx.AddUser(testUser))
numSlots := testUser.Slots numSlots := testUser.Slots
panicErrNil(tx.DecrementSlots(testUser)) panicOnErr(tx.DecrementSlots(testUser))
foundUser, _, err := tx.FindUser(testUser.Passkey) foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(err) panicOnErr(err)
if foundUser.Slots != numSlots-1 { if foundUser.Slots != numSlots-1 {
t.Error("cached slots not incremented") t.Error("cached slots not incremented")
@ -358,15 +359,15 @@ func TestDecrementSlots(t *testing.T) {
func TestLeecherFinished(t *testing.T) { func TestLeecherFinished(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID) testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
testLeecher.Left = 0 testLeecher.Left = 0
panicErrNil(tx.LeecherFinished(testTorrent, testLeecher)) panicOnErr(tx.LeecherFinished(testTorrent, testLeecher))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testLeecher)] foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testLeecher)]
if foundSeeder != *testLeecher { if foundSeeder != *testLeecher {
t.Error("seeder not added to cache", foundSeeder, *testLeecher) t.Error("seeder not added to cache", foundSeeder, *testLeecher)
@ -390,17 +391,17 @@ func TestUpdatePeer(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
// Update a seeder, set it, then check to make sure it updated // Update a seeder, set it, then check to make sure it updated
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder.Uploaded += uint64(r.Int63()) testSeeder.Uploaded += uint64(r.Int63())
panicErrNil(tx.SetSeeder(testTorrent, testSeeder)) panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
panicErrNil(tx.RemoveSeeder(testTorrent, testSeeder)) panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
if seeder, exists := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]; exists { if seeder, exists := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]; exists {
t.Error("seeder not removed from cache", seeder) t.Error("seeder not removed from cache", seeder)
} }
@ -417,16 +418,16 @@ func TestParallelFindUser(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testUserSuccess := createTestUser() testUserSuccess := createTestUser()
testUserFail := createTestUser() testUserFail := createTestUser()
panicErrNil(tx.AddUser(testUserSuccess)) panicOnErr(tx.AddUser(testUserSuccess))
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
foundUser, found, err := tx.FindUser(testUserFail.Passkey) foundUser, found, err := tx.FindUser(testUserFail.Passkey)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("user found", foundUser) t.Error("user found", foundUser)
} }
foundUser, found, err = tx.FindUser(testUserSuccess.Passkey) foundUser, found, err = tx.FindUser(testUserSuccess.Passkey)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
t.Error("user not found", testUserSuccess) t.Error("user not found", testUserSuccess)
} }
@ -444,19 +445,19 @@ func TestParallelFindTorrent(t *testing.T) {
tx := createTestTx() tx := createTestTx()
testTorrentSuccess := createTestTorrent() testTorrentSuccess := createTestTorrent()
testTorrentFail := createTestTorrent() testTorrentFail := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrentSuccess)) panicOnErr(tx.AddTorrent(testTorrentSuccess))
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
foundTorrent, found, err := tx.FindTorrent(testTorrentSuccess.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrentSuccess.Infohash)
panicErrNil(err) panicOnErr(err)
if !found { if !found {
t.Error("torrent not found", testTorrentSuccess) t.Error("torrent not found", testTorrentSuccess)
} }
if !torrentsEqual(foundTorrent, testTorrentSuccess) { if !reflect.DeepEqual(foundTorrent, testTorrentSuccess) {
t.Error("found torrent mismatch", foundTorrent, testTorrentSuccess) t.Error("found torrent mismatch", foundTorrent, testTorrentSuccess)
} }
foundTorrent, found, err = tx.FindTorrent(testTorrentFail.Infohash) foundTorrent, found, err = tx.FindTorrent(testTorrentFail.Infohash)
panicErrNil(err) panicOnErr(err)
if found { if found {
t.Error("torrent found", foundTorrent) t.Error("torrent found", foundTorrent)
} }
@ -470,18 +471,18 @@ func TestParallelSetSeeder(t *testing.T) {
} }
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID) testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder)) panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
testSeeder.Uploaded += uint64(r.Int63()) testSeeder.Uploaded += uint64(r.Int63())
panicErrNil(tx.SetSeeder(testTorrent, testSeeder)) panicOnErr(tx.SetSeeder(testTorrent, testSeeder))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testSeeder)] foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder { if foundSeeder != *testSeeder {
t.Error("seeder not updated in cache", foundSeeder, *testSeeder) t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
@ -500,15 +501,15 @@ func TestParallelAddLeecher(t *testing.T) {
} }
tx := createTestTx() tx := createTestTx()
testTorrent := createTestTorrent() testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent)) panicOnErr(tx.AddTorrent(testTorrent))
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID) testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher)) panicOnErr(tx.AddLeecher(testTorrent, testLeecher))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash) foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err) panicOnErr(err)
foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)] foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher { if found && foundLeecher != *testLeecher {
t.Error("leecher not added to cache", testLeecher) t.Error("leecher not added to cache", testLeecher)

View file

@ -34,7 +34,7 @@ type Torrent struct {
Seeders map[string]Peer `json:"seeders"` Seeders map[string]Peer `json:"seeders"`
Leechers map[string]Peer `json:"leechers"` Leechers map[string]Peer `json:"leechers"`
Snatches uint `json:"snatches"` Snatches uint64 `json:"snatches"`
UpMultiplier float64 `json:"up_multiplier"` UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"` DownMultiplier float64 `json:"down_multiplier"`
LastAction int64 `json:"last_action"` LastAction int64 `json:"last_action"`
@ -48,5 +48,5 @@ type User struct {
DownMultiplier float64 `json:"down_multiplier"` DownMultiplier float64 `json:"down_multiplier"`
Slots int64 `json:"slots"` Slots int64 `json:"slots"`
SlotsUsed int64 `json:"slots_used"` SlotsUsed int64 `json:"slots_used"`
Snatches uint `json:"snatches"` Snatches uint64 `json:"snatches"`
} }