Skip to content

Commit c55bec1

Browse files
committed
fix: defer emitRunResult so results are written even on test failure
Move emitRunResult into a deferred closure in all three test functions. If the test fails after metrics are collected, the structured JSON is still written. If it fails before result data exists, the defer is a no-op.
1 parent a941a30 commit c55bec1

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

test/e2e/benchmark/gasburner_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ func (s *SpamoorSuite) TestGasBurner() {
2323

2424
cfg.log(t)
2525

26+
var result *benchmarkResult
27+
var wallClock time.Duration
28+
var spamoorStats *runSpamoorStats
29+
defer func() {
30+
if result != nil {
31+
emitRunResult(t, cfg, result, wallClock, spamoorStats)
32+
}
33+
}()
34+
2635
e := s.setupEnv(cfg)
2736
api := e.spamoorAPI
2837

@@ -83,7 +92,7 @@ func (s *SpamoorSuite) TestGasBurner() {
8392
if err := waitForDrain(drainCtx, t.Logf, e.ethClient, 10); err != nil {
8493
t.Logf("warning: %v", err)
8594
}
86-
wallClock := time.Since(loadStart)
95+
wallClock = time.Since(loadStart)
8796

8897
endHeader, err := e.ethClient.HeaderByNumber(ctx, nil)
8998
s.Require().NoError(err, "failed to get end block header")
@@ -96,7 +105,7 @@ func (s *SpamoorSuite) TestGasBurner() {
96105

97106
traces := s.collectTraces(e, cfg.ServiceName)
98107

99-
result := newBenchmarkResult("GasBurner", bm, traces)
108+
result = newBenchmarkResult("GasBurner", bm, traces)
100109
s.Require().Greater(result.summary.SteadyState, time.Duration(0), "expected non-zero steady-state duration")
101110
result.log(t, wallClock)
102111
w.addEntries(result.entries())
@@ -105,6 +114,5 @@ func (s *SpamoorSuite) TestGasBurner() {
105114
s.Require().NoError(mErr, "failed to get final metrics")
106115
sent := sumCounter(metrics["spamoor_transactions_sent_total"])
107116
failed := sumCounter(metrics["spamoor_transactions_failed_total"])
108-
109-
emitRunResult(t, cfg, result, wallClock, &runSpamoorStats{Sent: sent, Failed: failed})
117+
spamoorStats = &runSpamoorStats{Sent: sent, Failed: failed}
110118
}

test/e2e/benchmark/spamoor_defi_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ func (s *SpamoorSuite) TestDeFiSimulation() {
2929
w := newResultWriter(t, "DeFiSimulation")
3030
defer w.flush()
3131

32+
var result *benchmarkResult
33+
var wallClock time.Duration
34+
var spamoorStats *runSpamoorStats
35+
defer func() {
36+
if result != nil {
37+
emitRunResult(t, cfg, result, wallClock, spamoorStats)
38+
}
39+
}()
40+
3241
e := s.setupEnv(cfg)
3342

3443
uniswapConfig := map[string]any{
@@ -93,7 +102,7 @@ func (s *SpamoorSuite) TestDeFiSimulation() {
93102
if err := waitForDrain(drainCtx, t.Logf, e.ethClient, 10); err != nil {
94103
t.Logf("warning: %v", err)
95104
}
96-
wallClock := time.Since(loadStart)
105+
wallClock = time.Since(loadStart)
97106

98107
endHeader, err := e.ethClient.HeaderByNumber(ctx, nil)
99108
s.Require().NoError(err, "failed to get end block header")
@@ -106,7 +115,7 @@ func (s *SpamoorSuite) TestDeFiSimulation() {
106115

107116
traces := s.collectTraces(e, cfg.ServiceName)
108117

109-
result := newBenchmarkResult("DeFiSimulation", bm, traces)
118+
result = newBenchmarkResult("DeFiSimulation", bm, traces)
110119
s.Require().Greater(result.summary.SteadyState, time.Duration(0), "expected non-zero steady-state duration")
111120
result.log(t, wallClock)
112121
w.addEntries(result.entries())
@@ -115,6 +124,5 @@ func (s *SpamoorSuite) TestDeFiSimulation() {
115124
s.Require().NoError(mErr, "failed to get final metrics")
116125
sent := sumCounter(metrics["spamoor_transactions_sent_total"])
117126
failed := sumCounter(metrics["spamoor_transactions_failed_total"])
118-
119-
emitRunResult(t, cfg, result, wallClock, &runSpamoorStats{Sent: sent, Failed: failed})
127+
spamoorStats = &runSpamoorStats{Sent: sent, Failed: failed}
120128
}

test/e2e/benchmark/spamoor_erc20_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ func (s *SpamoorSuite) TestERC20Throughput() {
2323
w := newResultWriter(t, "ERC20Throughput")
2424
defer w.flush()
2525

26+
var result *benchmarkResult
27+
var wallClock time.Duration
28+
var spamoorStats *runSpamoorStats
29+
defer func() {
30+
if result != nil {
31+
emitRunResult(t, cfg, result, wallClock, spamoorStats)
32+
}
33+
}()
34+
2635
e := s.setupEnv(cfg)
2736

2837
erc20Config := map[string]any{
@@ -68,7 +77,7 @@ func (s *SpamoorSuite) TestERC20Throughput() {
6877
if err := waitForDrain(drainCtx, t.Logf, e.ethClient, 10); err != nil {
6978
t.Logf("warning: %v", err)
7079
}
71-
wallClock := time.Since(loadStart)
80+
wallClock = time.Since(loadStart)
7281

7382
endHeader, err := e.ethClient.HeaderByNumber(ctx, nil)
7483
s.Require().NoError(err, "failed to get end block header")
@@ -80,13 +89,13 @@ func (s *SpamoorSuite) TestERC20Throughput() {
8089

8190
traces := s.collectTraces(e, cfg.ServiceName)
8291

83-
result := newBenchmarkResult("ERC20Throughput", bm, traces)
92+
result = newBenchmarkResult("ERC20Throughput", bm, traces)
8493
s.Require().Greater(result.summary.SteadyState, time.Duration(0), "expected non-zero steady-state duration")
8594
result.log(t, wallClock)
8695
w.addEntries(result.entries())
8796

97+
spamoorStats = &runSpamoorStats{Sent: sent, Failed: failed}
98+
8899
s.Require().Greater(sent, float64(0), "at least one transaction should have been sent")
89100
s.Require().Zero(failed, "no transactions should have failed")
90-
91-
emitRunResult(t, cfg, result, wallClock, &runSpamoorStats{Sent: sent, Failed: failed})
92101
}

0 commit comments

Comments
 (0)