Skip to content

Commit 2dc6466

Browse files
committed
store test: Explicitly test rate-limit errors on poll
1 parent b28eb28 commit 2dc6466

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/model/store_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,30 @@ void main() {
719719
connection.prepare(httpStatus: 200, body: 'nonsense');
720720
}
721721

722+
void prepareRateLimitExceptionCode() {
723+
// Example from the Zulip API docs:
724+
// https://zulip.com/api/rest-error-handling#rate-limit-exceeded
725+
// (The actual HTTP status should be 429, but that seems undocumented.)
726+
connection.prepare(httpStatus: 400, json: {
727+
'result': 'error', 'code': 'RATE_LIMIT_HIT',
728+
'msg': 'API usage exceeded rate limit',
729+
'retry-after': 28.706807374954224});
730+
}
731+
732+
void prepareRateLimitExceptionStatus() {
733+
// The HTTP status code for hitting a rate limit,
734+
// but for some reason a boring BAD_REQUEST error body.
735+
connection.prepare(httpStatus: 429, json: {
736+
'result': 'error', 'code': 'BAD_REQUEST', 'msg': 'Bad request'});
737+
}
738+
739+
void prepareRateLimitExceptionMalformed() {
740+
// The HTTP status code for hitting a rate limit,
741+
// but for some reason a non-JSON body.
742+
connection.prepare(httpStatus: 429,
743+
body: '<html><body>An error occurred.</body></html>');
744+
}
745+
722746
void prepareZulipApiExceptionBadRequest() {
723747
connection.prepare(httpStatus: 400, json: {
724748
'result': 'error', 'code': 'BAD_REQUEST', 'msg': 'Bad request'});
@@ -749,6 +773,18 @@ void main() {
749773
checkRetry(prepareMalformedServerResponseException);
750774
});
751775

776+
test('retries on rate limit: code RATE_LIMIT_HIT', () {
777+
checkRetry(prepareRateLimitExceptionCode);
778+
});
779+
780+
test('retries on rate limit: status 429 ZulipApiException', () {
781+
checkRetry(prepareRateLimitExceptionStatus);
782+
});
783+
784+
test('retries on rate limit: status 429 MalformedServerResponseException', () {
785+
checkRetry(prepareRateLimitExceptionMalformed);
786+
});
787+
752788
test('retries on generic ZulipApiException', () {
753789
checkRetry(prepareZulipApiExceptionBadRequest);
754790
});
@@ -882,6 +918,24 @@ void main() {
882918
"Error connecting to Zulip at");
883919
});
884920

921+
test('report rate limit: code RATE_LIMIT_HIT', () {
922+
checkReported(prepareRateLimitExceptionCode).startsWith(
923+
"Error connecting to Zulip. Retrying…\n"
924+
"Error connecting to Zulip at");
925+
});
926+
927+
test('report rate limit: status 429 ZulipApiException', () {
928+
checkReported(prepareRateLimitExceptionStatus).startsWith(
929+
"Error connecting to Zulip. Retrying…\n"
930+
"Error connecting to Zulip at");
931+
});
932+
933+
test('report rate limit: status 429 MalformedServerResponseException', () {
934+
checkReported(prepareRateLimitExceptionMalformed).startsWith(
935+
"Error connecting to Zulip. Retrying…\n"
936+
"Error connecting to Zulip at");
937+
});
938+
885939
test('report generic ZulipApiException', () {
886940
checkReported(prepareZulipApiExceptionBadRequest).startsWith(
887941
"Error connecting to Zulip. Retrying…\n"

0 commit comments

Comments
 (0)