Ejemplo n.º 1
0
// Tests that the torrent correctly reports it's current number of seeders / leechers.
func TestPeerEnumeration(test *testing.T) {
	torrent := MockTorrent()

	// create four "peers"
	randomPeerIds := make([][]byte, 4)
	for i := 0; i < len(randomPeerIds); i++ {
		randomPeerIds[i] = make([]byte, 20)
		rand.Read(randomPeerIds[i])

		torrent.AddPeer(string(randomPeerIds[i]), "127.0.0.1", "1337", "abcadefgawalthgrathorp")
	}

	// enumerate leechers [4]
	seeders, leechers := torrent.EnumeratePeers()
	if seeders != 0 || leechers != 4 {
		test.Error("Torrent should have four [just added] leechers!")
		test.FailNow()
	}

	// promote two peers
	torrent.UpdateStatsFor(string(randomPeerIds[0]), "0", "1024", "0")
	torrent.UpdateStatsFor(string(randomPeerIds[1]), "0", "1024", "0")

	seeders, leechers = torrent.EnumeratePeers()
	if seeders != 2 || leechers != 2 {
		test.Error("Torrent should have two [just promoted] seeders and two leechers!")
		test.FailNow()
	}
}
Ejemplo n.º 2
0
// Benchmarks how quickly the tracker's `bencoder` can serialize a map.
func BenchmarkResponseMapBencoder(bench *testing.B) {
	bench.StopTimer()
	torrent := MockTorrent()
	responseMap := make(map[string]interface{})

	responseMap["interval"] = 300
	responseMap["min interval"] = 10

	seeding, leeching := torrent.EnumeratePeers()
	responseMap["complete"] = seeding
	responseMap["incomplete"] = leeching

	bench.StartTimer()
	for i := 0; i < bench.N; i++ {
		encodeResponseMap(responseMap)
	}
}