From a802d137cdb74d36a895f2c60333965c25ab2749 Mon Sep 17 00:00:00 2001 From: Arik Kfir Date: Mon, 17 Jun 2024 16:18:21 +0300 Subject: [PATCH] feat: support slowing down tests to enable debugging consequences This change introduces a supported environment variable called `JUSTEST_SLOW_FACTOR` which, when set to an integer value, will cause Justest to multiple durations passed to the `For(...)` and `Within(...)` methods with its value. This will cause these duration to be longer, thus allowing the developer to investigate or debug side consequences of such tests - like logging into clusters and checking the effects of end-to-end tests, inspecting file-systems, etc. --- asserter.go | 6 ++++++ util.go | 21 +++++++++++++++++++++ util_test.go | 14 ++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 util.go create mode 100644 util_test.go 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() +}