Skip to content

Commit

Permalink
test(backup): WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Sep 9, 2024
1 parent e2c5dcb commit 920e99d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 78 deletions.
172 changes: 102 additions & 70 deletions backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"errors"
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -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)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Test / test-with-race

undefined: exampleTxs

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

undefined: exampleTxs (typecheck)

Check failure on line 188 in backup/backup_test.go

View workflow job for this annotation

GitHub Actions / Go Test / test

undefined: exampleTxs

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)
}
}

Expand All @@ -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
},
}
)
Expand Down Expand Up @@ -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++
}
Expand Down
20 changes: 12 additions & 8 deletions backup/mock_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 920e99d

Please sign in to comment.