Skip to content

Commit

Permalink
Refactor runtime/failpoint.go
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Wang <[email protected]>
Co-authored-by: Marek Siarkowicz <[email protected]>
Signed-off-by: Chun-Hung Tseng <[email protected]>
  • Loading branch information
3 people committed May 13, 2024
1 parent c591bde commit 5f5a127
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
34 changes: 32 additions & 2 deletions runtime/failpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
)

type Failpoint struct {
t *terms

t *terms
failpointMu sync.RWMutex
}

Expand Down Expand Up @@ -54,3 +53,34 @@ func (fp *Failpoint) Acquire() (interface{}, error) {
func (fp *Failpoint) BadType(v interface{}, t string) {
fmt.Printf("failpoint: %q got value %v of type \"%T\" but expected type %q\n", fp.t.fpath, v, v, t)
}

func (fp *Failpoint) SetTerm(t *terms) {
fp.failpointMu.Lock()
defer fp.failpointMu.Unlock()

fp.t = t
}

func (fp *Failpoint) ClearTerm() error {
fp.failpointMu.Lock()
defer fp.failpointMu.Unlock()

if fp.t == nil {
return ErrDisabled
}
fp.t = nil

return nil
}

func (fp *Failpoint) Status() (string, int, error) {
fp.failpointMu.RLock()
defer fp.failpointMu.RUnlock()

t := fp.t
if t == nil {
return "", 0, ErrDisabled
}

return t.desc, t.counter, nil
}
25 changes: 3 additions & 22 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ func Enable(name, inTerms string) error {
return err
}

fp.failpointMu.Lock()
defer fp.failpointMu.Unlock()

fp.t = t
fp.SetTerm(t)

return nil
}
Expand All @@ -102,15 +99,7 @@ func Disable(name string) error {
return ErrNoExist
}

fp.failpointMu.Lock()
defer fp.failpointMu.Unlock()

if fp.t == nil {
return ErrDisabled
}
fp.t = nil

return nil
return fp.ClearTerm()
}

// Status gives the current setting and execution count for the failpoint
Expand All @@ -122,15 +111,7 @@ func Status(failpath string) (string, int, error) {
return "", 0, ErrNoExist
}

fp.failpointMu.RLock()
defer fp.failpointMu.RUnlock()

t := fp.t
if t == nil {
return "", 0, ErrDisabled
}

return t.desc, t.counter, nil
return fp.Status()
}

func List() []string {
Expand Down

0 comments on commit 5f5a127

Please sign in to comment.