|
| 1 | +package ingest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stellar/go-stellar-sdk/ingest/ledgerbackend" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/stellar/wallet-backend/internal/indexer" |
| 15 | +) |
| 16 | + |
| 17 | +// TestStreamingLoadtestBackendRealCorpus replays real `stellar-core apply-load` |
| 18 | +// meta through the backend and the production transaction reader. It proves, |
| 19 | +// against real core output rather than hand-built fixtures, that renumbered |
| 20 | +// and merged ledgers still parse through the exact code path live ingestion |
| 21 | +// uses (transaction-set-to-result pairing included). |
| 22 | +// |
| 23 | +// Opt-in: set STREAMING_LOADTEST_CORPUS to a comma-separated list of meta.xdr |
| 24 | +// files (regular files work; EOF exercises the reopen path by replaying the |
| 25 | +// file as a new stream epoch). Generate them by running |
| 26 | +// `stellar-core apply-load` (BUILD_TESTS image) with METADATA_OUTPUT_STREAM |
| 27 | +// pointed at a file, one run per transaction profile. |
| 28 | +func TestStreamingLoadtestBackendRealCorpus(t *testing.T) { |
| 29 | + corpus := os.Getenv("STREAMING_LOADTEST_CORPUS") |
| 30 | + if corpus == "" { |
| 31 | + t.Skip("set STREAMING_LOADTEST_CORPUS=<meta.xdr>[,<meta.xdr>...] to run") |
| 32 | + } |
| 33 | + paths := strings.Split(corpus, ",") |
| 34 | + |
| 35 | + // apply-load hard-overrides its network passphrase to this value. |
| 36 | + const passphrase = "Apply Load" |
| 37 | + // Enough ledgers to cross at least one EOF/reopen boundary per file with |
| 38 | + // the reference smoke corpora (50 benchmark ledgers plus setup each). |
| 39 | + const ledgersToRead = 200 |
| 40 | + |
| 41 | + backend, err := NewStreamingLoadtestLedgerBackend(StreamingLoadtestBackendConfig{ |
| 42 | + MetaPipePaths: paths, |
| 43 | + }) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) |
| 47 | + defer cancel() |
| 48 | + require.NoError(t, backend.PrepareRange(ctx, ledgerbackend.UnboundedRange(1))) |
| 49 | + defer func() { |
| 50 | + cancel() |
| 51 | + require.NoError(t, backend.Close()) |
| 52 | + }() |
| 53 | + |
| 54 | + var lastCloseTime int64 |
| 55 | + totalTxs := 0 |
| 56 | + for seq := uint32(1); seq <= ledgersToRead; seq++ { |
| 57 | + lcm, err := backend.GetLedger(ctx, seq) |
| 58 | + require.NoError(t, err, "ledger %d", seq) |
| 59 | + |
| 60 | + require.Equal(t, seq, lcm.LedgerSequence()) |
| 61 | + ct := lcm.LedgerCloseTime() |
| 62 | + require.Positive(t, ct, "ledger %d close time", seq) |
| 63 | + require.GreaterOrEqual(t, ct, lastCloseTime, "ledger %d close time regressed", seq) |
| 64 | + lastCloseTime = ct |
| 65 | + |
| 66 | + // The production read path: this is what live ingestion runs on every |
| 67 | + // ledger, so a merged ledger it cannot parse would fail here first. |
| 68 | + txs, err := indexer.GetLedgerTransactions(ctx, passphrase, lcm) |
| 69 | + require.NoError(t, err, "reading transactions of ledger %d", seq) |
| 70 | + totalTxs += len(txs) |
| 71 | + } |
| 72 | + |
| 73 | + assert.Positive(t, totalTxs) |
| 74 | + t.Logf("read %d ledgers, %d transactions total from %d file(s)", ledgersToRead, totalTxs, len(paths)) |
| 75 | +} |
0 commit comments