mtest: refactor Run a bit
This commit is contained in:
parent
c850ace8d2
commit
9ccc787066
@ -80,10 +80,21 @@ type Checker struct {
|
||||
// State. This is called after Init and after every subsequent Action is
|
||||
// applied.
|
||||
Actions func(State) []Action
|
||||
|
||||
// MaxLength indicates the maximum number of Actions which can be strung
|
||||
// together in a single Run. Defaults to 10 if not set.
|
||||
MaxLength int
|
||||
}
|
||||
|
||||
// Run performs RunOnce in a loop until maxDuration has elapsed.
|
||||
func (c Checker) Run(maxDepth int, maxDuration time.Duration) error {
|
||||
func (c Checker) withDefaults() Checker {
|
||||
if c.MaxLength == 0 {
|
||||
c.MaxLength = 10
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// RunUntil performs Runs in a loop until maxDuration has elapsed.
|
||||
func (c Checker) RunUntil(maxDuration time.Duration) error {
|
||||
doneTimer := time.After(maxDuration)
|
||||
for {
|
||||
select {
|
||||
@ -92,18 +103,19 @@ func (c Checker) Run(maxDepth int, maxDuration time.Duration) error {
|
||||
default:
|
||||
}
|
||||
|
||||
if err := c.RunOnce(maxDepth); err != nil {
|
||||
if err := c.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RunOnce generates a single sequence of Actions and applies them in order,
|
||||
// returning nil once the number of Actions performed has reached maxDepth or a
|
||||
// Run generates a single sequence of Actions and applies them in order,
|
||||
// returning nil once the number of Actions performed has reached MaxLength or a
|
||||
// CheckErr if an error is returned.
|
||||
func (c Checker) RunOnce(maxDepth int) error {
|
||||
func (c Checker) Run() error {
|
||||
c = c.withDefaults()
|
||||
s := c.Init()
|
||||
applied := make([]Applyer, 0, maxDepth)
|
||||
applied := make([]Applyer, 0, c.MaxLength)
|
||||
for {
|
||||
actions := c.Actions(s)
|
||||
action := mrand.Element(actions, func(i int) uint64 {
|
||||
@ -124,7 +136,7 @@ func (c Checker) RunOnce(maxDepth int) error {
|
||||
}
|
||||
} else if action.Incomplete {
|
||||
continue
|
||||
} else if action.Terminate || len(applied) >= maxDepth {
|
||||
} else if action.Terminate || len(applied) >= c.MaxLength {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -30,15 +30,17 @@ func TestCheckerRun(t *T) {
|
||||
},
|
||||
}
|
||||
},
|
||||
MaxLength: 4,
|
||||
}
|
||||
|
||||
// 4 Actions should never be able to go over 5
|
||||
if err := c.Run(4, time.Second); err != nil {
|
||||
if err := c.RunUntil(time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 20 should always go over 5 eventually
|
||||
err := c.Run(20, time.Second)
|
||||
c.MaxLength = 20
|
||||
err := c.RunUntil(time.Second)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when maxDepth is 20")
|
||||
} else if len(err.(CheckerErr).Applied) < 6 {
|
||||
|
Loading…
Reference in New Issue
Block a user