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.

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