Skip to content

Commit d2a271c

Browse files
authored
feat: support slowing down tests to enable debugging consequences (#21)
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.
1 parent ac8fdf1 commit d2a271c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

asserter.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/arikkfir/justest/internal"
1111
)
1212

13+
const SlowFactorEnvVarName = "JUSTEST_SLOW_FACTOR"
14+
1315
//go:noinline
1416
func With(t T) VerifierAndEnsurer {
1517
if t == nil {
@@ -109,6 +111,8 @@ func (a *assertion) OrFail() {
109111
//go:noinline
110112
func (a *assertion) For(duration time.Duration, interval time.Duration) {
111113
GetHelper(a.t).Helper()
114+
duration = transformDurationIfNecessary(a.t, duration)
115+
112116
if a.evaluated {
113117
panic("assertion already evaluated")
114118
} else {
@@ -194,6 +198,8 @@ func (a *assertion) For(duration time.Duration, interval time.Duration) {
194198
//go:noinline
195199
func (a *assertion) Within(duration time.Duration, interval time.Duration) {
196200
GetHelper(a.t).Helper()
201+
duration = transformDurationIfNecessary(a.t, duration)
202+
197203
if a.evaluated {
198204
panic("assertion already evaluated")
199205
} else {

util.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package justest
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"time"
7+
)
8+
9+
func transformDurationIfNecessary(t T, d time.Duration) time.Duration {
10+
if v, found := os.LookupEnv(SlowFactorEnvVarName); found {
11+
if factor, err := strconv.ParseInt(v, 0, 0); err != nil {
12+
t.Logf("Ignoring value of '%s' environment variable: %+v", SlowFactorEnvVarName, err)
13+
return d
14+
} else {
15+
oldSeconds := int64(d.Seconds())
16+
newSeconds := oldSeconds * factor
17+
return time.Duration(newSeconds) * time.Second
18+
}
19+
}
20+
return d
21+
}

util_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package justest
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestTransformDurationIfNecessary(t *testing.T) {
9+
With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(5 * time.Second)).OrFail()
10+
t.Setenv(SlowFactorEnvVarName, "2")
11+
With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(10 * time.Second)).OrFail()
12+
t.Setenv(SlowFactorEnvVarName, "3")
13+
With(t).Verify(transformDurationIfNecessary(t, 5*time.Second)).Will(EqualTo(15 * time.Second)).OrFail()
14+
}

0 commit comments

Comments
 (0)