Skip to content

Commit 37c9612

Browse files
committed
Implement panic through mode
1 parent 499c886 commit 37c9612

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

benchmark.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Benchmark struct {
2727
loadSteps []BenchmarkStepFunc
2828
validationSteps []BenchmarkStepFunc
2929

30+
panicRecover bool
3031
prepareTimeout time.Duration
3132
loadTimeout time.Duration
3233
ignoreCodes []failure.Code
@@ -39,6 +40,7 @@ func NewBenchmark(opts ...BenchmarkOption) (*Benchmark, error) {
3940
prepareSteps: []BenchmarkStepFunc{},
4041
loadSteps: []BenchmarkStepFunc{},
4142
validationSteps: []BenchmarkStepFunc{},
43+
panicRecover: true,
4244
prepareTimeout: time.Duration(0),
4345
loadTimeout: time.Duration(0),
4446
ignoreCodes: []failure.Code{},
@@ -93,7 +95,7 @@ func (b *Benchmark) Start(parent context.Context) *BenchmarkResult {
9395
}
9496
defer prepareCancel()
9597

96-
if err := panicWrapper(func() error { return prepare(prepareCtx, step) }); err != nil {
98+
if err := panicWrapper(b.panicRecover, func() error { return prepare(prepareCtx, step) }); err != nil {
9799
for _, ignore := range b.ignoreCodes {
98100
if failure.IsCode(err, ignore) {
99101
goto Result
@@ -121,7 +123,7 @@ func (b *Benchmark) Start(parent context.Context) *BenchmarkResult {
121123
for _, load := range b.loadSteps {
122124
func(f BenchmarkStepFunc) {
123125
loadParallel.Do(func(c context.Context) {
124-
if err := panicWrapper(func() error { return f(c, step) }); err != nil {
126+
if err := panicWrapper(b.panicRecover, func() error { return f(c, step) }); err != nil {
125127
for _, ignore := range b.ignoreCodes {
126128
if failure.IsCode(err, ignore) {
127129
return
@@ -143,7 +145,7 @@ func (b *Benchmark) Start(parent context.Context) *BenchmarkResult {
143145

144146
step.setErrorCode(ErrValidation)
145147
for _, validation := range b.validationSteps {
146-
if err := panicWrapper(func() error { return validation(ctx, step) }); err != nil {
148+
if err := panicWrapper(b.panicRecover, func() error { return validation(ctx, step) }); err != nil {
147149
for _, ignore := range b.ignoreCodes {
148150
if failure.IsCode(err, ignore) {
149151
goto Result
@@ -197,7 +199,11 @@ func (b *Benchmark) IgnoreErrorCode(code failure.Code) {
197199
b.ignoreCodes = append(b.ignoreCodes, code)
198200
}
199201

200-
func panicWrapper(f func() error) (err error) {
202+
func panicWrapper(on bool, f func() error) (err error) {
203+
if !on {
204+
return f()
205+
}
206+
201207
defer func() {
202208
re := recover()
203209
if re == nil {

option.go benchmark_option.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ func WithLoadTimeout(d time.Duration) BenchmarkOption {
2323
return nil
2424
}
2525
}
26+
27+
func WithoutPanicRecover() BenchmarkOption {
28+
return func(b *Benchmark) error {
29+
b.panicRecover = false
30+
return nil
31+
}
32+
}

benchmark_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,28 @@ func TestBenchmarkValidationIgnoredError(t *testing.T) {
367367
t.Fatal(result.Errors.All())
368368
}
369369
}
370+
371+
func TestBenchmarWithoutPanicRecover(t *testing.T) {
372+
ctx := context.TODO()
373+
b := newBenchmark(WithoutPanicRecover())
374+
375+
panicErr := errors.New("panic")
376+
b.Validation(func(_ context.Context, _ *BenchmarkStep) error {
377+
panic(panicErr)
378+
})
379+
380+
func() {
381+
defer func() {
382+
err := recover()
383+
if err == nil {
384+
t.Fatalf("not thrown panic")
385+
}
386+
387+
if err != panicErr {
388+
t.Fatalf("invalid panic: %+v", err)
389+
}
390+
}()
391+
392+
b.Start(ctx)
393+
}()
394+
}

0 commit comments

Comments
 (0)