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.

123 lines
3.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
  1. package mempool
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. "github.com/tendermint/abci/example/counter"
  6. cfg "github.com/tendermint/tendermint/config"
  7. "github.com/tendermint/tendermint/proxy"
  8. "github.com/tendermint/tendermint/types"
  9. "github.com/tendermint/tmlibs/log"
  10. )
  11. func TestSerialReap(t *testing.T) {
  12. config := cfg.ResetTestRoot("mempool_test")
  13. app := counter.NewCounterApplication(true)
  14. app.SetOption("serial", "on")
  15. cc := proxy.NewLocalClientCreator(app)
  16. appConnMem, _ := cc.NewABCIClient()
  17. appConnMem.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "mempool"))
  18. if _, err := appConnMem.Start(); err != nil {
  19. t.Fatalf("Error starting ABCI client: %v", err.Error())
  20. }
  21. appConnCon, _ := cc.NewABCIClient()
  22. appConnCon.SetLogger(log.TestingLogger().With("module", "abci-client", "connection", "consensus"))
  23. if _, err := appConnCon.Start(); err != nil {
  24. t.Fatalf("Error starting ABCI client: %v", err.Error())
  25. }
  26. mempool := NewMempool(config.Mempool, appConnMem)
  27. mempool.SetLogger(log.TestingLogger())
  28. deliverTxsRange := func(start, end int) {
  29. // Deliver some txs.
  30. for i := start; i < end; i++ {
  31. // This will succeed
  32. txBytes := make([]byte, 8)
  33. binary.BigEndian.PutUint64(txBytes, uint64(i))
  34. err := mempool.CheckTx(txBytes, nil)
  35. if err != nil {
  36. t.Fatal("Error after CheckTx: %v", err)
  37. }
  38. // This will fail because not serial (incrementing)
  39. // However, error should still be nil.
  40. // It just won't show up on Reap().
  41. err = mempool.CheckTx(txBytes, nil)
  42. if err != nil {
  43. t.Fatal("Error after CheckTx: %v", err)
  44. }
  45. }
  46. }
  47. reapCheck := func(exp int) {
  48. txs := mempool.Reap(-1)
  49. if len(txs) != exp {
  50. t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
  51. }
  52. }
  53. updateRange := func(start, end int) {
  54. txs := make([]types.Tx, 0)
  55. for i := start; i < end; i++ {
  56. txBytes := make([]byte, 8)
  57. binary.BigEndian.PutUint64(txBytes, uint64(i))
  58. txs = append(txs, txBytes)
  59. }
  60. mempool.Update(0, txs)
  61. }
  62. commitRange := func(start, end int) {
  63. // Deliver some txs.
  64. for i := start; i < end; i++ {
  65. txBytes := make([]byte, 8)
  66. binary.BigEndian.PutUint64(txBytes, uint64(i))
  67. res := appConnCon.DeliverTxSync(txBytes)
  68. if !res.IsOK() {
  69. t.Errorf("Error committing tx. Code:%v result:%X log:%v",
  70. res.Code, res.Data, res.Log)
  71. }
  72. }
  73. res := appConnCon.CommitSync()
  74. if len(res.Data) != 8 {
  75. t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
  76. }
  77. }
  78. //----------------------------------------
  79. // Deliver some txs.
  80. deliverTxsRange(0, 100)
  81. // Reap the txs.
  82. reapCheck(100)
  83. // Reap again. We should get the same amount
  84. reapCheck(100)
  85. // Deliver 0 to 999, we should reap 900 new txs
  86. // because 100 were already counted.
  87. deliverTxsRange(0, 1000)
  88. // Reap the txs.
  89. reapCheck(1000)
  90. // Reap again. We should get the same amount
  91. reapCheck(1000)
  92. // Commit from the conensus AppConn
  93. commitRange(0, 500)
  94. updateRange(0, 500)
  95. // We should have 500 left.
  96. reapCheck(500)
  97. // Deliver 100 invalid txs and 100 valid txs
  98. deliverTxsRange(900, 1100)
  99. // We should have 600 now.
  100. reapCheck(600)
  101. }