github.com/imran-kn/cilium-fork@v1.6.9/pkg/backoff/backoff_test.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package backoff
    18  
    19  import (
    20  	"fmt"
    21  	"math"
    22  	"testing"
    23  	"time"
    24  
    25  	"gopkg.in/check.v1"
    26  )
    27  
    28  func Test(t *testing.T) {
    29  	check.TestingT(t)
    30  }
    31  
    32  type BackoffSuite struct{}
    33  
    34  var _ = check.Suite(&BackoffSuite{})
    35  
    36  func (b *BackoffSuite) TestJitter(c *check.C) {
    37  	var prev time.Duration
    38  	for i := 0; i < 100; i++ {
    39  		current := CalculateDuration(time.Second, time.Minute, 2.0, true, 1)
    40  		c.Assert(current, check.Not(check.Equals), prev)
    41  		prev = current
    42  	}
    43  }
    44  
    45  type fakeNodeManager struct {
    46  	nodes *int
    47  }
    48  
    49  func (f *fakeNodeManager) ClusterSizeDependantInterval(baseInterval time.Duration) time.Duration {
    50  	numNodes := *f.nodes
    51  
    52  	if numNodes == 0 {
    53  		return baseInterval
    54  	}
    55  
    56  	waitNanoseconds := float64(baseInterval.Nanoseconds()) * math.Log1p(float64(numNodes))
    57  	return time.Duration(int64(waitNanoseconds))
    58  }
    59  
    60  func (b *BackoffSuite) TestClusterSizeDependantInterval(c *check.C) {
    61  	var (
    62  		nnodes      = 0
    63  		nodeManager = fakeNodeManager{
    64  			nodes: &nnodes,
    65  		}
    66  	)
    67  
    68  	nodeBackoff := &Exponential{
    69  		Min:         time.Second,
    70  		Max:         2 * time.Minute,
    71  		NodeManager: &nodeManager,
    72  		Jitter:      true,
    73  		Factor:      2.0,
    74  	}
    75  
    76  	fmt.Printf("nodes      4        16       128       512      1024      2048\n")
    77  	for attempt := 1; attempt <= 8; attempt++ {
    78  		fmt.Printf("%d:", attempt)
    79  		for _, n := range []int{4, 16, 128, 512, 1024, 2048} {
    80  			nnodes = n
    81  			fmt.Printf("%10s", nodeBackoff.Duration(attempt).Round(time.Second/10))
    82  		}
    83  		fmt.Printf("\n")
    84  	}
    85  }
    86  
    87  func (b *BackoffSuite) TestJitterDistribution(c *check.C) {
    88  	nodeBackoff := &Exponential{
    89  		Min:    time.Second,
    90  		Factor: 2.0,
    91  	}
    92  
    93  	for attempt := 1; attempt <= 8; attempt++ {
    94  		current := nodeBackoff.Duration(attempt).Round(time.Second / 10)
    95  		fmt.Printf("%d: %s\n", attempt, current)
    96  	}
    97  }