github.com/thanos-io/thanos@v0.32.5/pkg/receive/request_limiter_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package receive 5 6 import ( 7 "testing" 8 9 "github.com/efficientgo/core/testutil" 10 ) 11 12 func TestRequestLimiter_limitsFor(t *testing.T) { 13 tenantWithLimits := "limited-tenant" 14 tenantWithoutLimits := "unlimited-tenant" 15 16 limits := WriteLimitsConfig{ 17 DefaultLimits: DefaultLimitsConfig{ 18 RequestLimits: *NewEmptyRequestLimitsConfig(). 19 SetSeriesLimit(10), 20 }, 21 TenantsLimits: TenantsWriteLimitsConfig{ 22 tenantWithLimits: &WriteLimitConfig{ 23 RequestLimits: NewEmptyRequestLimitsConfig(). 24 SetSeriesLimit(30), 25 }, 26 }, 27 } 28 tests := []struct { 29 name string 30 tenant string 31 wantLimits *requestLimitsConfig 32 }{ 33 { 34 name: "Gets the default limits when tenant's limits aren't present", 35 tenant: tenantWithoutLimits, 36 wantLimits: NewEmptyRequestLimitsConfig(). 37 SetSeriesLimit(10). 38 SetSamplesLimit(0). 39 SetSizeBytesLimit(0), 40 }, 41 { 42 name: "Gets the tenant's limits when it is present", 43 tenant: tenantWithLimits, 44 wantLimits: NewEmptyRequestLimitsConfig(). 45 SetSeriesLimit(30). 46 SetSamplesLimit(0). 47 SetSizeBytesLimit(0), 48 }, 49 } 50 51 requestLimiter := newConfigRequestLimiter(nil, &limits) 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 limits := requestLimiter.limitsFor(tt.tenant) 56 testutil.Equals(t, tt.wantLimits, limits) 57 }) 58 } 59 } 60 61 func TestRequestLimiter_AllowRequestBodySizeBytes(t *testing.T) { 62 tests := []struct { 63 name string 64 defaultLimits *requestLimitsConfig 65 sizeByteLimit int64 66 sizeBytes int64 67 want bool 68 }{ 69 { 70 name: "Allowed when request size limit is < 0", 71 sizeByteLimit: -1, 72 sizeBytes: 30000, 73 want: true, 74 }, 75 { 76 name: "Allowed when request size limit is 0", 77 sizeByteLimit: 0, 78 sizeBytes: 30000, 79 want: true, 80 }, 81 { 82 name: "Allowed when under request size limit", 83 sizeByteLimit: 50000, 84 sizeBytes: 30000, 85 want: true, 86 }, 87 { 88 name: "Allowed when equal to the request size limit", 89 sizeByteLimit: 30000, 90 sizeBytes: 30000, 91 want: true, 92 }, 93 { 94 name: "Not allowed when above the request size limit", 95 sizeByteLimit: 30000, 96 sizeBytes: 30001, 97 want: false, 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 tenant := "tenant" 103 limits := WriteLimitsConfig{ 104 DefaultLimits: DefaultLimitsConfig{ 105 RequestLimits: *NewEmptyRequestLimitsConfig().SetSeriesLimit(10), 106 }, 107 TenantsLimits: TenantsWriteLimitsConfig{ 108 tenant: &WriteLimitConfig{ 109 RequestLimits: NewEmptyRequestLimitsConfig().SetSizeBytesLimit(tt.sizeByteLimit), 110 }, 111 }, 112 } 113 l := newConfigRequestLimiter(nil, &limits) 114 testutil.Equals(t, tt.want, l.AllowSizeBytes(tenant, tt.sizeBytes)) 115 }) 116 } 117 } 118 119 func TestRequestLimiter_AllowSeries(t *testing.T) { 120 tests := []struct { 121 name string 122 seriesLimit int64 123 series int64 124 want bool 125 }{ 126 { 127 name: "Allowed when series limit is < 0", 128 seriesLimit: -1, 129 series: 30000, 130 want: true, 131 }, 132 { 133 name: "Allowed when series limit is 0", 134 seriesLimit: 0, 135 series: 30000, 136 want: true, 137 }, 138 { 139 name: "Allowed when under series limit", 140 seriesLimit: 50000, 141 series: 30000, 142 want: true, 143 }, 144 { 145 name: "Allowed when equal to the series limit", 146 seriesLimit: 30000, 147 series: 30000, 148 want: true, 149 }, 150 { 151 name: "Not allowed when above the series limit", 152 seriesLimit: 30000, 153 series: 30001, 154 want: false, 155 }, 156 } 157 for _, tt := range tests { 158 t.Run(tt.name, func(t *testing.T) { 159 tenant := "tenant" 160 limits := WriteLimitsConfig{ 161 DefaultLimits: DefaultLimitsConfig{ 162 RequestLimits: *NewEmptyRequestLimitsConfig().SetSeriesLimit(10), 163 }, 164 TenantsLimits: TenantsWriteLimitsConfig{ 165 tenant: &WriteLimitConfig{ 166 RequestLimits: NewEmptyRequestLimitsConfig().SetSeriesLimit(tt.seriesLimit), 167 }, 168 }, 169 } 170 171 l := newConfigRequestLimiter(nil, &limits) 172 testutil.Equals(t, tt.want, l.AllowSeries(tenant, tt.series)) 173 }) 174 } 175 } 176 177 func TestRequestLimiter_AllowSamples(t *testing.T) { 178 tests := []struct { 179 name string 180 samplesLimit int64 181 samples int64 182 want bool 183 }{ 184 { 185 name: "Allowed when samples limit is < 0", 186 samplesLimit: -1, 187 samples: 30000, 188 want: true, 189 }, 190 { 191 name: "Allowed when samples limit is 0", 192 samplesLimit: 0, 193 samples: 30000, 194 want: true, 195 }, 196 { 197 name: "Allowed when under samples limit", 198 samplesLimit: 50000, 199 samples: 30000, 200 want: true, 201 }, 202 { 203 name: "Allowed when equal to the samples limit", 204 samplesLimit: 30000, 205 samples: 30000, 206 want: true, 207 }, 208 { 209 name: "Not allowed when above the samples limit", 210 samplesLimit: 30000, 211 samples: 30001, 212 want: false, 213 }, 214 } 215 for _, tt := range tests { 216 t.Run(tt.name, func(t *testing.T) { 217 tenant := "tenant" 218 limits := WriteLimitsConfig{ 219 DefaultLimits: DefaultLimitsConfig{ 220 RequestLimits: *NewEmptyRequestLimitsConfig().SetSeriesLimit(10), 221 }, 222 TenantsLimits: TenantsWriteLimitsConfig{ 223 tenant: &WriteLimitConfig{ 224 RequestLimits: NewEmptyRequestLimitsConfig().SetSamplesLimit(tt.samplesLimit), 225 }, 226 }, 227 } 228 229 l := newConfigRequestLimiter(nil, &limits) 230 testutil.Equals(t, tt.want, l.AllowSamples("tenant", tt.samples)) 231 }) 232 } 233 }