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.

147 lines
3.7 KiB

9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
  1. package p2p
  2. import (
  3. "math/rand"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. )
  9. // Returns an empty dummy peer
  10. func randPeer() *peer {
  11. return &peer{
  12. key: cmn.RandStr(12),
  13. nodeInfo: &NodeInfo{
  14. RemoteAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
  15. ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
  16. },
  17. }
  18. }
  19. func TestPeerSetAddRemoveOne(t *testing.T) {
  20. t.Parallel()
  21. peerSet := NewPeerSet()
  22. var peerList []Peer
  23. for i := 0; i < 5; i++ {
  24. p := randPeer()
  25. peerSet.Add(p)
  26. peerList = append(peerList, p)
  27. }
  28. n := len(peerList)
  29. // 1. Test removing from the front
  30. for i, peerAtFront := range peerList {
  31. peerSet.Remove(peerAtFront)
  32. wantSize := n - i - 1
  33. for j := 0; j < 2; j++ {
  34. assert.Equal(t, false, peerSet.Has(peerAtFront.Key()), "#%d Run #%d: failed to remove peer", i, j)
  35. assert.Equal(t, wantSize, peerSet.Size(), "#%d Run #%d: failed to remove peer and decrement size", i, j)
  36. // Test the route of removing the now non-existent element
  37. peerSet.Remove(peerAtFront)
  38. }
  39. }
  40. // 2. Next we are testing removing the peer at the end
  41. // a) Replenish the peerSet
  42. for _, peer := range peerList {
  43. peerSet.Add(peer)
  44. }
  45. // b) In reverse, remove each element
  46. for i := n - 1; i >= 0; i-- {
  47. peerAtEnd := peerList[i]
  48. peerSet.Remove(peerAtEnd)
  49. assert.Equal(t, false, peerSet.Has(peerAtEnd.Key()), "#%d: failed to remove item at end", i)
  50. assert.Equal(t, i, peerSet.Size(), "#%d: differing sizes after peerSet.Remove(atEndPeer)", i)
  51. }
  52. }
  53. func TestPeerSetAddRemoveMany(t *testing.T) {
  54. t.Parallel()
  55. peerSet := NewPeerSet()
  56. peers := []Peer{}
  57. N := 100
  58. for i := 0; i < N; i++ {
  59. peer := randPeer()
  60. if err := peerSet.Add(peer); err != nil {
  61. t.Errorf("Failed to add new peer")
  62. }
  63. if peerSet.Size() != i+1 {
  64. t.Errorf("Failed to add new peer and increment size")
  65. }
  66. peers = append(peers, peer)
  67. }
  68. for i, peer := range peers {
  69. peerSet.Remove(peer)
  70. if peerSet.Has(peer.Key()) {
  71. t.Errorf("Failed to remove peer")
  72. }
  73. if peerSet.Size() != len(peers)-i-1 {
  74. t.Errorf("Failed to remove peer and decrement size")
  75. }
  76. }
  77. }
  78. func TestPeerSetAddDuplicate(t *testing.T) {
  79. t.Parallel()
  80. peerSet := NewPeerSet()
  81. peer := randPeer()
  82. n := 20
  83. errsChan := make(chan error)
  84. // Add the same asynchronously to test the
  85. // concurrent guarantees of our APIs, and
  86. // our expectation in the end is that only
  87. // one addition succeeded, but the rest are
  88. // instances of ErrSwitchDuplicatePeer.
  89. for i := 0; i < n; i++ {
  90. go func() {
  91. errsChan <- peerSet.Add(peer)
  92. }()
  93. }
  94. // Now collect and tally the results
  95. errsTally := make(map[error]int)
  96. for i := 0; i < n; i++ {
  97. err := <-errsChan
  98. errsTally[err] += 1
  99. }
  100. // Our next procedure is to ensure that only one addition
  101. // succeeded and that the rest are each ErrSwitchDuplicatePeer.
  102. wantErrCount, gotErrCount := n-1, errsTally[ErrSwitchDuplicatePeer]
  103. assert.Equal(t, wantErrCount, gotErrCount, "invalid ErrSwitchDuplicatePeer count")
  104. wantNilErrCount, gotNilErrCount := 1, errsTally[nil]
  105. assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount")
  106. }
  107. func TestPeerSetGet(t *testing.T) {
  108. t.Parallel()
  109. peerSet := NewPeerSet()
  110. peer := randPeer()
  111. assert.Nil(t, peerSet.Get(peer.Key()), "expecting a nil lookup, before .Add")
  112. if err := peerSet.Add(peer); err != nil {
  113. t.Fatalf("Failed to add new peer: %v", err)
  114. }
  115. var wg sync.WaitGroup
  116. for i := 0; i < 10; i++ {
  117. // Add them asynchronously to test the
  118. // concurrent guarantees of our APIs.
  119. wg.Add(1)
  120. go func(i int) {
  121. defer wg.Done()
  122. got, want := peerSet.Get(peer.Key()), peer
  123. assert.Equal(t, got, want, "#%d: got=%v want=%v", i, got, want)
  124. }(i)
  125. }
  126. wg.Wait()
  127. }