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.

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