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