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.

113 lines
2.6 KiB

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