@@ -11,17 +11,21 @@ import (
1111 "time"
1212
1313 "github.com/ava-labs/coreth/ethclient"
14+ "github.com/bradleyjkemp/cupaloy/v2"
1415 "github.com/caarlos0/env/v10"
15- "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
17+ "gorm.io/gorm"
1618)
1719
1820type testConfig struct {
19- DBHost string `env:"DB_HOST" envDefault:"localhost"`
20- DBPort int `env:"DB_PORT" envDefault:"3306"`
21- DBName string `env:"DB_NAME" envDefault:"flare_ftso_indexer_test"`
22- DBUsername string `env:"DB_USERNAME" envDefault:"root"`
23- DBPassword string `env:"DB_PASSWORD" envDefault:"root"`
24- MockChainPort int `env:"MOCK_CHAIN_PORT" envDefault:"5500"`
21+ DBHost string `env:"DB_HOST" envDefault:"localhost"`
22+ DBPort int `env:"DB_PORT" envDefault:"3306"`
23+ DBName string `env:"DB_NAME" envDefault:"flare_ftso_indexer_test"`
24+ DBUsername string `env:"DB_USERNAME" envDefault:"root"`
25+ DBPassword string `env:"DB_PASSWORD" envDefault:"root"`
26+ MockChainPort int `env:"MOCK_CHAIN_PORT" envDefault:"5500"`
27+ RecorderNodeURL string `env:"RECORDER_NODE_URL"`
28+ ResponsesFile string `env:"RESPONSES_FILE" envDefault:"../testing/chain_copy/responses.json"`
2529}
2630
2731func TestIndexer (t * testing.T ) {
@@ -33,21 +37,6 @@ func TestIndexer(t *testing.T) {
3337 t .Fatal ("Config parse error:" , err )
3438 }
3539
36- // mock blockchain
37- go func () {
38- err := indexer_testing .MockChain (
39- tCfg .MockChainPort ,
40- "../testing/chain_copy/blocks.json" ,
41- "../testing/chain_copy/transactions.json" ,
42- )
43- if err != nil {
44- logger .Fatal ("Mock chain error: %s" , err )
45- }
46- }()
47-
48- time .Sleep (3 * time .Second )
49- indexer_testing .ChainLastBlock = 2000
50-
5140 // set configuration parameters
5241 mockChainAddress := fmt .Sprintf ("http://localhost:%d" , tCfg .MockChainPort )
5342 cfgChain := config.ChainConfig {NodeURL : mockChainAddress }
@@ -98,12 +87,51 @@ func TestIndexer(t *testing.T) {
9887 cfg := config.Config {Indexer : cfgIndexer , Chain : cfgChain , Logger : cfgLog , DB : cfgDB }
9988 config .GlobalConfigCallback .Call (cfg )
10089
90+ // mock blockchain
91+ mockChain , err := indexer_testing .NewMockChain (
92+ tCfg .MockChainPort ,
93+ tCfg .ResponsesFile ,
94+ tCfg .RecorderNodeURL ,
95+ )
96+ require .NoError (t , err )
97+
10198 // connect to the database
102- db , err := database .ConnectAndInitialize (ctx , & cfgDB )
99+ db , err := database .ConnectAndInitialize (ctx , & cfg . DB )
103100 if err != nil {
104101 logger .Fatal ("Database connect and initialize error: %s" , err )
105102 }
106103
104+ err = runIndexer (ctx , mockChain , db , & cfg )
105+ require .NoError (t , err )
106+
107+ // correctness check
108+ states , err := database .UpdateDBStates (ctx , db )
109+ require .NoError (t , err )
110+
111+ // Set the update timestamps to zero for the snapshot as these will
112+ // vary with current system time.
113+ for _ , state := range states .States {
114+ state .Updated = time.Time {}
115+ }
116+
117+ cupaloy .SnapshotT (t , states )
118+ }
119+
120+ func runIndexer (ctx context.Context , mockChain * indexer_testing.MockChain , db * gorm.DB , cfg * config.Config ) error {
121+ go func () {
122+ if err := mockChain .Run (ctx ); err != nil {
123+ logger .Fatal ("Mock chain error: %s" , err )
124+ }
125+ }()
126+
127+ defer func () {
128+ if err := mockChain .Stop (); err != nil {
129+ logger .Error ("Mock chain stop error: %s" , err )
130+ }
131+ }()
132+
133+ time .Sleep (3 * time .Second )
134+
107135 // set a new starting index based on the history drop interval
108136 historyDropIntervalSeconds := uint64 (10000 )
109137
@@ -113,7 +141,7 @@ func TestIndexer(t *testing.T) {
113141 }
114142
115143 // create the indexer
116- cIndexer , err := CreateBlockIndexer (& cfg , db , ethClient )
144+ cIndexer , err := CreateBlockIndexer (cfg , db , ethClient )
117145 if err != nil {
118146 logger .Fatal ("Create indexer error: %s" , err )
119147 }
@@ -124,9 +152,6 @@ func TestIndexer(t *testing.T) {
124152 logger .Fatal ("History run error: %s" , err )
125153 }
126154
127- // at the mock server add new blocks after some time
128- go increaseLastBlockAndStop ()
129-
130155 // turn on the function to delete in the database everything that
131156 // is older than the historyDrop interval
132157 go database .DropHistory (
@@ -139,19 +164,5 @@ func TestIndexer(t *testing.T) {
139164 logger .Fatal ("Continuous run error: %s" , err )
140165 }
141166
142- // correctness check
143- states , err := database .UpdateDBStates (ctx , db )
144- assert .NoError (t , err )
145- assert .Equal (t , 1112 , int (states .States [database .FirstDatabaseIndexState ].Index ))
146- assert .Equal (t , 2400 , int (states .States [database .LastDatabaseIndexState ].Index ))
147- assert .Equal (t , 8980059 , int (states .States [database .LastChainIndexState ].Index ))
148- }
149-
150- func increaseLastBlockAndStop () {
151- indexer_testing .ChainLastBlock = 2100
152- time .Sleep (time .Second )
153- indexer_testing .ChainLastBlock = 2200
154- time .Sleep (time .Second )
155- indexer_testing .ChainLastBlock = 2499
156- time .Sleep (10 * time .Second )
167+ return nil
157168}
0 commit comments