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.

117 lines
2.6 KiB

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