github.com/outbrain/consul@v1.4.5/lib/cluster_test.go (about) 1 package lib 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestDurationMinusBuffer(t *testing.T) { 9 tests := []struct { 10 Duration time.Duration 11 Buffer time.Duration 12 Jitter int64 13 }{ 14 { 15 Duration: 1 * time.Minute, 16 Buffer: 10 * time.Second, 17 Jitter: 16, 18 }, 19 { 20 Duration: 1 * time.Second, 21 Buffer: 500 * time.Millisecond, 22 Jitter: 4, 23 }, 24 { 25 Duration: 1 * time.Second, 26 Buffer: 1 * time.Second, 27 Jitter: 4, 28 }, 29 { 30 Duration: 1 * time.Second, 31 Buffer: 1 * time.Second, 32 Jitter: 0, 33 }, 34 { 35 Duration: 1 * time.Second, 36 Buffer: 1 * time.Second, 37 Jitter: 1, 38 }, 39 } 40 41 for _, test := range tests { 42 min, max := DurationMinusBufferDomain(test.Duration, test.Buffer, test.Jitter) 43 for i := 0; i < 10; i++ { 44 d := DurationMinusBuffer(test.Duration, test.Buffer, test.Jitter) 45 if d < min || d > max { 46 t.Fatalf("Bad: %v", d) 47 } 48 } 49 } 50 } 51 52 func TestDurationMinusBufferDomain(t *testing.T) { 53 tests := []struct { 54 Duration time.Duration 55 Buffer time.Duration 56 Jitter int64 57 Min time.Duration 58 Max time.Duration 59 }{ 60 { 61 Duration: 60 * time.Second, 62 Buffer: 10 * time.Second, 63 Jitter: 16, 64 Min: 46*time.Second + 875*time.Millisecond, 65 Max: 50 * time.Second, 66 }, 67 { 68 Duration: 60 * time.Second, 69 Buffer: 0 * time.Second, 70 Jitter: 16, 71 Min: 56*time.Second + 250*time.Millisecond, 72 Max: 60 * time.Second, 73 }, 74 { 75 Duration: 60 * time.Second, 76 Buffer: 0 * time.Second, 77 Jitter: 0, 78 Min: 60 * time.Second, 79 Max: 60 * time.Second, 80 }, 81 { 82 Duration: 60 * time.Second, 83 Buffer: 0 * time.Second, 84 Jitter: 1, 85 Min: 0 * time.Second, 86 Max: 60 * time.Second, 87 }, 88 { 89 Duration: 60 * time.Second, 90 Buffer: 0 * time.Second, 91 Jitter: 2, 92 Min: 30 * time.Second, 93 Max: 60 * time.Second, 94 }, 95 { 96 Duration: 60 * time.Second, 97 Buffer: 0 * time.Second, 98 Jitter: 4, 99 Min: 45 * time.Second, 100 Max: 60 * time.Second, 101 }, 102 { 103 Duration: 0 * time.Second, 104 Buffer: 0 * time.Second, 105 Jitter: 0, 106 Min: 0 * time.Second, 107 Max: 0 * time.Second, 108 }, 109 { 110 Duration: 60 * time.Second, 111 Buffer: 120 * time.Second, 112 Jitter: 8, 113 Min: -1 * (52*time.Second + 500*time.Millisecond), 114 Max: -1 * 60 * time.Second, 115 }, 116 } 117 118 for _, test := range tests { 119 min, max := DurationMinusBufferDomain(test.Duration, test.Buffer, test.Jitter) 120 if min != test.Min { 121 t.Errorf("Bad min: %v != %v", min, test.Min) 122 } 123 124 if max != test.Max { 125 t.Errorf("Bad max: %v != %v", max, test.Max) 126 } 127 } 128 } 129 130 func TestRandomStagger(t *testing.T) { 131 intv := time.Minute 132 for i := 0; i < 10; i++ { 133 stagger := RandomStagger(intv) 134 if stagger < 0 || stagger >= intv { 135 t.Fatalf("Bad: %v", stagger) 136 } 137 } 138 } 139 140 func TestRateScaledInterval(t *testing.T) { 141 const min = 1 * time.Second 142 rate := 200.0 143 if v := RateScaledInterval(rate, min, 0); v != min { 144 t.Fatalf("Bad: %v", v) 145 } 146 if v := RateScaledInterval(rate, min, 100); v != min { 147 t.Fatalf("Bad: %v", v) 148 } 149 if v := RateScaledInterval(rate, min, 200); v != 1*time.Second { 150 t.Fatalf("Bad: %v", v) 151 } 152 if v := RateScaledInterval(rate, min, 1000); v != 5*time.Second { 153 t.Fatalf("Bad: %v", v) 154 } 155 if v := RateScaledInterval(rate, min, 5000); v != 25*time.Second { 156 t.Fatalf("Bad: %v", v) 157 } 158 if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second { 159 t.Fatalf("Bad: %v", v) 160 } 161 halfMin := minRate / 2.0 162 if v := RateScaledInterval(halfMin, min, 100); v != min { 163 t.Fatalf("Bad: %v", v) 164 } 165 if v := RateScaledInterval(0, min, 10000); v != min { 166 t.Fatalf("Bad: %v", v) 167 } 168 if v := RateScaledInterval(0.0, min, 10000); v != min { 169 t.Fatalf("Bad: %v", v) 170 } 171 if v := RateScaledInterval(-1, min, 10000); v != min { 172 t.Fatalf("Bad: %v", v) 173 } 174 }