pkg/failpoint: add DelegatedEval API
Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
		| @@ -25,6 +25,9 @@ import ( | ||||
| 	"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 Type int | ||||
|  | ||||
| @@ -100,6 +103,12 @@ func NewFailpoint(fnName string, terms string) (*Failpoint, error) { | ||||
|  | ||||
| // Evaluate evaluates a failpoint. | ||||
| 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 | ||||
|  | ||||
| 	func() { | ||||
| @@ -118,9 +127,9 @@ func (fp *Failpoint) Evaluate() error { | ||||
| 	}() | ||||
|  | ||||
| 	if target == nil { | ||||
| 		return nil | ||||
| 		return nopEvalFn | ||||
| 	} | ||||
| 	return target.evaluate() | ||||
| 	return target.evaluate | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| 	return term[i+1:], nil | ||||
| } | ||||
|  | ||||
| func nopEvalFn() error { | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Wei Fu
					Wei Fu