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.

115 lines
2.7 KiB

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. appConnCon, _ := cc.NewABCIClient()
  18. mempool := NewMempool(config.Mempool, appConnMem)
  19. mempool.SetLogger(log.TestingLogger())
  20. deliverTxsRange := func(start, end int) {
  21. // Deliver some txs.
  22. for i := start; i < end; i++ {
  23. // This will succeed
  24. txBytes := make([]byte, 8)
  25. binary.BigEndian.PutUint64(txBytes, uint64(i))
  26. err := mempool.CheckTx(txBytes, nil)
  27. if err != nil {
  28. t.Fatal("Error after CheckTx: %v", err)
  29. }
  30. // This will fail because not serial (incrementing)
  31. // However, error should still be nil.
  32. // It just won't show up on Reap().
  33. err = mempool.CheckTx(txBytes, nil)
  34. if err != nil {
  35. t.Fatal("Error after CheckTx: %v", err)
  36. }
  37. }
  38. }
  39. reapCheck := func(exp int) {
  40. txs := mempool.Reap(-1)
  41. if len(txs) != exp {
  42. t.Fatalf("Expected to reap %v txs but got %v", exp, len(txs))
  43. }
  44. }
  45. updateRange := func(start, end int) {
  46. txs := make([]types.Tx, 0)
  47. for i := start; i < end; i++ {
  48. txBytes := make([]byte, 8)
  49. binary.BigEndian.PutUint64(txBytes, uint64(i))
  50. txs = append(txs, txBytes)
  51. }
  52. mempool.Update(0, txs)
  53. }
  54. commitRange := func(start, end int) {
  55. // Deliver some txs.
  56. for i := start; i < end; i++ {
  57. txBytes := make([]byte, 8)
  58. binary.BigEndian.PutUint64(txBytes, uint64(i))
  59. res := appConnCon.DeliverTxSync(txBytes)
  60. if !res.IsOK() {
  61. t.Errorf("Error committing tx. Code:%v result:%X log:%v",
  62. res.Code, res.Data, res.Log)
  63. }
  64. }
  65. res := appConnCon.CommitSync()
  66. if len(res.Data) != 8 {
  67. t.Errorf("Error committing. Hash:%X log:%v", res.Data, res.Log)
  68. }
  69. }
  70. //----------------------------------------
  71. // Deliver some txs.
  72. deliverTxsRange(0, 100)
  73. // Reap the txs.
  74. reapCheck(100)
  75. // Reap again. We should get the same amount
  76. reapCheck(100)
  77. // Deliver 0 to 999, we should reap 900 new txs
  78. // because 100 were already counted.
  79. deliverTxsRange(0, 1000)
  80. // Reap the txs.
  81. reapCheck(1000)
  82. // Reap again. We should get the same amount
  83. reapCheck(1000)
  84. // Commit from the conensus AppConn
  85. commitRange(0, 500)
  86. updateRange(0, 500)
  87. // We should have 500 left.
  88. reapCheck(500)
  89. // Deliver 100 invalid txs and 100 valid txs
  90. deliverTxsRange(900, 1100)
  91. // We should have 600 now.
  92. reapCheck(600)
  93. }