src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/testutil/scaled.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"time"
     7  
     8  	"src.elv.sh/pkg/env"
     9  )
    10  
    11  // Scaled returns d scaled by $E:ELVISH_TEST_TIME_SCALE. If the environment
    12  // variable does not exist or contains an invalid value, the scale defaults to
    13  // 1.
    14  func Scaled(d time.Duration) time.Duration {
    15  	return time.Duration(float64(d) * TestTimeScale())
    16  }
    17  
    18  // TestTimeScale parses $E:ELVISH_TEST_TIME_SCALE, defaulting to 1 it it's not
    19  // set or can't be parsed as a float64.
    20  func TestTimeScale() float64 {
    21  	env := os.Getenv(env.ELVISH_TEST_TIME_SCALE)
    22  	if env == "" {
    23  		return 1
    24  	}
    25  	scale, err := strconv.ParseFloat(env, 64)
    26  	if err != nil || scale <= 0 {
    27  		return 1
    28  	}
    29  	return scale
    30  }