...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils/backoff_test.go

Documentation: github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils

     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/grpc-ecosystem/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  	// arguments to jitterup
    21  	duration := 10 * time.Second
    22  	variance := 0.10
    23  
    24  	// bound to check
    25  	max := 11000 * time.Millisecond
    26  	min := 9000 * time.Millisecond
    27  	high := scaleDuration(max, 0.98)
    28  	low := scaleDuration(min, 1.02)
    29  
    30  	highCount := 0
    31  	lowCount := 0
    32  
    33  	for i := 0; i < 1000; i++ {
    34  		out := backoffutils.JitterUp(duration, variance)
    35  		assert.True(t, out <= max, "value %s must be <= %s", out, max)
    36  		assert.True(t, out >= min, "value %s must be >= %s", out, min)
    37  
    38  		if out > high {
    39  			highCount++
    40  		}
    41  		if out < low {
    42  			lowCount++
    43  		}
    44  	}
    45  
    46  	assert.True(t, highCount != 0, "at least one sample should reach to >%s", high)
    47  	assert.True(t, lowCount != 0, "at least one sample should to <%s", low)
    48  }
    49  

View as plain text