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.

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