From 11c0c3cb3c45b97fe40289f1f59344dd4e737320 Mon Sep 17 00:00:00 2001
From: cpb8010 <cpb8010@gmail.com>
Date: Sat, 28 Sep 2013 09:09:03 -0400
Subject: [PATCH] Cleanup Redis keys after tests and benchmarks

---
 cache/redis/redis_bench_test.go | 47 +++++++++++++++++++++++++++++++--
 cache/redis/tx_test.go          | 41 ++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/cache/redis/redis_bench_test.go b/cache/redis/redis_bench_test.go
index 59cfe00..74aa054 100644
--- a/cache/redis/redis_bench_test.go
+++ b/cache/redis/redis_bench_test.go
@@ -2,7 +2,6 @@
 // Use of this source code is governed by the BSD 2-Clause license,
 // which can be found in the LICENSE file.
 
-// Benchmarks two different redis schemeas
 package redis
 
 import (
@@ -29,6 +28,10 @@ func BenchmarkSuccessfulFindUser(b *testing.B) {
 			b.Error("found user mismatch", *foundUser, testUser)
 		}
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveUser(testUser))
+	b.StartTimer()
 }
 
 func BenchmarkFailedFindUser(b *testing.B) {
@@ -66,6 +69,10 @@ func BenchmarkSuccessfulFindTorrent(b *testing.B) {
 			b.Error("found torrent mismatch", foundTorrent, testTorrent)
 		}
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 func BenchmarkFailFindTorrent(b *testing.B) {
@@ -97,6 +104,10 @@ func BenchmarkSuccessfulClientWhitelisted(b *testing.B) {
 			b.Error("peerID not found", testPeerID)
 		}
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.UnWhitelistClient(testPeerID))
+	b.StartTimer()
 }
 
 func BenchmarkFailClientWhitelisted(b *testing.B) {
@@ -126,6 +137,11 @@ func BenchmarkRecordSnatch(b *testing.B) {
 	for bCount := 0; bCount < b.N; bCount++ {
 		panicOnErr(tx.RecordSnatch(testUser, testTorrent))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	panicOnErr(tx.RemoveUser(testUser))
+	b.StartTimer()
 }
 
 func BenchmarkMarkActive(b *testing.B) {
@@ -139,6 +155,10 @@ func BenchmarkMarkActive(b *testing.B) {
 	for bCount := 0; bCount < b.N; bCount++ {
 		panicOnErr(tx.MarkActive(testTorrent))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 func BenchmarkAddSeeder(b *testing.B) {
@@ -155,6 +175,10 @@ func BenchmarkAddSeeder(b *testing.B) {
 
 		panicOnErr(tx.AddSeeder(testTorrent, testSeeder))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 func BenchmarkRemoveSeeder(b *testing.B) {
@@ -172,6 +196,10 @@ func BenchmarkRemoveSeeder(b *testing.B) {
 
 		panicOnErr(tx.RemoveSeeder(testTorrent, testSeeder))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 func BenchmarkSetSeeder(b *testing.B) {
@@ -191,6 +219,10 @@ func BenchmarkSetSeeder(b *testing.B) {
 
 		tx.SetSeeder(testTorrent, testSeeder)
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 func BenchmarkIncrementSlots(b *testing.B) {
@@ -203,6 +235,10 @@ func BenchmarkIncrementSlots(b *testing.B) {
 	for bCount := 0; bCount < b.N; bCount++ {
 		panicOnErr(tx.IncrementSlots(testUser))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveUser(testUser))
+	b.StartTimer()
 }
 
 func BenchmarkLeecherFinished(b *testing.B) {
@@ -221,6 +257,10 @@ func BenchmarkLeecherFinished(b *testing.B) {
 
 		panicOnErr(tx.LeecherFinished(testTorrent, testLeecher))
 	}
+	// Cleanup
+	b.StopTimer()
+	panicOnErr(tx.RemoveTorrent(testTorrent))
+	b.StartTimer()
 }
 
 // This is a comparision to the Leecher finished function
@@ -241,5 +281,8 @@ func BenchmarkRemoveLeecherAddSeeder(b *testing.B) {
 		panicOnErr(tx.RemoveLeecher(testTorrent, testLeecher))
 		panicOnErr(tx.AddSeeder(testTorrent, testLeecher))
 	}
-
+	// Cleanup
+	b.StopTimer()
+	tx.RemoveTorrent(testTorrent)
+	b.StartTimer()
 }
diff --git a/cache/redis/tx_test.go b/cache/redis/tx_test.go
index 4d39b47..3d9772c 100644
--- a/cache/redis/tx_test.go
+++ b/cache/redis/tx_test.go
@@ -43,6 +43,8 @@ func TestFindUserSuccess(t *testing.T) {
 	if *foundUser != *testUser {
 		t.Error("found user mismatch", *foundUser, testUser)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveUser(testUser))
 }
 
 func TestFindUserFail(t *testing.T) {
@@ -83,6 +85,8 @@ func TestFindTorrentSuccess(t *testing.T) {
 	if !reflect.DeepEqual(foundTorrent, testTorrent) {
 		t.Error("found torrent mismatch", foundTorrent, testTorrent)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestFindTorrentFail(t *testing.T) {
@@ -107,6 +111,8 @@ func TestRemoveTorrent(t *testing.T) {
 	if found {
 		t.Error("removed torrent found", foundTorrent)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestClientWhitelistSuccess(t *testing.T) {
@@ -119,6 +125,8 @@ func TestClientWhitelistSuccess(t *testing.T) {
 	if !found {
 		t.Error("peerID not found", testPeerID)
 	}
+	// Cleanup
+	panicOnErr(tx.UnWhitelistClient(testPeerID))
 }
 
 func TestClientWhitelistFail(t *testing.T) {
@@ -161,6 +169,9 @@ func TestRecordSnatch(t *testing.T) {
 	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) {
@@ -179,6 +190,8 @@ func TestMarkActive(t *testing.T) {
 	if testTorrent.Active != true {
 		t.Error("cached torrent not activated")
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestClientWhitelistRemove(t *testing.T) {
@@ -211,6 +224,8 @@ func TestAddSeeder(t *testing.T) {
 	if found && foundSeeder != *testSeeder {
 		t.Error("seeder not added to local", testSeeder)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestAddLeecher(t *testing.T) {
@@ -230,6 +245,8 @@ func TestAddLeecher(t *testing.T) {
 	if found && foundLeecher != *testLeecher {
 		t.Error("leecher not added to local", testLeecher)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestRemoveSeeder(t *testing.T) {
@@ -251,6 +268,8 @@ func TestRemoveSeeder(t *testing.T) {
 	if found || foundSeeder == *testSeeder {
 		t.Error("seeder not removed from cache", foundSeeder, *testSeeder)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestRemoveLeecher(t *testing.T) {
@@ -271,6 +290,8 @@ func TestRemoveLeecher(t *testing.T) {
 	if found || foundLeecher == *testLeecher {
 		t.Error("leecher not removed from local", foundLeecher, *testLeecher)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestSetSeeder(t *testing.T) {
@@ -295,6 +316,8 @@ func TestSetSeeder(t *testing.T) {
 	if foundSeeder != *testSeeder {
 		t.Error("seeder not updated in local", foundSeeder, *testSeeder)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestSetLeecher(t *testing.T) {
@@ -318,6 +341,8 @@ func TestSetLeecher(t *testing.T) {
 	if foundLeecher != *testLeecher {
 		t.Error("leecher not updated in local", testLeecher)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestIncrementSlots(t *testing.T) {
@@ -336,6 +361,8 @@ func TestIncrementSlots(t *testing.T) {
 	if testUser.Slots != numSlots+1 {
 		t.Error("local slots not incremented")
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveUser(testUser))
 }
 
 func TestDecrementSlots(t *testing.T) {
@@ -354,6 +381,8 @@ func TestDecrementSlots(t *testing.T) {
 	if testUser.Slots != numSlots-1 {
 		t.Error("local slots not incremented")
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveUser(testUser))
 }
 
 func TestLeecherFinished(t *testing.T) {
@@ -384,6 +413,8 @@ func TestLeecherFinished(t *testing.T) {
 	if foundSeeder == *testLeecher {
 		t.Error("leecher not removed from local", testLeecher)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 // Add, update, verify remove
@@ -408,6 +439,8 @@ func TestUpdatePeer(t *testing.T) {
 	if seeder, exists := testTorrent.Seeders[models.PeerMapKey(testSeeder)]; exists {
 		t.Error("seeder not removed from local", seeder)
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestParallelFindUser(t *testing.T) {
@@ -435,6 +468,8 @@ func TestParallelFindUser(t *testing.T) {
 			t.Error("found user mismatch", *foundUser, testUserSuccess)
 		}
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveUser(testUserSuccess))
 }
 
 func TestParallelFindTorrent(t *testing.T) {
@@ -462,6 +497,8 @@ func TestParallelFindTorrent(t *testing.T) {
 			t.Error("torrent found", foundTorrent)
 		}
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrentSuccess))
 }
 
 func TestParallelSetSeeder(t *testing.T) {
@@ -492,6 +529,8 @@ func TestParallelSetSeeder(t *testing.T) {
 			t.Error("seeder not updated in local", foundSeeder, *testSeeder)
 		}
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }
 
 func TestParallelAddLeecher(t *testing.T) {
@@ -519,4 +558,6 @@ func TestParallelAddLeecher(t *testing.T) {
 			t.Error("leecher not added to local", testLeecher)
 		}
 	}
+	// Cleanup
+	panicOnErr(tx.RemoveTorrent(testTorrent))
 }