From 920e99dcd249149ea8a5bb4685bb1d051e15928c Mon Sep 17 00:00:00 2001 From: aeddi Date: Mon, 9 Sep 2024 15:19:50 +0200 Subject: [PATCH] test(backup): WIP --- backup/backup_test.go | 172 +++++++++++++++++++++++++----------------- backup/mock_test.go | 20 +++-- 2 files changed, 114 insertions(+), 78 deletions(-) diff --git a/backup/backup_test.go b/backup/backup_test.go index 46ab293..7606293 100644 --- a/backup/backup_test.go +++ b/backup/backup_test.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "errors" + "fmt" "os" "testing" "time" @@ -81,84 +82,123 @@ func TestBackup_DetermineRightBound(t *testing.T) { }) } +func generateBlocksTransactions(t *testing.T, nBlocks, nTxs uint64) [][]*types.TxData { + t.Helper() + + gen := make([][]*types.TxData, nBlocks) + + for i := range gen { + txs := make([]*types.TxData, nTxs) + for j := range txs { + txs[j] = &types.TxData{ + BlockNum: uint64(i), + Tx: std.Tx{ + Memo: fmt.Sprintf("example transaction %d:%d", i, j), + }, + } + } + gen[i] = txs + } + + return gen +} + func TestBackup_ExecuteBackup_FixedRange(t *testing.T) { t.Parallel() - var ( - tempFile = createTempFile(t) + type testCase struct { + name string + from uint64 + to uint64 + nBlocks uint64 + nTxs uint64 + } - fromBlock uint64 = 10 - toBlock = fromBlock + 10 + testCases := []testCase{ + {name: "toto"}, + } - exampleTx = std.Tx{ - Memo: "example transaction", - } + testFunc := func(t *testing.T, tCase testCase) { + t.Run(tCase.name, func(t *testing.T) { + t.Parallel() - cfg = DefaultConfig() + var ( + tempFile = createTempFile(t) - mockClient = &mockClient{ - getLatestBlockNumberFn: func() (uint64, error) { - return toBlock, nil - }, - getBlockTransactionsFn: func(blockNum uint64) ([]std.Tx, error) { - // Sanity check - if blockNum < fromBlock && blockNum > toBlock { - t.Fatal("invalid block number requested") + fromBlock uint64 = tCase.from + toBlock = tCase.to + + cfg = DefaultConfig() + + mockClient = &mockClient{ + getLatestBlockNumberFn: func() (uint64, error) { + return toBlock, nil + }, + getBlocksTransactionsFn: func(ctx context.Context, start, stop uint64) ([][]*types.TxData, error) { + // Sanity check + if start > stop { + t.Fatal("invalid block number requested") + } + + return generateBlocksTransactions(t, tCase.nBlocks, tCase.nTxs), nil + }, } + ) - return []std.Tx{exampleTx}, nil // 1 tx per block - }, - } - ) + // Temp file cleanup + t.Cleanup(func() { + require.NoError(t, tempFile.Close()) + require.NoError(t, os.Remove(tempFile.Name())) + }) - // Temp file cleanup - t.Cleanup(func() { - require.NoError(t, tempFile.Close()) - require.NoError(t, os.Remove(tempFile.Name())) - }) + // Set the config + cfg.FromBlock = fromBlock + cfg.ToBlock = &toBlock - // Set the config - cfg.FromBlock = fromBlock - cfg.ToBlock = &toBlock + s := NewService(mockClient, standard.NewWriter(tempFile), WithLogger(noop.New())) - s := NewService(mockClient, standard.NewWriter(tempFile), WithLogger(noop.New())) + // Run the backup procedure + require.NoError( + t, + s.ExecuteBackup( + context.Background(), + cfg, + ), + ) - // Run the backup procedure - require.NoError( - t, - s.ExecuteBackup( - context.Background(), - cfg, - ), - ) + // Read the output file + fileRaw, err := os.Open(tempFile.Name()) + require.NoError(t, err) - // Read the output file - fileRaw, err := os.Open(tempFile.Name()) - require.NoError(t, err) + // Set up a line-by-line scanner + scanner := bufio.NewScanner(fileRaw) - // Set up a line-by-line scanner - scanner := bufio.NewScanner(fileRaw) + expectedBlock := fromBlock - expectedBlock := fromBlock + // Iterate over each line in the file + for scanner.Scan() { + var txData types.TxData - // Iterate over each line in the file - for scanner.Scan() { - var txData types.TxData + // Unmarshal the JSON data into the Person struct + if err := amino.UnmarshalJSON(scanner.Bytes(), &txData); err != nil { + t.Fatalf("unable to unmarshal JSON line, %v", err) + } - // Unmarshal the JSON data into the Person struct - if err := amino.UnmarshalJSON(scanner.Bytes(), &txData); err != nil { - t.Fatalf("unable to unmarshal JSON line, %v", err) - } + assert.Equal(t, expectedBlock, txData.BlockNum) + assert.Equal(t, exampleTxs, txData.Tx) - assert.Equal(t, expectedBlock, txData.BlockNum) - assert.Equal(t, exampleTx, txData.Tx) + expectedBlock++ + } - expectedBlock++ + // Check for errors during scanning + if err := scanner.Err(); err != nil { + t.Fatalf("error encountered during scan, %v", err) + } + }) } - // Check for errors during scanning - if err := scanner.Err(); err != nil { - t.Fatalf("error encountered during scan, %v", err) + for _, tCase := range testCases { + testFunc(t, tCase) } } @@ -177,28 +217,20 @@ func TestBackup_ExecuteBackup_Watch(t *testing.T) { requestToBlock = toBlock / 2 - exampleTx = std.Tx{ - Memo: "example transaction", - } - - cfg = DefaultConfig() + exampleTxs = generateBlocksTransactions(t, 10, 2) + cfg = DefaultConfig() mockClient = &mockClient{ getLatestBlockNumberFn: func() (uint64, error) { return toBlock, nil }, - getBlockTransactionsFn: func(blockNum uint64) ([]std.Tx, error) { + getBlocksTransactionsFn: func(ctx context.Context, start, stop uint64) ([][]*types.TxData, error) { // Sanity check - if blockNum < fromBlock && blockNum > toBlock { + if start > stop { t.Fatal("invalid block number requested") } - if blockNum == toBlock { - // End of the road, close the watch process - cancelFn() - } - - return []std.Tx{exampleTx}, nil // 1 tx per block + return exampleTxs, nil // 1 tx per block }, } ) @@ -245,7 +277,7 @@ func TestBackup_ExecuteBackup_Watch(t *testing.T) { } assert.Equal(t, expectedBlock, txData.BlockNum) - assert.Equal(t, exampleTx, txData.Tx) + assert.Equal(t, exampleTxs, txData.Tx) expectedBlock++ } diff --git a/backup/mock_test.go b/backup/mock_test.go index 25fda87..a289d38 100644 --- a/backup/mock_test.go +++ b/backup/mock_test.go @@ -1,15 +1,19 @@ package backup -import "github.com/gnolang/gno/tm2/pkg/std" +import ( + "context" + + "github.com/gnolang/tx-archive/types" +) type ( - getLatestBlockNumberDelegate func() (uint64, error) - getBlockTransactionsDelegate func(uint64) ([]std.Tx, error) + getLatestBlockNumberDelegate func() (uint64, error) + getBlocksTransactionsDelegate func(context.Context, uint64, uint64) ([][]*types.TxData, error) ) type mockClient struct { - getLatestBlockNumberFn getLatestBlockNumberDelegate - getBlockTransactionsFn getBlockTransactionsDelegate + getLatestBlockNumberFn getLatestBlockNumberDelegate + getBlocksTransactionsFn getBlocksTransactionsDelegate } func (m *mockClient) GetLatestBlockNumber() (uint64, error) { @@ -20,9 +24,9 @@ func (m *mockClient) GetLatestBlockNumber() (uint64, error) { return 0, nil } -func (m *mockClient) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) { - if m.getBlockTransactionsFn != nil { - return m.getBlockTransactionsFn(blockNum) +func (m *mockClient) GetBlocksTransactions(ctx context.Context, fromBlock, toBlock uint64) ([][]*types.TxData, error) { + if m.getBlocksTransactionsFn != nil { + return m.getBlocksTransactionsFn(ctx, fromBlock, toBlock) } return nil, nil