github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3aggregator/config/aggregator_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package config 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/require" 28 yaml "gopkg.in/yaml.v2" 29 ) 30 31 func TestJitterBuckets(t *testing.T) { 32 config := ` 33 - flushInterval: 1m 34 maxJitterPercent: 1.0 35 - flushInterval: 10m 36 maxJitterPercent: 0.5 37 - flushInterval: 1h 38 maxJitterPercent: 0.25` 39 40 var buckets jitterBuckets 41 require.NoError(t, yaml.Unmarshal([]byte(config), &buckets)) 42 43 maxJitterFn, err := buckets.NewMaxJitterFn() 44 require.NoError(t, err) 45 46 inputs := []struct { 47 interval time.Duration 48 expectedMaxJitter time.Duration 49 }{ 50 {interval: time.Second, expectedMaxJitter: time.Second}, 51 {interval: 10 * time.Second, expectedMaxJitter: 10 * time.Second}, 52 {interval: time.Minute, expectedMaxJitter: time.Minute}, 53 {interval: 10 * time.Minute, expectedMaxJitter: 5 * time.Minute}, 54 {interval: time.Hour, expectedMaxJitter: 15 * time.Minute}, 55 {interval: 6 * time.Hour, expectedMaxJitter: 90 * time.Minute}, 56 } 57 for _, input := range inputs { 58 require.Equal(t, input.expectedMaxJitter, maxJitterFn(input.interval)) 59 } 60 } 61 62 func TestMaxAllowedForwardingDelayFnJitterEnabled(t *testing.T) { 63 maxJitterFn := func(resolution time.Duration) time.Duration { 64 if resolution <= time.Second { 65 return time.Second 66 } 67 if resolution <= time.Minute { 68 return time.Duration(0.5 * float64(resolution)) 69 } 70 return time.Duration(0.25 * float64(resolution)) 71 } 72 c := forwardingConfiguration{MaxSingleDelay: 5 * time.Second} 73 fn := c.MaxAllowedForwardingDelayFn(true, maxJitterFn) 74 75 inputs := []struct { 76 resolution time.Duration 77 numForwardedTimes int 78 expected time.Duration 79 }{ 80 {resolution: time.Second, numForwardedTimes: 1, expected: 6 * time.Second}, 81 {resolution: time.Second, numForwardedTimes: 3, expected: 16 * time.Second}, 82 {resolution: 10 * time.Second, numForwardedTimes: 1, expected: 10 * time.Second}, 83 {resolution: 10 * time.Second, numForwardedTimes: 3, expected: 20 * time.Second}, 84 {resolution: 10 * time.Minute, numForwardedTimes: 1, expected: 155 * time.Second}, 85 {resolution: 10 * time.Minute, numForwardedTimes: 3, expected: 165 * time.Second}, 86 } 87 for _, input := range inputs { 88 require.Equal(t, input.expected, fn(input.resolution, input.numForwardedTimes)) 89 } 90 } 91 92 func TestMaxAllowedForwardingDelayFnJitterDisabled(t *testing.T) { 93 maxJitterFn := func(resolution time.Duration) time.Duration { 94 if resolution <= time.Second { 95 return time.Second 96 } 97 if resolution <= time.Minute { 98 return time.Duration(0.5 * float64(resolution)) 99 } 100 return time.Duration(0.25 * float64(resolution)) 101 } 102 c := forwardingConfiguration{MaxSingleDelay: 5 * time.Second} 103 fn := c.MaxAllowedForwardingDelayFn(false, maxJitterFn) 104 105 inputs := []struct { 106 resolution time.Duration 107 numForwardedTimes int 108 expected time.Duration 109 }{ 110 {resolution: time.Second, numForwardedTimes: 1, expected: 6 * time.Second}, 111 {resolution: time.Second, numForwardedTimes: 3, expected: 16 * time.Second}, 112 {resolution: 10 * time.Second, numForwardedTimes: 1, expected: 15 * time.Second}, 113 {resolution: 10 * time.Second, numForwardedTimes: 3, expected: 25 * time.Second}, 114 {resolution: 10 * time.Minute, numForwardedTimes: 1, expected: 605 * time.Second}, 115 {resolution: 10 * time.Minute, numForwardedTimes: 3, expected: 615 * time.Second}, 116 } 117 for _, input := range inputs { 118 require.Equal(t, input.expected, fn(input.resolution, input.numForwardedTimes)) 119 } 120 }