github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/util/backoffutils/backoff_test.go (about) 1 // Copyright 2016 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package backoffutils_test 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/hxx258456/ccgo/go-grpc-middleware/util/backoffutils" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // scale duration by a factor 15 func scaleDuration(d time.Duration, factor float64) time.Duration { 16 return time.Duration(float64(d) * factor) 17 } 18 19 func TestJitterUp(t *testing.T) { 20 21 // arguments to jitterup 22 duration := 10 * time.Second 23 variance := 0.10 24 25 // bound to check 26 max := 11000 * time.Millisecond 27 min := 9000 * time.Millisecond 28 high := scaleDuration(max, 0.98) 29 low := scaleDuration(min, 1.02) 30 31 highCount := 0 32 lowCount := 0 33 34 for i := 0; i < 1000; i++ { 35 out := backoffutils.JitterUp(duration, variance) 36 assert.True(t, out <= max, "value %s must be <= %s", out, max) 37 assert.True(t, out >= min, "value %s must be >= %s", out, min) 38 39 if out > high { 40 highCount++ 41 } 42 if out < low { 43 lowCount++ 44 } 45 } 46 47 assert.True(t, highCount != 0, "at least one sample should reach to >%s", high) 48 assert.True(t, lowCount != 0, "at least one sample should to <%s", low) 49 50 }