src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/testutil/scaled_test.go (about) 1 package testutil 2 3 import ( 4 "os" 5 "testing" 6 "time" 7 8 "src.elv.sh/pkg/env" 9 ) 10 11 var scaledMsTests = []struct { 12 name string 13 env string 14 d time.Duration 15 16 want time.Duration 17 }{ 18 {"default 10ms", "", 10 * time.Millisecond, 10 * time.Millisecond}, 19 20 {"2x 10ms", "2", 10 * time.Millisecond, 20 * time.Millisecond}, 21 {"2x 3s", "2", 3 * time.Second, 6 * time.Second}, 22 {"0.5x 10ms", "0.5", 10 * time.Millisecond, 5 * time.Millisecond}, 23 24 {"invalid treated as 1", "a", 10 * time.Millisecond, 10 * time.Millisecond}, 25 {"0 treated as 1", "0", 10 * time.Millisecond, 10 * time.Millisecond}, 26 {"negative treated as 1", "-1", 10 * time.Millisecond, 10 * time.Millisecond}, 27 } 28 29 func TestScaled(t *testing.T) { 30 envSave := os.Getenv(env.ELVISH_TEST_TIME_SCALE) 31 defer os.Setenv(env.ELVISH_TEST_TIME_SCALE, envSave) 32 33 for _, test := range scaledMsTests { 34 t.Run(test.name, func(t *testing.T) { 35 os.Setenv(env.ELVISH_TEST_TIME_SCALE, test.env) 36 got := Scaled(test.d) 37 if got != test.want { 38 t.Errorf("got %v, want %v", got, test.want) 39 } 40 }) 41 } 42 }