diff --git a/asserter.go b/asserter.go index c9cc2ea..fbcff26 100644 --- a/asserter.go +++ b/asserter.go @@ -10,6 +10,8 @@ import ( "github.com/arikkfir/justest/internal" ) +const SlowFactorEnvVarName = "JUSTEST_SLOW_FACTOR" + //go:noinline func With(t T) VerifierAndEnsurer { if t == nil { @@ -109,6 +111,8 @@ func (a *assertion) OrFail() { //go:noinline func (a *assertion) For(duration time.Duration, interval time.Duration) { GetHelper(a.t).Helper() + duration = transformDurationIfNecessary(a.t, duration) + if a.evaluated { panic("assertion already evaluated") } else { @@ -194,6 +198,8 @@ func (a *assertion) For(duration time.Duration, interval time.Duration) { //go:noinline func (a *assertion) Within(duration time.Duration, interval time.Duration) { GetHelper(a.t).Helper() + duration = transformDurationIfNecessary(a.t, duration) + if a.evaluated { panic("assertion already evaluated") } else { diff --git a/util.go b/util.go new file mode 100644 index 0000000..1ede2fd --- /dev/null +++ b/util.go @@ -0,0 +1,21 @@ +package justest + +import ( + "os" + "strconv" + "time" +) + +func transformDurationIfNecessary(t T, d time.Duration) time.Duration { + if v, found := os.LookupEnv(SlowFactorEnvVarName); found { + if factor, err := strconv.ParseInt(v, 0, 0); err != nil { + t.Logf("Ignoring value of '%s' environment variable: %+v", SlowFactorEnvVarName, err) + return d + } else { + oldSeconds := int64(d.Seconds()) + newSeconds := oldSeconds * factor + return time.Duration(newSeconds) * time.Second + } + } + return d +} diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..2cd47ff --- /dev/null +++ b/util_test.go @@ -0,0 +1,14 @@ +package justest + +import ( + "testing" + "time" +) + +func TestTransformDurationIfNecessary(t *testing.T) { + With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(5 * time.Second)).OrFail() + t.Setenv(SlowFactorEnvVarName, "2") + With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(10 * time.Second)).OrFail() + t.Setenv(SlowFactorEnvVarName, "3") + With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(15 * time.Second)).OrFail() +}