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.

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