github.com/blend/go-sdk@v1.20220411.3/grpcutil/backoff_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package grpcutil 9 10 import ( 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/blend/go-sdk/assert" 16 ) 17 18 // scale duration by a factor 19 func scaleDuration(d time.Duration, factor float64) time.Duration { 20 return time.Duration(float64(d) * factor) 21 } 22 23 func TestJitterUp(t *testing.T) { 24 assert := assert.New(t) 25 26 // arguments to jitterup 27 duration := 10 * time.Second 28 variance := 0.10 29 30 // bound to check 31 max := 11000 * time.Millisecond 32 min := 9000 * time.Millisecond 33 high := scaleDuration(max, 0.98) 34 low := scaleDuration(min, 1.02) 35 36 highCount := 0 37 lowCount := 0 38 39 for i := 0; i < 1000; i++ { 40 out := JitterUp(duration, variance) 41 assert.True(out <= max, fmt.Sprintf("value %s must be <= %s", out, max)) 42 assert.True(out >= min, fmt.Sprintf("value %s must be >= %s", out, min)) 43 44 if out > high { 45 highCount++ 46 } 47 if out < low { 48 lowCount++ 49 } 50 } 51 52 assert.True(highCount != 0, fmt.Sprintf("at least one sample should reach to > %s", high)) 53 assert.True(lowCount != 0, fmt.Sprintf("at least one sample should to < %s", low)) 54 }