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.

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