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.

119 lines
2.7 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, 8)
  23. binary.BigEndian.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, 8)
  50. binary.BigEndian.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, 8)
  62. binary.BigEndian.PutUint64(txBytes, uint64(i))
  63. code, result, logStr := appConnCon.AppendTx(txBytes)
  64. if code != tmsp.RetCodeOK {
  65. t.Errorf("Error committing tx. Code:%v result:%X log:%v",
  66. code, result, logStr)
  67. }
  68. }
  69. hash, log := appConnCon.GetHash()
  70. if len(hash) != 8 {
  71. t.Errorf("Error getting hash. Hash:%X log:%v", hash, log)
  72. }
  73. }
  74. //----------------------------------------
  75. // Append some txs.
  76. appendTxsRange(0, 100)
  77. // Reap the txs.
  78. reapCheck(100)
  79. // Reap again. We should get the same amount
  80. reapCheck(100)
  81. // Append 0 to 999, we should reap 900 new txs
  82. // because 100 were already counted.
  83. appendTxsRange(0, 1000)
  84. // Reap the txs.
  85. reapCheck(1000)
  86. // Reap again. We should get the same amount
  87. reapCheck(1000)
  88. // Commit from the conensus AppConn
  89. commitRange(0, 500)
  90. updateRange(0, 500)
  91. // We should have 500 left.
  92. reapCheck(500)
  93. // Append 100 invalid txs and 100 valid txs
  94. appendTxsRange(900, 1100)
  95. // We should have 600 now.
  96. reapCheck(600)
  97. }