-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 941 - respect rate-limits during integration tests
New files: * `too_many_requests_exception.dart`: Exception when the server returns "Too many requests". * `too_many_requests_manager.dart`: Manager dedicated to "too many requests" server response. Deleted files: * `api_get_to_be_completed_products_test.dart`: moved "searchProduct" code to `api_search_products_test` * `api_get_user_products_test.dart`: moved "searchProduct" code to `api_search_products_test` * `api_matched_product_v2_test.dart`: moved "searchProduct" code to `api_search_products_test` Impacted files: * `api_get_localized_product_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_get_product_image_ids_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_get_product_test.dart`: moved "searchProduct" code to `api_search_products_test`; now using the `TooManyRequestsManager` for "getProduct" queries * `api_json_to_from_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_matched_product_v1_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_not_food_get_product_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_ocr_ingredients_test.dart`: now using the `TooManyRequestsManager` for "getProduct" queries * `api_search_products_test.dart`: now using the `TooManyRequestsManager` for "searchProducts" queries; moved code there from other test files, gathering all "searchProducts" queries here * `get_locations_order.dart`: unrelated minor improvement * `get_proofs_order.dart`: unrelated minor improvement * `open_food_api_client.dart`: now checking for `TooManyRequestsException`s for "getProduct" and "searchProducts" queries * `openfoodfacts.dart`: added the 2 new files
- Loading branch information
1 parent
e4bd1ed
commit 655a321
Showing
17 changed files
with
664 additions
and
589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:http/http.dart'; | ||
|
||
/// Exception when the server returns "Too many requests". | ||
class TooManyRequestsException implements Exception { | ||
const TooManyRequestsException(); | ||
|
||
/// Start of the response body when the server received too many requests. | ||
static const String _tooManyRequestsError = | ||
'<!DOCTYPE html><html><head><meta name="robots" content="noindex"></head><body><h1>TOO MANY REQUESTS</h1>'; | ||
|
||
static void check(final Response response) { | ||
if (response.body.startsWith(_tooManyRequestsError)) { | ||
throw TooManyRequestsException(); | ||
} | ||
} | ||
|
||
@override | ||
String toString() => 'Too many requests'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/// Manager dedicated to "too many requests" server response. | ||
/// | ||
/// Typically, the server may limit the number of requests to a [maxCount] | ||
/// during a specific [duration]. | ||
class TooManyRequestsManager { | ||
TooManyRequestsManager({ | ||
required this.maxCount, | ||
required this.duration, | ||
}); | ||
|
||
final int maxCount; | ||
final Duration duration; | ||
|
||
final List<int> _requestTimestamps = <int>[]; | ||
|
||
/// Waits the needed duration in order to avoid "too many requests" error. | ||
Future<void> waitIfNeeded() async { | ||
while (_requestTimestamps.length >= maxCount) { | ||
final int previousInMillis = _requestTimestamps.first; | ||
final int nowInMillis = DateTime.now().millisecondsSinceEpoch; | ||
final int waitingInMillis = | ||
duration.inMilliseconds - nowInMillis + previousInMillis; | ||
if (waitingInMillis > 0) { | ||
await Future.delayed(Duration(milliseconds: waitingInMillis)); | ||
} | ||
_requestTimestamps.removeAt(0); | ||
} | ||
final DateTime now = DateTime.now(); | ||
final int nowInMillis = now.millisecondsSinceEpoch; | ||
_requestTimestamps.add(nowInMillis); | ||
} | ||
} | ||
|
||
/// [TooManyRequestsManager] dedicated to "searchProducts" queries in PROD. | ||
final TooManyRequestsManager searchProductsTooManyRequestsManager = | ||
TooManyRequestsManager( | ||
maxCount: 10, | ||
duration: Duration(minutes: 1), | ||
); | ||
|
||
/// [TooManyRequestsManager] dedicated to "getProduct" queries in PROD. | ||
final TooManyRequestsManager getProductTooManyRequestsManager = | ||
TooManyRequestsManager( | ||
maxCount: 100, | ||
duration: Duration(minutes: 1), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.