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.

67 lines
1.4 KiB

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