github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/validation/limits_test.go (about) 1 package validation 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 // nolint:goconst 9 func TestSmallestPositiveIntPerTenant(t *testing.T) { 10 type args struct { 11 tenantIDs []string 12 f func(string) int 13 } 14 tests := []struct { 15 name string 16 args args 17 want int 18 }{ 19 { 20 name: "smallest positive int per tenant", 21 args: args{ 22 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 23 f: func(tenantID string) int { 24 // For ease of testing just pick three unique values representing per-tenant limits 25 return len(tenantID) 26 }, 27 }, 28 want: 7, // The smallest of the three 29 }, 30 } 31 for _, tt := range tests { 32 t.Run(tt.name, func(t *testing.T) { 33 if got := SmallestPositiveIntPerTenant(tt.args.tenantIDs, tt.args.f); got != tt.want { 34 t.Errorf("SmallestPositiveIntPerTenant() = %v, want %v", got, tt.want) 35 } 36 }) 37 } 38 } 39 40 // nolint:goconst 41 func TestSmallestPositiveNonZeroIntPerTenant(t *testing.T) { 42 type args struct { 43 tenantIDs []string 44 f func(string) int 45 } 46 tests := []struct { 47 name string 48 args args 49 want int 50 }{ 51 { 52 name: "smallest positive non-zero int per tenant", 53 args: args{ 54 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 55 f: func(tenantID string) int { 56 // For ease of testing just pick three unique values representing per-tenant limits 57 switch tenantID { 58 case "tenant1": 59 return 1 60 case "tenantTwo": 61 return 0 62 case "tenantThree": 63 return 2 64 default: 65 return len(tenantID) 66 } 67 }, 68 }, 69 want: 1, // The smallest non-zero of the three 70 }, 71 } 72 for _, tt := range tests { 73 t.Run(tt.name, func(t *testing.T) { 74 if got := SmallestPositiveNonZeroIntPerTenant(tt.args.tenantIDs, tt.args.f); got != tt.want { 75 t.Errorf("SmallestPositiveNonZeroIntPerTenant() = %v, want %v", got, tt.want) 76 } 77 }) 78 } 79 } 80 81 // nolint:goconst 82 func TestSmallestPositiveNonZeroDurationPerTenant(t *testing.T) { 83 type args struct { 84 tenantIDs []string 85 f func(string) time.Duration 86 } 87 tests := []struct { 88 name string 89 args args 90 want time.Duration 91 }{ 92 { 93 name: "smallest positive non-zero duration per tenant", 94 args: args{ 95 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 96 f: func(tenantID string) time.Duration { 97 // For ease of testing just pick three unique values representing per-tenant limits 98 switch tenantID { 99 case "tenant1": 100 return time.Second 101 case "tenantTwo": 102 return 0 103 case "tenantThree": 104 return 2 * time.Second 105 default: 106 return 3 * time.Second 107 } 108 }, 109 }, 110 want: time.Second, // The smallest non-zero of the three 111 }, 112 } 113 for _, tt := range tests { 114 t.Run(tt.name, func(t *testing.T) { 115 if got := SmallestPositiveNonZeroDurationPerTenant(tt.args.tenantIDs, tt.args.f); got != tt.want { 116 t.Errorf("SmallestPositiveNonZeroDurationPerTenant() = %v, want %v", got, tt.want) 117 } 118 }) 119 } 120 } 121 122 // nolint:goconst 123 func TestMaxDurationPerTenant(t *testing.T) { 124 type args struct { 125 tenantIDs []string 126 f func(string) time.Duration 127 } 128 tests := []struct { 129 name string 130 args args 131 want time.Duration 132 }{ 133 { 134 name: "max duration per tenant", 135 args: args{ 136 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 137 f: func(tenantID string) time.Duration { 138 // For ease of testing just pick three unique values representing per-tenant limits 139 switch tenantID { 140 case "tenant1": 141 return time.Second 142 case "tenantTwo": 143 return 2 * time.Second 144 case "tenantThree": 145 return 3 * time.Second 146 default: 147 return time.Second 148 } 149 }, 150 }, 151 want: 3 * time.Second, // The largest of the three 152 }, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 if got := MaxDurationPerTenant(tt.args.tenantIDs, tt.args.f); got != tt.want { 157 t.Errorf("MaxDurationPerTenant() = %v, want %v", got, tt.want) 158 } 159 }) 160 } 161 } 162 163 // nolint:goconst 164 func TestMaxDurationOrZeroPerTenant(t *testing.T) { 165 type args struct { 166 tenantIDs []string 167 f func(string) time.Duration 168 } 169 tests := []struct { 170 name string 171 args args 172 want time.Duration 173 }{ 174 { 175 name: "max duration or zero per tenant with no zero values", 176 args: args{ 177 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 178 f: func(tenantID string) time.Duration { 179 // For ease of testing just pick three unique values representing per-tenant limits 180 switch tenantID { 181 case "tenant1": 182 return time.Second 183 case "tenantTwo": 184 return 2 * time.Second 185 case "tenantThree": 186 return 3 * time.Second 187 default: 188 return time.Second 189 } 190 }, 191 }, 192 want: 3 * time.Second, // The largest of the three with no zero values present 193 }, 194 { 195 name: "max duration or zero per tenant with zero values", 196 args: args{ 197 tenantIDs: []string{"tenant1", "tenantTwo", "tenantThree"}, 198 f: func(tenantID string) time.Duration { 199 // For ease of testing just pick three unique values representing per-tenant limits 200 switch tenantID { 201 case "tenant1": 202 return time.Second 203 case "tenantTwo": 204 return time.Duration(0) 205 case "tenantThree": 206 return 3 * time.Second 207 default: 208 return time.Second 209 } 210 }, 211 }, 212 want: time.Duration(0), // Return zero when a single tenant has a zero value 213 }, 214 } 215 for _, tt := range tests { 216 t.Run(tt.name, func(t *testing.T) { 217 if got := MaxDurationOrZeroPerTenant(tt.args.tenantIDs, tt.args.f); got != tt.want { 218 t.Errorf("MaxDurationOrZeroPerTenant() = %v, want %v", got, tt.want) 219 } 220 }) 221 } 222 }