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.

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