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