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.

205 lines
5.3 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package behaviour_test
  2. import (
  3. "sync"
  4. "testing"
  5. bh "github.com/tendermint/tendermint/behaviour"
  6. "github.com/tendermint/tendermint/p2p"
  7. )
  8. // TestMockReporter tests the MockReporter's ability to store reported
  9. // peer behaviour in memory indexed by the peerID.
  10. func TestMockReporter(t *testing.T) {
  11. var peerID p2p.ID = "MockPeer"
  12. pr := bh.NewMockReporter()
  13. behaviours := pr.GetBehaviours(peerID)
  14. if len(behaviours) != 0 {
  15. t.Error("Expected to have no behaviours reported")
  16. }
  17. badMessage := bh.BadMessage(peerID, "bad message")
  18. if err := pr.Report(badMessage); err != nil {
  19. t.Error(err)
  20. }
  21. behaviours = pr.GetBehaviours(peerID)
  22. if len(behaviours) != 1 {
  23. t.Error("Expected the peer have one reported behaviour")
  24. }
  25. if behaviours[0] != badMessage {
  26. t.Error("Expected Bad Message to have been reported")
  27. }
  28. }
  29. type scriptItem struct {
  30. peerID p2p.ID
  31. behaviour bh.PeerBehaviour
  32. }
  33. // equalBehaviours returns true if a and b contain the same PeerBehaviours with
  34. // the same freequencies and otherwise false.
  35. func equalBehaviours(a []bh.PeerBehaviour, b []bh.PeerBehaviour) bool {
  36. aHistogram := map[bh.PeerBehaviour]int{}
  37. bHistogram := map[bh.PeerBehaviour]int{}
  38. for _, behaviour := range a {
  39. aHistogram[behaviour]++
  40. }
  41. for _, behaviour := range b {
  42. bHistogram[behaviour]++
  43. }
  44. if len(aHistogram) != len(bHistogram) {
  45. return false
  46. }
  47. for _, behaviour := range a {
  48. if aHistogram[behaviour] != bHistogram[behaviour] {
  49. return false
  50. }
  51. }
  52. for _, behaviour := range b {
  53. if bHistogram[behaviour] != aHistogram[behaviour] {
  54. return false
  55. }
  56. }
  57. return true
  58. }
  59. // TestEqualPeerBehaviours tests that equalBehaviours can tell that two slices
  60. // of peer behaviours can be compared for the behaviours they contain and the
  61. // freequencies that those behaviours occur.
  62. func TestEqualPeerBehaviours(t *testing.T) {
  63. var (
  64. peerID p2p.ID = "MockPeer"
  65. consensusVote = bh.ConsensusVote(peerID, "voted")
  66. blockPart = bh.BlockPart(peerID, "blocked")
  67. equals = []struct {
  68. left []bh.PeerBehaviour
  69. right []bh.PeerBehaviour
  70. }{
  71. // Empty sets
  72. {[]bh.PeerBehaviour{}, []bh.PeerBehaviour{}},
  73. // Single behaviours
  74. {[]bh.PeerBehaviour{consensusVote}, []bh.PeerBehaviour{consensusVote}},
  75. // Equal Frequencies
  76. {[]bh.PeerBehaviour{consensusVote, consensusVote},
  77. []bh.PeerBehaviour{consensusVote, consensusVote}},
  78. // Equal frequencies different orders
  79. {[]bh.PeerBehaviour{consensusVote, blockPart},
  80. []bh.PeerBehaviour{blockPart, consensusVote}},
  81. }
  82. unequals = []struct {
  83. left []bh.PeerBehaviour
  84. right []bh.PeerBehaviour
  85. }{
  86. // Comparing empty sets to non empty sets
  87. {[]bh.PeerBehaviour{}, []bh.PeerBehaviour{consensusVote}},
  88. // Different behaviours
  89. {[]bh.PeerBehaviour{consensusVote}, []bh.PeerBehaviour{blockPart}},
  90. // Same behaviour with different frequencies
  91. {[]bh.PeerBehaviour{consensusVote},
  92. []bh.PeerBehaviour{consensusVote, consensusVote}},
  93. }
  94. )
  95. for _, test := range equals {
  96. if !equalBehaviours(test.left, test.right) {
  97. t.Errorf("expected %#v and %#v to be equal", test.left, test.right)
  98. }
  99. }
  100. for _, test := range unequals {
  101. if equalBehaviours(test.left, test.right) {
  102. t.Errorf("expected %#v and %#v to be unequal", test.left, test.right)
  103. }
  104. }
  105. }
  106. // TestPeerBehaviourConcurrency constructs a scenario in which
  107. // multiple goroutines are using the same MockReporter instance.
  108. // This test reproduces the conditions in which MockReporter will
  109. // be used within a Reactor `Receive` method tests to ensure thread safety.
  110. func TestMockPeerBehaviourReporterConcurrency(t *testing.T) {
  111. var (
  112. behaviourScript = []struct {
  113. peerID p2p.ID
  114. behaviours []bh.PeerBehaviour
  115. }{
  116. {"1", []bh.PeerBehaviour{bh.ConsensusVote("1", "")}},
  117. {"2", []bh.PeerBehaviour{bh.ConsensusVote("2", ""), bh.ConsensusVote("2", ""), bh.ConsensusVote("2", "")}},
  118. {
  119. "3",
  120. []bh.PeerBehaviour{bh.BlockPart("3", ""),
  121. bh.ConsensusVote("3", ""),
  122. bh.BlockPart("3", ""),
  123. bh.ConsensusVote("3", "")}},
  124. {
  125. "4",
  126. []bh.PeerBehaviour{bh.ConsensusVote("4", ""),
  127. bh.ConsensusVote("4", ""),
  128. bh.ConsensusVote("4", ""),
  129. bh.ConsensusVote("4", "")}},
  130. {
  131. "5",
  132. []bh.PeerBehaviour{bh.BlockPart("5", ""),
  133. bh.ConsensusVote("5", ""),
  134. bh.BlockPart("5", ""),
  135. bh.ConsensusVote("5", "")}},
  136. }
  137. )
  138. var receiveWg sync.WaitGroup
  139. pr := bh.NewMockReporter()
  140. scriptItems := make(chan scriptItem)
  141. done := make(chan int)
  142. numConsumers := 3
  143. for i := 0; i < numConsumers; i++ {
  144. receiveWg.Add(1)
  145. go func() {
  146. defer receiveWg.Done()
  147. for {
  148. select {
  149. case pb := <-scriptItems:
  150. if err := pr.Report(pb.behaviour); err != nil {
  151. t.Error(err)
  152. }
  153. case <-done:
  154. return
  155. }
  156. }
  157. }()
  158. }
  159. var sendingWg sync.WaitGroup
  160. sendingWg.Add(1)
  161. go func() {
  162. defer sendingWg.Done()
  163. for _, item := range behaviourScript {
  164. for _, reason := range item.behaviours {
  165. scriptItems <- scriptItem{item.peerID, reason}
  166. }
  167. }
  168. }()
  169. sendingWg.Wait()
  170. for i := 0; i < numConsumers; i++ {
  171. done <- 1
  172. }
  173. receiveWg.Wait()
  174. for _, items := range behaviourScript {
  175. reported := pr.GetBehaviours(items.peerID)
  176. if !equalBehaviours(reported, items.behaviours) {
  177. t.Errorf("expected peer %s to have behaved \nExpected: %#v \nGot %#v \n",
  178. items.peerID, items.behaviours, reported)
  179. }
  180. }
  181. }