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.

182 lines
4.8 KiB

  1. package p2p
  2. import (
  3. "net"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. cmn "github.com/tendermint/tendermint/libs/common"
  9. )
  10. // mockPeer for testing the PeerSet
  11. type mockPeer struct {
  12. cmn.BaseService
  13. ip net.IP
  14. id ID
  15. }
  16. func (mp *mockPeer) TrySend(chID byte, msgBytes []byte) bool { return true }
  17. func (mp *mockPeer) Send(chID byte, msgBytes []byte) bool { return true }
  18. func (mp *mockPeer) NodeInfo() NodeInfo { return DefaultNodeInfo{} }
  19. func (mp *mockPeer) Status() ConnectionStatus { return ConnectionStatus{} }
  20. func (mp *mockPeer) ID() ID { return mp.id }
  21. func (mp *mockPeer) IsOutbound() bool { return false }
  22. func (mp *mockPeer) IsPersistent() bool { return true }
  23. func (mp *mockPeer) Get(s string) interface{} { return s }
  24. func (mp *mockPeer) Set(string, interface{}) {}
  25. func (mp *mockPeer) RemoteIP() net.IP { return mp.ip }
  26. // Returns a mock peer
  27. func newMockPeer(ip net.IP) *mockPeer {
  28. if ip == nil {
  29. ip = net.IP{127, 0, 0, 1}
  30. }
  31. nodeKey := NodeKey{PrivKey: ed25519.GenPrivKey()}
  32. return &mockPeer{
  33. ip: ip,
  34. id: nodeKey.ID(),
  35. }
  36. }
  37. func TestPeerSetAddRemoveOne(t *testing.T) {
  38. t.Parallel()
  39. peerSet := NewPeerSet()
  40. var peerList []Peer
  41. for i := 0; i < 5; i++ {
  42. p := newMockPeer(net.IP{127, 0, 0, byte(i)})
  43. if err := peerSet.Add(p); err != nil {
  44. t.Error(err)
  45. }
  46. peerList = append(peerList, p)
  47. }
  48. n := len(peerList)
  49. // 1. Test removing from the front
  50. for i, peerAtFront := range peerList {
  51. peerSet.Remove(peerAtFront)
  52. wantSize := n - i - 1
  53. for j := 0; j < 2; j++ {
  54. assert.Equal(t, false, peerSet.Has(peerAtFront.ID()), "#%d Run #%d: failed to remove peer", i, j)
  55. assert.Equal(t, wantSize, peerSet.Size(), "#%d Run #%d: failed to remove peer and decrement size", i, j)
  56. // Test the route of removing the now non-existent element
  57. peerSet.Remove(peerAtFront)
  58. }
  59. }
  60. // 2. Next we are testing removing the peer at the end
  61. // a) Replenish the peerSet
  62. for _, peer := range peerList {
  63. if err := peerSet.Add(peer); err != nil {
  64. t.Error(err)
  65. }
  66. }
  67. // b) In reverse, remove each element
  68. for i := n - 1; i >= 0; i-- {
  69. peerAtEnd := peerList[i]
  70. peerSet.Remove(peerAtEnd)
  71. assert.Equal(t, false, peerSet.Has(peerAtEnd.ID()), "#%d: failed to remove item at end", i)
  72. assert.Equal(t, i, peerSet.Size(), "#%d: differing sizes after peerSet.Remove(atEndPeer)", i)
  73. }
  74. }
  75. func TestPeerSetAddRemoveMany(t *testing.T) {
  76. t.Parallel()
  77. peerSet := NewPeerSet()
  78. peers := []Peer{}
  79. N := 100
  80. for i := 0; i < N; i++ {
  81. peer := newMockPeer(net.IP{127, 0, 0, byte(i)})
  82. if err := peerSet.Add(peer); err != nil {
  83. t.Errorf("Failed to add new peer")
  84. }
  85. if peerSet.Size() != i+1 {
  86. t.Errorf("Failed to add new peer and increment size")
  87. }
  88. peers = append(peers, peer)
  89. }
  90. for i, peer := range peers {
  91. peerSet.Remove(peer)
  92. if peerSet.Has(peer.ID()) {
  93. t.Errorf("Failed to remove peer")
  94. }
  95. if peerSet.Size() != len(peers)-i-1 {
  96. t.Errorf("Failed to remove peer and decrement size")
  97. }
  98. }
  99. }
  100. func TestPeerSetAddDuplicate(t *testing.T) {
  101. t.Parallel()
  102. peerSet := NewPeerSet()
  103. peer := newMockPeer(nil)
  104. n := 20
  105. errsChan := make(chan error)
  106. // Add the same asynchronously to test the
  107. // concurrent guarantees of our APIs, and
  108. // our expectation in the end is that only
  109. // one addition succeeded, but the rest are
  110. // instances of ErrSwitchDuplicatePeer.
  111. for i := 0; i < n; i++ {
  112. go func() {
  113. errsChan <- peerSet.Add(peer)
  114. }()
  115. }
  116. // Now collect and tally the results
  117. errsTally := make(map[string]int)
  118. for i := 0; i < n; i++ {
  119. err := <-errsChan
  120. switch err.(type) {
  121. case ErrSwitchDuplicatePeerID:
  122. errsTally["duplicateID"]++
  123. default:
  124. errsTally["other"]++
  125. }
  126. }
  127. // Our next procedure is to ensure that only one addition
  128. // succeeded and that the rest are each ErrSwitchDuplicatePeer.
  129. wantErrCount, gotErrCount := n-1, errsTally["duplicateID"]
  130. assert.Equal(t, wantErrCount, gotErrCount, "invalid ErrSwitchDuplicatePeer count")
  131. wantNilErrCount, gotNilErrCount := 1, errsTally["other"]
  132. assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount")
  133. }
  134. func TestPeerSetGet(t *testing.T) {
  135. t.Parallel()
  136. var (
  137. peerSet = NewPeerSet()
  138. peer = newMockPeer(nil)
  139. )
  140. assert.Nil(t, peerSet.Get(peer.ID()), "expecting a nil lookup, before .Add")
  141. if err := peerSet.Add(peer); err != nil {
  142. t.Fatalf("Failed to add new peer: %v", err)
  143. }
  144. var wg sync.WaitGroup
  145. for i := 0; i < 10; i++ {
  146. // Add them asynchronously to test the
  147. // concurrent guarantees of our APIs.
  148. wg.Add(1)
  149. go func(i int) {
  150. defer wg.Done()
  151. have, want := peerSet.Get(peer.ID()), peer
  152. assert.Equal(t, have, want, "%d: have %v, want %v", i, have, want)
  153. }(i)
  154. }
  155. wg.Wait()
  156. }