You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.3 KiB

10 years ago
  1. package p2p
  2. import (
  3. "math/rand"
  4. "testing"
  5. . "github.com/tendermint/tendermint2/common"
  6. )
  7. // Returns an empty dummy peer
  8. func randPeer() *Peer {
  9. return &Peer{
  10. Key: Fmt("%v.%v.%v.%v:%v", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%10000+80),
  11. }
  12. }
  13. func TestAddRemoveOne(t *testing.T) {
  14. peerSet := NewPeerSet()
  15. peer := randPeer()
  16. added := peerSet.Add(peer)
  17. if !added {
  18. t.Errorf("Failed to add new peer")
  19. }
  20. if peerSet.Size() != 1 {
  21. t.Errorf("Failed to add new peer and increment size")
  22. }
  23. peerSet.Remove(peer)
  24. if peerSet.Has(peer.Key) {
  25. t.Errorf("Failed to remove peer")
  26. }
  27. if peerSet.Size() != 0 {
  28. t.Errorf("Failed to remove peer and decrement size")
  29. }
  30. }
  31. func TestAddRemoveMany(t *testing.T) {
  32. peerSet := NewPeerSet()
  33. peers := []*Peer{}
  34. for i := 0; i < 100; i++ {
  35. peer := randPeer()
  36. added := peerSet.Add(peer)
  37. if !added {
  38. t.Errorf("Failed to add new peer")
  39. }
  40. if peerSet.Size() != i+1 {
  41. t.Errorf("Failed to add new peer and increment size")
  42. }
  43. peers = append(peers, peer)
  44. }
  45. for i, peer := range peers {
  46. peerSet.Remove(peer)
  47. if peerSet.Has(peer.Key) {
  48. t.Errorf("Failed to remove peer")
  49. }
  50. if peerSet.Size() != len(peers)-i-1 {
  51. t.Errorf("Failed to remove peer and decrement size")
  52. }
  53. }
  54. }