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.

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