Completed cache.Tx tests and added benchmarks

This commit is contained in:
cpb8010
2013-09-16 00:06:48 -04:00
parent 3caa06b5f6
commit 1ea24f80dc
6 changed files with 527 additions and 323 deletions
+224 -143
View File
@@ -5,7 +5,6 @@
package redis
import (
"fmt"
"math/rand"
"os"
"testing"
@@ -13,15 +12,9 @@ import (
"github.com/pushrax/chihaya/cache"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/models"
)
func panicErrNil(err error) {
if err != nil {
fmt.Println(err)
panic(err)
}
}
func createTestTx() cache.Tx {
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
panicErrNil(err)
@@ -38,24 +31,24 @@ func createTestTx() cache.Tx {
func TestFindUserSuccess(t *testing.T) {
tx := createTestTx()
testUser1 := createTestUser()
testUser := createTestUser()
panicErrNil(tx.AddUser(&testUser1))
foundUser, found, err := tx.FindUser(testUser1.Passkey)
panicErrNil(tx.AddUser(testUser))
foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err)
if !found {
t.Error("user not found", testUser1)
t.Error("user not found", testUser)
}
if *foundUser != testUser1 {
t.Error("found user mismatch", *foundUser, testUser1)
if *foundUser != *testUser {
t.Error("found user mismatch", *foundUser, testUser)
}
}
func TestFindUserFail(t *testing.T) {
tx := createTestTx()
testUser2 := createTestUser()
testUser := createTestUser()
foundUser, found, err := tx.FindUser(testUser2.Passkey)
foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err)
if found {
t.Error("user found", foundUser)
@@ -64,12 +57,12 @@ func TestFindUserFail(t *testing.T) {
func TestRemoveUser(t *testing.T) {
tx := createTestTx()
testUser1 := createTestUser()
testUser := createTestUser()
panicErrNil(tx.AddUser(&testUser1))
err := tx.RemoveUser(&testUser1)
panicErrNil(tx.AddUser(testUser))
err := tx.RemoveUser(testUser)
panicErrNil(err)
foundUser, found, err := tx.FindUser(testUser1.Passkey)
foundUser, found, err := tx.FindUser(testUser.Passkey)
panicErrNil(err)
if found {
t.Error("removed user found", foundUser)
@@ -78,25 +71,25 @@ func TestRemoveUser(t *testing.T) {
func TestFindTorrent(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.AddTorrent(testTorrent))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
if !found {
t.Error("torrent not found", testTorrent1)
t.Error("torrent not found", testTorrent)
}
// Incomplete comparison as maps make struct not nativly comparable
if foundTorrent.Infohash != testTorrent1.Infohash {
t.Error("found torrent mismatch", foundTorrent, testTorrent1)
if foundTorrent.Infohash != testTorrent.Infohash {
t.Error("found torrent mismatch", foundTorrent, testTorrent)
}
}
func TestFindTorrentFail(t *testing.T) {
tx := createTestTx()
testTorrent2 := createTestTorrent()
testTorrent := createTestTorrent()
foundTorrent, found, err := tx.FindTorrent(testTorrent2.Infohash)
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
if found {
t.Error("torrent found", foundTorrent)
@@ -105,11 +98,11 @@ func TestFindTorrentFail(t *testing.T) {
func TestRemoveTorrent(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
panicErrNil(tx.RemoveTorrent(testTorrent1))
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.RemoveTorrent(testTorrent))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
if found {
t.Error("removed torrent found", foundTorrent)
@@ -118,13 +111,13 @@ func TestRemoveTorrent(t *testing.T) {
func TestClientWhitelistSuccess(t *testing.T) {
tx := createTestTx()
testPeerID1 := "-lt0D30-"
testPeerID := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID1))
found, err := tx.ClientWhitelisted(testPeerID1)
panicErrNil(tx.WhitelistClient(testPeerID))
found, err := tx.ClientWhitelisted(testPeerID)
panicErrNil(err)
if !found {
t.Error("peerID not found", testPeerID1)
t.Error("peerID not found", testPeerID)
}
}
@@ -137,194 +130,282 @@ func TestClientWhitelistFail(t *testing.T) {
if found {
t.Error("peerID found", testPeerID2)
}
}
func TestRecordSnatch(t *testing.T) {
tx := createTestTx()
testTorrent := createTestTorrent()
testUser := createTestUser()
panicErrNil(tx.AddTorrent(testTorrent))
panicErrNil(tx.AddUser(testUser))
userSnatches := testUser.Snatches
torrentSnatches := testTorrent.Snatches
panicErrNil(tx.RecordSnatch(testUser, testTorrent))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(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")
}
}
func TestMarkActive(t *testing.T) {
tx := createTestTx()
testTorrent := createTestTorrent()
testTorrent.Active = false
panicErrNil(tx.AddTorrent(testTorrent))
panicErrNil(tx.MarkActive(testTorrent))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
if foundTorrent.Active != true {
t.Error("cached torrent not activated")
}
if testTorrent.Active != true {
t.Error("cached torrent not activated")
}
}
func TestClientWhitelistRemove(t *testing.T) {
tx := createTestTx()
testPeerID1 := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID1))
panicErrNil(tx.UnWhitelistClient(testPeerID1))
testPeerID := "-lt0D30-"
panicErrNil(tx.WhitelistClient(testPeerID))
panicErrNil(tx.UnWhitelistClient(testPeerID))
found, err := tx.ClientWhitelisted(testPeerID1)
found, err := tx.ClientWhitelisted(testPeerID)
panicErrNil(err)
if found {
t.Error("removed peerID found", testPeerID1)
t.Error("removed peerID found", testPeerID)
}
}
func TestAddSeeder(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testSeeder1 := createTestPeer(createTestUserID(), testTorrent1.ID)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent1, testSeeder1))
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundSeeder, found := foundTorrent.Seeders[testSeeder1.ID]
if found && foundSeeder != *testSeeder1 {
t.Error("seeder not added to cache", testSeeder1)
foundSeeder, found := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found && foundSeeder != *testSeeder {
t.Error("seeder not added to cache", testSeeder)
}
foundSeeder, found = testTorrent1.Seeders[testSeeder1.ID]
if found && foundSeeder != *testSeeder1 {
t.Error("seeder not added to local", testSeeder1)
foundSeeder, found = testTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found && foundSeeder != *testSeeder {
t.Error("seeder not added to local", testSeeder)
}
}
func TestAddLeecher(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testLeecher1 := createTestPeer(createTestUserID(), testTorrent1.ID)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
tx.AddLeecher(testTorrent1, testLeecher1)
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundLeecher, found := foundTorrent.Leechers[testLeecher1.ID]
if found && foundLeecher != *testLeecher1 {
t.Error("leecher not added to cache", testLeecher1)
foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to cache", testLeecher)
}
foundLeecher, found = testTorrent1.Leechers[testLeecher1.ID]
if found && foundLeecher != *testLeecher1 {
t.Error("leecher not added to local", testLeecher1)
foundLeecher, found = testTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found && foundLeecher != *testLeecher {
t.Error("leecher not added to local", testLeecher)
}
}
func TestRemoveSeeder(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testSeeder1 := createTestPeer(createTestUserID(), testTorrent1.ID)
tx.AddSeeder(testTorrent1, testSeeder1)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder))
panicErrNil(tx.RemoveSeeder(testTorrent1, testSeeder1))
foundSeeder, found := testTorrent1.Seeders[testSeeder1.ID]
if found || foundSeeder == *testSeeder1 {
panicErrNil(tx.RemoveSeeder(testTorrent, testSeeder))
foundSeeder, found := testTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from local", foundSeeder)
}
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundSeeder, found = foundTorrent.Seeders[testSeeder1.ID]
if found || foundSeeder == *testSeeder1 {
t.Error("seeder not removed from cache", foundSeeder)
foundSeeder, found = foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if found || foundSeeder == *testSeeder {
t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
}
}
func TestRemoveLeecher(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testLeecher1 := createTestPeer(createTestUserID(), testTorrent1.ID)
tx.AddLeecher(testTorrent1, testLeecher1)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher))
tx.RemoveLeecher(testTorrent1, testLeecher1)
foundTorrent, found, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.RemoveLeecher(testTorrent, testLeecher))
foundTorrent, found, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundLeecher, found := foundTorrent.Leechers[testLeecher1.ID]
if found || foundLeecher == *testLeecher1 {
t.Error("leecher not removed from cache", foundLeecher)
foundLeecher, found := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found || foundLeecher == *testLeecher {
t.Error("leecher not removed from cache", foundLeecher, *testLeecher)
}
foundLeecher, found = testTorrent1.Leechers[testLeecher1.ID]
if found || foundLeecher == *testLeecher1 {
t.Error("leecher not removed from local", foundLeecher)
foundLeecher, found = testTorrent.Leechers[models.PeerMapKey(testLeecher)]
if found || foundLeecher == *testLeecher {
t.Error("leecher not removed from local", foundLeecher, *testLeecher)
}
}
func TestSetSeeder(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testSeeder1 := createTestPeer(createTestUserID(), testTorrent1.ID)
tx.AddSeeder(testTorrent1, testSeeder1)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddSeeder(testTorrent, testSeeder))
testSeeder1.Uploaded += 100
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder.Uploaded += uint64(r.Int63())
tx.SetSeeder(testTorrent1, testSeeder1)
foundTorrent, _, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.SetSeeder(testTorrent, testSeeder))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundSeeder, _ := foundTorrent.Seeders[testSeeder1.ID]
if foundSeeder != *testSeeder1 {
t.Error("seeder not updated in cache", testSeeder1)
foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in cache", foundSeeder, *testSeeder)
}
foundSeeder, _ = testTorrent1.Seeders[testSeeder1.ID]
if foundSeeder != *testSeeder1 {
t.Error("seeder not updated in local", testSeeder1)
foundSeeder, _ = testTorrent.Seeders[models.PeerMapKey(testSeeder)]
if foundSeeder != *testSeeder {
t.Error("seeder not updated in local", foundSeeder, *testSeeder)
}
}
func TestSetLeecher(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testLeecher1 := createTestPeer(createTestUserID(), testTorrent1.ID)
tx.AddLeecher(testTorrent1, testLeecher1)
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher))
testLeecher1.Uploaded += 100
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testLeecher.Uploaded += uint64(r.Int63())
tx.SetLeecher(testTorrent1, testLeecher1)
foundTorrent, _, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.SetLeecher(testTorrent, testLeecher))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundLeecher, _ := foundTorrent.Leechers[testLeecher1.ID]
if foundLeecher != *testLeecher1 {
t.Error("leecher not updated in cache", testLeecher1)
foundLeecher, _ := foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if foundLeecher != *testLeecher {
t.Error("leecher not updated in cache", testLeecher)
}
foundLeecher, _ = testTorrent1.Leechers[testLeecher1.ID]
if foundLeecher != *testLeecher1 {
t.Error("leecher not updated in local", testLeecher1)
foundLeecher, _ = testTorrent.Leechers[models.PeerMapKey(testLeecher)]
if foundLeecher != *testLeecher {
t.Error("leecher not updated in local", testLeecher)
}
}
func TestIncrementSlots(t *testing.T) {
tx := createTestTx()
testUser := createTestUser()
panicErrNil(tx.AddUser(testUser))
numSlots := testUser.Slots
panicErrNil(tx.IncrementSlots(testUser))
foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(err)
if foundUser.Slots != numSlots+1 {
t.Error("cached slots not incremented")
}
if testUser.Slots != numSlots+1 {
t.Error("local slots not incremented")
}
}
func TestDecrementSlots(t *testing.T) {
tx := createTestTx()
testUser := createTestUser()
panicErrNil(tx.AddUser(testUser))
numSlots := testUser.Slots
panicErrNil(tx.DecrementSlots(testUser))
foundUser, _, err := tx.FindUser(testUser.Passkey)
panicErrNil(err)
if foundUser.Slots != numSlots-1 {
t.Error("cached slots not incremented")
}
if testUser.Slots != numSlots-1 {
t.Error("local slots not incremented")
}
}
func TestLeecherFinished(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent1))
testLeecher1 := createTestPeer(createTestUserID(), testTorrent1.ID)
tx.AddLeecher(testTorrent1, testLeecher1)
testLeecher1.Left = 0
testTorrent := createTestTorrent()
panicErrNil(tx.AddTorrent(testTorrent))
testLeecher := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddLeecher(testTorrent, testLeecher))
testLeecher.Left = 0
tx.LeecherFinished(testTorrent1, testLeecher1)
foundTorrent, _, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.LeecherFinished(testTorrent, testLeecher))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
foundSeeder, _ := foundTorrent.Seeders[testLeecher1.ID]
if foundSeeder != *testLeecher1 {
t.Error("seeder not added to cache", testLeecher1, foundSeeder)
foundSeeder, _ := foundTorrent.Seeders[models.PeerMapKey(testLeecher)]
if foundSeeder != *testLeecher {
t.Error("seeder not added to cache", foundSeeder, *testLeecher)
}
foundSeeder, _ = foundTorrent.Leechers[testLeecher1.ID]
if foundSeeder == *testLeecher1 {
t.Error("leecher not removed from cache", testLeecher1)
foundSeeder, _ = foundTorrent.Leechers[models.PeerMapKey(testLeecher)]
if foundSeeder == *testLeecher {
t.Error("leecher not removed from cache", testLeecher)
}
foundSeeder, _ = testTorrent1.Seeders[testLeecher1.ID]
if foundSeeder != *testLeecher1 {
t.Error("seeder not added to local", testLeecher1)
foundSeeder, _ = testTorrent.Seeders[models.PeerMapKey(testLeecher)]
if foundSeeder != *testLeecher {
t.Error("seeder not added to local", testLeecher)
}
foundSeeder, _ = testTorrent1.Leechers[testLeecher1.ID]
if foundSeeder == *testLeecher1 {
t.Error("leecher not removed from local", testLeecher1)
foundSeeder, _ = testTorrent.Leechers[models.PeerMapKey(testLeecher)]
if foundSeeder == *testLeecher {
t.Error("leecher not removed from local", testLeecher)
}
}
// Add, update, verify remove
func TestUpdatePeer(t *testing.T) {
tx := createTestTx()
testTorrent1 := createTestTorrent()
testSeeder1 := createTestPeer(createTestUserID(), testTorrent1.ID)
panicErrNil(tx.AddTorrent(testTorrent1))
panicErrNil(tx.AddSeeder(testTorrent1, testSeeder1))
testTorrent := createTestTorrent()
testSeeder := createTestPeer(createTestUserID(), testTorrent.ID)
panicErrNil(tx.AddTorrent(testTorrent))
panicErrNil(tx.AddSeeder(testTorrent, testSeeder))
// Update a seeder, set it, then check to make sure it updated
r := rand.New(rand.NewSource(time.Now().UnixNano()))
testSeeder1.Uploaded += uint64(r.Int63())
testSeeder.Uploaded += uint64(r.Int63())
panicErrNil(tx.SetSeeder(testTorrent1, testSeeder1))
panicErrNil(tx.SetSeeder(testTorrent, testSeeder))
panicErrNil(tx.RemoveSeeder(testTorrent1, testSeeder1))
foundTorrent, _, err := tx.FindTorrent(testTorrent1.Infohash)
panicErrNil(tx.RemoveSeeder(testTorrent, testSeeder))
foundTorrent, _, err := tx.FindTorrent(testTorrent.Infohash)
panicErrNil(err)
if seeder1, exists := foundTorrent.Seeders[testSeeder1.ID]; exists {
t.Error("seeder not removed from cache", seeder1)
if seeder, exists := foundTorrent.Seeders[models.PeerMapKey(testSeeder)]; exists {
t.Error("seeder not removed from cache", seeder)
}
if seeder1, exists := testTorrent1.Seeders[testSeeder1.ID]; exists {
t.Error("seeder not removed from local", seeder1)
if seeder, exists := testTorrent.Seeders[models.PeerMapKey(testSeeder)]; exists {
t.Error("seeder not removed from local", seeder)
}
}