From cf6160b3f2f4b0b3435a3d5609bcd93ddabe2e15 Mon Sep 17 00:00:00 2001 From: Matthew Rothenberg Date: Tue, 25 Apr 2017 12:33:20 -0400 Subject: [PATCH] fix deterministic scenario ordering with feature concurrency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while global rand is concurrent safe, since each suite is handling picking scenario order on it’s own, if running features under concurrency we cannot guarantee the order Features will execute under, therefore we have each suite initialize its own randomness Source with the initial seed, such that their random order selection within a Feature should always be deterministic when given a specific seed. --- run.go | 4 ---- suite.go | 3 ++- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/run.go b/run.go index bae334a..7d2351c 100644 --- a/run.go +++ b/run.go @@ -118,10 +118,6 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt r := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) seed = r.Int63n(99998) + 1 } - if seed != 0 { - // init global rand module (concurrent safe) with our seed - rand.Seed(seed) - } r := runner{ fmt: formatter(suite, output), diff --git a/suite.go b/suite.go index e386f75..d6c1631 100644 --- a/suite.go +++ b/suite.go @@ -337,7 +337,8 @@ func (s *Suite) runFeature(f *feature) { // then shuffle it if we are randomizing scenarios scenarios := make([]interface{}, len(f.ScenarioDefinitions)) if s.randomSeed != 0 { - perm := rand.Perm(len(f.ScenarioDefinitions)) + r := rand.New(rand.NewSource(s.randomSeed)) + perm := r.Perm(len(f.ScenarioDefinitions)) for i, v := range perm { scenarios[v] = f.ScenarioDefinitions[i] }