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.

77 lines
2.2 KiB

  1. package coordinator
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/hermeznetwork/hermez-node/batchbuilder"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. "github.com/hermeznetwork/hermez-node/db/l2db"
  10. "github.com/hermeznetwork/hermez-node/db/statedb"
  11. "github.com/hermeznetwork/hermez-node/eth"
  12. "github.com/hermeznetwork/hermez-node/log"
  13. "github.com/hermeznetwork/hermez-node/txselector"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBuilder) { // FUTURE once Synchronizer is ready, should return it also
  18. nLevels := 32
  19. synchDB, err := ioutil.TempDir("", "tmpSynchDB")
  20. require.Nil(t, err)
  21. sdb, err := statedb.NewStateDB(synchDB, true, nLevels)
  22. assert.Nil(t, err)
  23. pass := os.Getenv("POSTGRES_PASS")
  24. l2DB, err := l2db.NewL2DB(5432, "localhost", "hermez", pass, "l2", 10, 512, 24*time.Hour)
  25. require.Nil(t, err)
  26. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  27. require.Nil(t, err)
  28. txsel, err := txselector.NewTxSelector(txselDir, sdb, l2DB, 10, 10, 10)
  29. assert.Nil(t, err)
  30. bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB")
  31. require.Nil(t, err)
  32. bb, err := batchbuilder.NewBatchBuilder(bbDir, sdb, nil, 0, uint64(nLevels))
  33. assert.Nil(t, err)
  34. // l1Txs, coordinatorL1Txs, poolL2Txs := test.GenerateTestTxsFromSet(t, test.SetTest0)
  35. return txsel, bb
  36. }
  37. func TestCoordinator(t *testing.T) {
  38. txsel, bb := newTestModules(t)
  39. conf := Config{
  40. LoopInterval: 100 * time.Millisecond,
  41. }
  42. hdb := &historydb.HistoryDB{}
  43. c := NewCoordinator(conf, hdb, txsel, bb, &eth.Client{})
  44. c.Start()
  45. time.Sleep(1 * time.Second)
  46. // simulate forgeSequence time
  47. log.Debug("simulate entering in forge time")
  48. c.isForgeSeq = true
  49. time.Sleep(1 * time.Second)
  50. // simulate going out from forgeSequence
  51. log.Debug("simulate going out from forge time")
  52. c.isForgeSeq = false
  53. time.Sleep(1 * time.Second)
  54. // simulate entering forgeSequence time again
  55. log.Debug("simulate entering in forge time again")
  56. c.isForgeSeq = true
  57. time.Sleep(1 * time.Second)
  58. // simulate stopping forgerLoop by channel
  59. log.Debug("simulate stopping forgerLoop by closing coordinator stopch")
  60. c.Stop()
  61. time.Sleep(1 * time.Second)
  62. }