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.

127 lines
2.9 KiB

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