diff --git a/mtest/checker.go b/mtest/checker.go index 9a75b76..5fe672a 100644 --- a/mtest/checker.go +++ b/mtest/checker.go @@ -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 } } diff --git a/mtest/checker_test.go b/mtest/checker_test.go index e50b9fd..cbb5532 100644 --- a/mtest/checker_test.go +++ b/mtest/checker_test.go @@ -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 {