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.

145 lines
3.8 KiB

  1. package v1
  2. import (
  3. "os"
  4. "strings"
  5. "sync"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/abci/example/kvstore"
  9. "github.com/tendermint/tendermint/config"
  10. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  11. "github.com/tendermint/tendermint/internal/p2p"
  12. "github.com/tendermint/tendermint/internal/p2p/p2ptest"
  13. "github.com/tendermint/tendermint/libs/log"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. type reactorTestSuite struct {
  17. network *p2ptest.Network
  18. logger log.Logger
  19. reactors map[types.NodeID]*Reactor
  20. mempoolChannels map[types.NodeID]*p2p.Channel
  21. mempools map[types.NodeID]*TxMempool
  22. kvstores map[types.NodeID]*kvstore.Application
  23. peerChans map[types.NodeID]chan p2p.PeerUpdate
  24. peerUpdates map[types.NodeID]*p2p.PeerUpdates
  25. nodes []types.NodeID
  26. }
  27. func setupReactors(t *testing.T, numNodes int, chBuf uint) *reactorTestSuite {
  28. t.Helper()
  29. cfg := config.ResetTestRoot(strings.ReplaceAll(t.Name(), "/", "|"))
  30. t.Cleanup(func() {
  31. os.RemoveAll(cfg.RootDir)
  32. })
  33. rts := &reactorTestSuite{
  34. logger: log.TestingLogger().With("testCase", t.Name()),
  35. network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
  36. reactors: make(map[types.NodeID]*Reactor, numNodes),
  37. mempoolChannels: make(map[types.NodeID]*p2p.Channel, numNodes),
  38. mempools: make(map[types.NodeID]*TxMempool, numNodes),
  39. kvstores: make(map[types.NodeID]*kvstore.Application, numNodes),
  40. peerChans: make(map[types.NodeID]chan p2p.PeerUpdate, numNodes),
  41. peerUpdates: make(map[types.NodeID]*p2p.PeerUpdates, numNodes),
  42. }
  43. chDesc := GetChannelDescriptor(cfg.Mempool)
  44. rts.mempoolChannels = rts.network.MakeChannelsNoCleanup(t, chDesc)
  45. for nodeID := range rts.network.Nodes {
  46. rts.kvstores[nodeID] = kvstore.NewApplication()
  47. mempool := setup(t, 0)
  48. rts.mempools[nodeID] = mempool
  49. rts.peerChans[nodeID] = make(chan p2p.PeerUpdate)
  50. rts.peerUpdates[nodeID] = p2p.NewPeerUpdates(rts.peerChans[nodeID], 1)
  51. rts.network.Nodes[nodeID].PeerManager.Register(rts.peerUpdates[nodeID])
  52. rts.reactors[nodeID] = NewReactor(
  53. rts.logger.With("nodeID", nodeID),
  54. cfg.Mempool,
  55. rts.network.Nodes[nodeID].PeerManager,
  56. mempool,
  57. rts.mempoolChannels[nodeID],
  58. rts.peerUpdates[nodeID],
  59. )
  60. rts.nodes = append(rts.nodes, nodeID)
  61. require.NoError(t, rts.reactors[nodeID].Start())
  62. require.True(t, rts.reactors[nodeID].IsRunning())
  63. }
  64. require.Len(t, rts.reactors, numNodes)
  65. t.Cleanup(func() {
  66. for nodeID := range rts.reactors {
  67. if rts.reactors[nodeID].IsRunning() {
  68. require.NoError(t, rts.reactors[nodeID].Stop())
  69. require.False(t, rts.reactors[nodeID].IsRunning())
  70. }
  71. }
  72. })
  73. return rts
  74. }
  75. func (rts *reactorTestSuite) start(t *testing.T) {
  76. t.Helper()
  77. rts.network.Start(t)
  78. require.Len(t,
  79. rts.network.RandomNode().PeerManager.Peers(),
  80. len(rts.nodes)-1,
  81. "network does not have expected number of nodes")
  82. }
  83. func TestReactorBroadcastDoesNotPanic(t *testing.T) {
  84. numNodes := 2
  85. rts := setupReactors(t, numNodes, 0)
  86. observePanic := func(r interface{}) {
  87. t.Fatal("panic detected in reactor")
  88. }
  89. primary := rts.nodes[0]
  90. secondary := rts.nodes[1]
  91. primaryReactor := rts.reactors[primary]
  92. primaryMempool := primaryReactor.mempool
  93. secondaryReactor := rts.reactors[secondary]
  94. primaryReactor.observePanic = observePanic
  95. secondaryReactor.observePanic = observePanic
  96. firstTx := &WrappedTx{}
  97. primaryMempool.insertTx(firstTx)
  98. // run the router
  99. rts.start(t)
  100. closer := tmsync.NewCloser()
  101. primaryReactor.peerWG.Add(1)
  102. go primaryReactor.broadcastTxRoutine(secondary, closer)
  103. wg := &sync.WaitGroup{}
  104. for i := 0; i < 50; i++ {
  105. next := &WrappedTx{}
  106. wg.Add(1)
  107. go func() {
  108. defer wg.Done()
  109. primaryMempool.insertTx(next)
  110. }()
  111. }
  112. err := primaryReactor.Stop()
  113. require.NoError(t, err)
  114. primaryReactor.peerWG.Wait()
  115. wg.Wait()
  116. }