github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/testutil/scaled.go (about)

     1  package testutil
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/markusbkk/elvish/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) * getTestTimeScale())
    16  }
    17  
    18  func getTestTimeScale() float64 {
    19  	env := os.Getenv(env.ELVISH_TEST_TIME_SCALE)
    20  	if env == "" {
    21  		return 1
    22  	}
    23  	scale, err := strconv.ParseFloat(env, 64)
    24  	if err != nil || scale <= 0 {
    25  		return 1
    26  	}
    27  	return scale
    28  }