1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.appsearch; 18 19 import static com.android.server.appsearch.ServiceAppSearchConfig.DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY; 20 import static com.android.server.appsearch.ServiceAppSearchConfig.DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE; 21 import static com.android.server.appsearch.ServiceAppSearchConfig.DEFAULT_RATE_LIMIT_API_COSTS_STRING; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import com.android.server.appsearch.external.localstorage.stats.CallStats; 26 27 import org.junit.Test; 28 29 public class AppSearchRateLimitConfigTest { 30 @Test testDefaultRateLimitConfig()31 public void testDefaultRateLimitConfig() { 32 AppSearchRateLimitConfig rateLimitConfig = AppSearchRateLimitConfig.create( 33 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 34 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 35 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 36 37 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo( 38 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY); 39 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo( 40 (int) (DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE 41 * DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY)); 42 // Verify that API costs are set to default of 1 43 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 44 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 45 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 46 assertThat(rateLimitConfig.getApiCost( 47 CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_SEARCH)).isEqualTo(1); 48 } 49 50 @Test testCustomRateLimitConfig()51 public void testCustomRateLimitConfig() { 52 AppSearchRateLimitConfig rateLimitConfig = 53 AppSearchRateLimitConfig.create(1000, 0.8f, 54 "localPutDocuments:5;localGetDocuments:11;localSetSchema:99"); 55 56 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 57 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 58 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(5); 59 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 60 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 61 // Unset API costs = 1 by default 62 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 63 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 64 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 65 assertThat(rateLimitConfig.getApiCost( 66 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 67 } 68 69 @Test testRateLimitConfigRebuild_noChanges()70 public void testRateLimitConfigRebuild_noChanges() { 71 AppSearchRateLimitConfig rateLimitConfig1 = AppSearchRateLimitConfig.create( 72 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 73 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 74 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 75 AppSearchRateLimitConfig rateLimitConfig2 = rateLimitConfig1.rebuildIfNecessary( 76 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 77 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 78 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 79 assertThat(rateLimitConfig2).isEqualTo(rateLimitConfig1); 80 } 81 82 @Test testRateLimitConfigRebuild_changeTotalCapacity()83 public void testRateLimitConfigRebuild_changeTotalCapacity() { 84 AppSearchRateLimitConfig rateLimitConfig = AppSearchRateLimitConfig.create( 85 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 86 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 87 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 88 rateLimitConfig = rateLimitConfig.rebuildIfNecessary(1000, 89 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 90 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 91 92 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 93 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo( 94 (int) (DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE 95 * 1000)); 96 // API costs = 1 by default 97 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 98 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 99 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 100 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 101 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 102 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 103 assertThat(rateLimitConfig.getApiCost( 104 CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_SEARCH)).isEqualTo(1); 105 } 106 107 @Test testRateLimitConfigRebuild_changePerPackagePercentage()108 public void testRateLimitConfigRebuild_changePerPackagePercentage() { 109 AppSearchRateLimitConfig rateLimitConfig = AppSearchRateLimitConfig.create(10000, 110 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 111 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 112 rateLimitConfig = rateLimitConfig.rebuildIfNecessary(10000, 0.5f, 113 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 114 115 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(10000); 116 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo( 117 (int) (0.5 * 10000)); 118 // API costs = 1 by default 119 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 120 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 121 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 122 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 123 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 124 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 125 assertThat(rateLimitConfig.getApiCost( 126 CallStats.CALL_TYPE_REMOVE_DOCUMENTS_BY_SEARCH)).isEqualTo(1); 127 } 128 129 @Test testRateLimitConfigRebuild_changeApiCosts()130 public void testRateLimitConfigRebuild_changeApiCosts() { 131 AppSearchRateLimitConfig rateLimitConfig = AppSearchRateLimitConfig.create( 132 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 133 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 134 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 135 rateLimitConfig = rateLimitConfig.rebuildIfNecessary( 136 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 137 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 138 "localPutDocuments:5;localGetDocuments:11;localSetSchema:99"); 139 140 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo( 141 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY); 142 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo( 143 (int) (DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE 144 * DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY)); 145 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(5); 146 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 147 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 148 // Unset API costs = 1 by default 149 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 150 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 151 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 152 assertThat(rateLimitConfig.getApiCost( 153 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 154 } 155 156 @Test testRateLimitConfigRebuild_allConfigsChanged()157 public void testRateLimitConfigRebuild_allConfigsChanged() { 158 AppSearchRateLimitConfig rateLimitConfig = AppSearchRateLimitConfig.create( 159 DEFAULT_RATE_LIMIT_TASK_QUEUE_TOTAL_CAPACITY, 160 DEFAULT_RATE_LIMIT_TASK_QUEUE_PER_PACKAGE_CAPACITY_PERCENTAGE, 161 DEFAULT_RATE_LIMIT_API_COSTS_STRING); 162 rateLimitConfig = rateLimitConfig.rebuildIfNecessary(1000, 0.8f, 163 "localPutDocuments:5;localGetDocuments:11;localSetSchema:99"); 164 165 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 166 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 167 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(5); 168 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 169 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 170 // Unset API costs = 1 by default 171 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 172 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 173 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 174 assertThat(rateLimitConfig.getApiCost( 175 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 176 } 177 178 @Test testApiCostStringCaseSensitive()179 public void testApiCostStringCaseSensitive() { 180 AppSearchRateLimitConfig rateLimitConfig = 181 AppSearchRateLimitConfig.create(1000, 0.8f, 182 "putdocuments:5;LOCALgEtDOcUmENts:11;SETSCHEMA:99"); 183 184 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 185 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 186 // Unable to set API costs since the API name is case-sensitive 187 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 188 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 189 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 190 // Unset API costs = 1 by default 191 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 192 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 193 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 194 assertThat(rateLimitConfig.getApiCost( 195 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 196 } 197 198 @Test testInvalidApiCostsString_unspecifiedApiCosts()199 public void testInvalidApiCostsString_unspecifiedApiCosts() { 200 AppSearchRateLimitConfig rateLimitConfig = 201 AppSearchRateLimitConfig.create(1000, 0.8f, 202 "localPutDocuments;localGetDocuments:;localSetSchema:99"); 203 204 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 205 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 206 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 207 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 208 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 209 // Unset API costs = 1 by default 210 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 211 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 212 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 213 assertThat(rateLimitConfig.getApiCost( 214 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 215 } 216 217 @Test testInvalidApiCostsString_nonIntegerApiCosts()218 public void testInvalidApiCostsString_nonIntegerApiCosts() { 219 AppSearchRateLimitConfig rateLimitConfig = 220 AppSearchRateLimitConfig.create(1000, 0.8f, 221 "localPutDocuments:0.5;localGetDocuments:11;localSetSchema:99"); 222 223 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 224 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 225 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 226 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 227 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 228 // Unset API costs = 1 by default 229 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 230 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 231 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 232 assertThat(rateLimitConfig.getApiCost( 233 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 234 235 rateLimitConfig = 236 AppSearchRateLimitConfig.create(1000, 0.8f, 237 "localPutDocuments:cost"); 238 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 239 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 240 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 241 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 242 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 243 // Unset API costs = 1 by default 244 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 245 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 246 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 247 assertThat(rateLimitConfig.getApiCost( 248 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 249 } 250 251 @Test testInvalidApiCostsString_negativeApiCostNotAllowed()252 public void testInvalidApiCostsString_negativeApiCostNotAllowed() { 253 AppSearchRateLimitConfig rateLimitConfig = 254 AppSearchRateLimitConfig.create(1000, 0.8f, 255 "localPutDocuments:-1;localGetDocuments:11;localSetSchema:99"); 256 257 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 258 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 259 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 260 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 261 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 262 // Unset API costs = 1 by default 263 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 264 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 265 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 266 assertThat(rateLimitConfig.getApiCost( 267 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 268 } 269 270 @Test testInvalidApiCostsString_emptyString()271 public void testInvalidApiCostsString_emptyString() { 272 AppSearchRateLimitConfig rateLimitConfig = 273 AppSearchRateLimitConfig.create(1000, 0.8f, ""); 274 275 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 276 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 277 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 278 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 279 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 280 // Unset API costs = 1 by default 281 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 282 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 283 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 284 assertThat(rateLimitConfig.getApiCost( 285 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 286 } 287 288 @Test testInvalidApiCostsString_emptySegments()289 public void testInvalidApiCostsString_emptySegments() { 290 AppSearchRateLimitConfig rateLimitConfig = 291 AppSearchRateLimitConfig.create(1000, 0.8f, ";localPutDocuments:10;"); 292 293 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 294 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 295 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(10); 296 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 297 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 298 // Unset API costs = 1 by default 299 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 300 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 301 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 302 assertThat(rateLimitConfig.getApiCost( 303 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 304 } 305 306 @Test testInvalidApiCostsString_wrongCostDelimiter()307 public void testInvalidApiCostsString_wrongCostDelimiter() { 308 AppSearchRateLimitConfig rateLimitConfig = 309 AppSearchRateLimitConfig.create(1000, 0.8f, "localPutDocuments;10"); 310 311 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 312 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 313 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 314 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 315 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 316 // Unset API costs = 1 by default 317 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 318 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 319 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 320 assertThat(rateLimitConfig.getApiCost( 321 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 322 } 323 324 @Test testInvalidApiCostsString_wrongEntryDelimiter()325 public void testInvalidApiCostsString_wrongEntryDelimiter() { 326 AppSearchRateLimitConfig rateLimitConfig = 327 AppSearchRateLimitConfig.create(1000, 0.8f, 328 "localPutDocuments:10:localGetDocuments:11:localSetSchema:99"); 329 330 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 331 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 332 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 333 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(1); 334 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(1); 335 // Unset API costs = 1 by default 336 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 337 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 338 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 339 assertThat(rateLimitConfig.getApiCost( 340 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 341 } 342 343 @Test testInvalidApiCostsString_invalidApiEntry()344 public void testInvalidApiCostsString_invalidApiEntry() { 345 AppSearchRateLimitConfig rateLimitConfig = 346 AppSearchRateLimitConfig.create(1000, 0.8f, 347 "foo:10;localGetDocuments:11;localSetSchema:99"); 348 349 assertThat(rateLimitConfig.getTaskQueueTotalCapacity()).isEqualTo(1000); 350 assertThat(rateLimitConfig.getTaskQueuePerPackageCapacity()).isEqualTo(800); 351 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_DOCUMENTS)).isEqualTo(11); 352 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SET_SCHEMA)).isEqualTo(99); 353 // Unset API costs = 1 by default 354 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_PUT_DOCUMENTS)).isEqualTo(1); 355 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_NAMESPACES)).isEqualTo(1); 356 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_SEARCH)).isEqualTo(1); 357 assertThat(rateLimitConfig.getApiCost(CallStats.CALL_TYPE_GET_STORAGE_INFO)).isEqualTo(1); 358 assertThat(rateLimitConfig.getApiCost( 359 CallStats.CALL_TYPE_REMOVE_DOCUMENT_BY_SEARCH)).isEqualTo(1); 360 } 361 } 362