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.

118 lines
2.6 KiB

  1. package mempool
  2. import (
  3. "encoding/binary"
  4. "sync"
  5. "testing"
  6. "github.com/tendermint/tendermint/proxy"
  7. "github.com/tendermint/tendermint/types"
  8. "github.com/tendermint/tmsp/example/golang"
  9. tmsp "github.com/tendermint/tmsp/types"
  10. )
  11. func TestSerialReap(t *testing.T) {
  12. app := example.NewCounterApplication(true)
  13. app.SetOption("serial", "on")
  14. mtx := new(sync.Mutex)
  15. appConnMem := proxy.NewLocalAppConn(mtx, app)
  16. appConnCon := proxy.NewLocalAppConn(mtx, app)
  17. mempool := NewMempool(appConnMem)
  18. appendTxsRange := func(start, end int) {
  19. // Append some txs.
  20. for i := start; i < end; i++ {
  21. // This will succeed
  22. txBytes := make([]byte, 32)
  23. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  24. err := mempool.CheckTx(txBytes)
  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)
  32. if err != nil {
  33. t.Fatal("Error after CheckTx: %v", err)
  34. }
  35. }
  36. }
  37. reapCheck := func(exp int) {
  38. txs, err := mempool.Reap()
  39. if err != nil {
  40. t.Error("Error in mempool.Reap()", err)
  41. }
  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, 32)
  50. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  51. txs = append(txs, txBytes)
  52. }
  53. err := mempool.Update(0, txs)
  54. if err != nil {
  55. t.Error("Error in mempool.Update()", err)
  56. }
  57. }
  58. commitRange := func(start, end int) {
  59. // Append some txs.
  60. for i := start; i < end; i++ {
  61. txBytes := make([]byte, 32)
  62. binary.LittleEndian.PutUint64(txBytes, uint64(i))
  63. _, retCode := appConnCon.AppendTx(txBytes)
  64. if retCode != tmsp.RetCodeOK {
  65. t.Error("Error committing tx", retCode)
  66. }
  67. }
  68. _, retCode := appConnCon.GetHash()
  69. if retCode != tmsp.RetCodeOK {
  70. t.Error("Error committing range", retCode)
  71. }
  72. }
  73. //----------------------------------------
  74. // Append some txs.
  75. appendTxsRange(0, 100)
  76. // Reap the txs.
  77. reapCheck(100)
  78. // Reap again. We should get the same amount
  79. reapCheck(100)
  80. // Append 0 to 999, we should reap 900 new txs
  81. // because 100 were already counted.
  82. appendTxsRange(0, 1000)
  83. // Reap the txs.
  84. reapCheck(1000)
  85. // Reap again. We should get the same amount
  86. reapCheck(1000)
  87. // Commit from the conensus AppConn
  88. commitRange(0, 500)
  89. updateRange(0, 500)
  90. // We should have 500 left.
  91. reapCheck(500)
  92. // Append 100 invalid txs and 100 valid txs
  93. appendTxsRange(900, 1100)
  94. // We should have 600 now.
  95. reapCheck(600)
  96. }