pkg/failpoint: add DelegatedEval API

Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
Wei Fu 2022-06-19 19:59:06 +08:00
parent b297775eaf
commit 1ae6e8b076

View File

@ -25,6 +25,9 @@ import (
"time" "time"
) )
// EvalFn is the func type about delegated evaluation.
type EvalFn func() error
// Type is the type of failpoint to specifies which action to take. // Type is the type of failpoint to specifies which action to take.
type Type int type Type int
@ -100,6 +103,12 @@ func NewFailpoint(fnName string, terms string) (*Failpoint, error) {
// Evaluate evaluates a failpoint. // Evaluate evaluates a failpoint.
func (fp *Failpoint) Evaluate() error { func (fp *Failpoint) Evaluate() error {
fn := fp.DelegatedEval()
return fn()
}
// DelegatedEval evaluates a failpoint but delegates to caller to fire that.
func (fp *Failpoint) DelegatedEval() EvalFn {
var target *failpointEntry var target *failpointEntry
func() { func() {
@ -118,9 +127,9 @@ func (fp *Failpoint) Evaluate() error {
}() }()
if target == nil { if target == nil {
return nil return nopEvalFn
} }
return target.evaluate() return target.evaluate
} }
// Failpoint returns the current state of control in string format. // Failpoint returns the current state of control in string format.
@ -291,3 +300,7 @@ func parseInt64(term []byte, terminate byte, val *int64) ([]byte, error) {
*val = v *val = v
return term[i+1:], nil return term[i+1:], nil
} }
func nopEvalFn() error {
return nil
}