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.

108 lines
2.9 KiB

7 years ago
  1. package evpool
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/go-kit/kit/log/term"
  9. "github.com/tendermint/abci/example/dummy"
  10. "github.com/tendermint/tmlibs/log"
  11. cfg "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/p2p"
  13. "github.com/tendermint/tendermint/proxy"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. // evpoolLogger is a TestingLogger which uses a different
  17. // color for each validator ("validator" key must exist).
  18. func evpoolLogger() log.Logger {
  19. return log.TestingLoggerWithColorFn(func(keyvals ...interface{}) term.FgBgColor {
  20. for i := 0; i < len(keyvals)-1; i += 2 {
  21. if keyvals[i] == "validator" {
  22. return term.FgBgColor{Fg: term.Color(uint8(keyvals[i+1].(int) + 1))}
  23. }
  24. }
  25. return term.FgBgColor{}
  26. })
  27. }
  28. // connect N evpool reactors through N switches
  29. func makeAndConnectEvidencePoolReactors(config *cfg.Config, N int) []*EvidencePoolReactor {
  30. reactors := make([]*EvidencePoolReactor, N)
  31. logger := evpoolLogger()
  32. for i := 0; i < N; i++ {
  33. app := dummy.NewDummyApplication()
  34. cc := proxy.NewLocalClientCreator(app)
  35. evpool := newEvidencePoolWithApp(cc)
  36. reactors[i] = NewEvidencePoolReactor(config.EvidencePool, evpool) // so we dont start the consensus states
  37. reactors[i].SetLogger(logger.With("validator", i))
  38. }
  39. p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch {
  40. s.AddReactor("MEMPOOL", reactors[i])
  41. return s
  42. }, p2p.Connect2Switches)
  43. return reactors
  44. }
  45. // wait for all evidences on all reactors
  46. func waitForTxs(t *testing.T, evidences types.Txs, reactors []*EvidencePoolReactor) {
  47. // wait for the evidences in all evpools
  48. wg := new(sync.WaitGroup)
  49. for i := 0; i < len(reactors); i++ {
  50. wg.Add(1)
  51. go _waitForTxs(t, wg, evidences, i, reactors)
  52. }
  53. done := make(chan struct{})
  54. go func() {
  55. wg.Wait()
  56. close(done)
  57. }()
  58. timer := time.After(TIMEOUT)
  59. select {
  60. case <-timer:
  61. t.Fatal("Timed out waiting for evidences")
  62. case <-done:
  63. }
  64. }
  65. // wait for all evidences on a single evpool
  66. func _waitForTxs(t *testing.T, wg *sync.WaitGroup, evidences types.Txs, reactorIdx int, reactors []*EvidencePoolReactor) {
  67. evpool := reactors[reactorIdx].EvidencePool
  68. for evpool.Size() != len(evidences) {
  69. time.Sleep(time.Second)
  70. }
  71. reapedTxs := evpool.Reap(len(evidences))
  72. for i, evidence := range evidences {
  73. assert.Equal(t, evidence, reapedTxs[i], fmt.Sprintf("evidences at index %d on reactor %d don't match: %v vs %v", i, reactorIdx, evidence, reapedTxs[i]))
  74. }
  75. wg.Done()
  76. }
  77. var (
  78. NUM_TXS = 1000
  79. TIMEOUT = 120 * time.Second // ridiculously high because CircleCI is slow
  80. )
  81. func TestReactorBroadcastTxMessage(t *testing.T) {
  82. config := cfg.TestConfig()
  83. N := 4
  84. reactors := makeAndConnectEvidencePoolReactors(config, N)
  85. // send a bunch of evidences to the first reactor's evpool
  86. // and wait for them all to be received in the others
  87. evidences := checkTxs(t, reactors[0].EvidencePool, NUM_TXS)
  88. waitForTxs(t, evidences, reactors)
  89. }