/
summ_ai_test.py
744 lines (647 loc) · 25.3 KB
/
summ_ai_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
from __future__ import annotations
import asyncio
import time
from functools import partial
from typing import TYPE_CHECKING, TypeVar
import aiohttp
import pytest
from django.test.client import AsyncClient
from django.urls import reverse
from integreat_cms.cms.forms import PageTranslationForm
from integreat_cms.cms.models.pages.page_translation import PageTranslation
from integreat_cms.cms.utils.stringify_list import iter_to_string
from integreat_cms.summ_ai_api.summ_ai_api_client import (
SummAiApiClient,
SummAiInvalidJSONError,
SummAiRateLimitingExceeded,
SummAiRuntimeError,
)
from integreat_cms.summ_ai_api.utils import (
HTMLField,
PatientTaskQueue,
TextField,
worker,
)
from ..conftest import (
ANONYMOUS,
APP_TEAM,
CMS_TEAM,
PRIV_STAFF_ROLES,
ROOT,
SERVICE_TEAM,
)
from ..utils import assert_message_in_log
from .utils import (
enable_summ_api,
get_changed_pages,
make_mock_summ_ai_server,
make_mock_summ_ai_server_rate_limited,
make_rogue_summ_ai_server,
MockedRequest,
)
if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any, Final
from _pytest.logging import LogCaptureFixture
from pytest_django.fixtures import SettingsWrapper
# pylint: disable=global-statement
# Mapping between roles and pages used in the tests
# to avoid simultaneous translation of the same content by different users
role_pages_mapping: Final[dict[str, list[int]]] = {
ROOT: [1, 2],
APP_TEAM: [3, 4],
SERVICE_TEAM: [5],
CMS_TEAM: [6],
}
attempts: dict
def test_worker() -> None:
"""
Test that the worker properly works through tasks from the
PatientTaskQueue, returning the results.
"""
async def works(msg: str) -> str:
return msg
global attempts
attempts = {}
async def fails_on_first_attempt(msg: str) -> str:
if msg not in attempts:
attempts[msg] = 0
attempts[msg] += 1
if attempts[msg] < 2:
raise SummAiRateLimitingExceeded
return msg
tasks = [
partial(works, "1"),
partial(fails_on_first_attempt, "2"),
partial(works, "3"),
partial(fails_on_first_attempt, "4"),
partial(works, "5"),
]
task_generator = PatientTaskQueue(tasks, 0.1)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(worker(loop, task_generator, "0-test"))
assert set(result) == {"1", "2", "3", "4", "5"}
async def test_translate_text_field_successful_translation(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
:param settings: The Django settings
:param aiohttp_raw_server: The fixture providing the dummy aiohttp server used for faking the SUMM.AI API server
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
session = aiohttp.ClientSession()
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"translated_text": "a text",
"jobid": "9999",
},
status=200,
)
)
# Redirect call to the SUMM.AI API to the fake server
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
assert text_field.text == "ein Text"
translated_text_field = await my_api_client.translate_text_field(
session, text_field
)
assert translated_text_field is text_field
assert translated_text_field.translated_text == "a text"
# we check that the origin text is not changed
assert translated_text_field.text == "ein Text"
# The same with an HTMLField as well
html_field = HTMLField("content", PageTranslation(content="<p>ein <b>Text</b></p>"))
translated_html_segments = [
await my_api_client.translate_text_field(session, segment)
for segment in html_field.segments
]
assert set(translated_html_segments) == set(html_field.segments)
assert html_field.translated_text == "<p>a text</p>\n"
await session.close()
async def test_translate_text_field_hit_rate_limit(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
see :func:`~test_translate_text_field_successful_translation`
"""
session = aiohttp.ClientSession()
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"error": "Too many requests. Please wait and resend your request.",
},
status=429,
)
)
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
with pytest.raises(SummAiRateLimitingExceeded):
await my_api_client.translate_text_field(session, text_field)
await session.close()
async def test_translate_text_field_ddos_defense(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
see :func:`~test_translate_text_field_successful_translation`
"""
session = aiohttp.ClientSession()
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"error": "Too many requests. Please wait and resend your request.",
},
status=529,
)
)
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
with pytest.raises(SummAiRateLimitingExceeded):
await my_api_client.translate_text_field(session, text_field)
await session.close()
async def test_translate_text_field_internal_server_error(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
see :func:`~test_translate_text_field_successful_translation`
"""
session = aiohttp.ClientSession()
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
# get a fresh log to test
caplog.clear()
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"error": "An error occurred",
"jobid": "9999",
},
status=500,
)
)
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
await my_api_client.translate_text_field(session, text_field)
errors = tuple(
(record.message for record in caplog.records if record.levelname == "ERROR")
)
assert (
"SUMM.AI translation of <TextField (text: ein Text)> failed because of <class 'integreat_cms.summ_ai_api.utils.SummAiRuntimeError'>: API has internal server error"
in errors
)
await session.close()
async def test_translate_text_forbidden(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
tests 403 response
see :func:`~test_translate_text_field_successful_translation`
"""
session = aiohttp.ClientSession()
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
# get a fresh log to test
caplog.clear()
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"jobid": "9999",
},
status=403,
)
)
# Redirect call to the SUMM.AI API to the fake server
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
await my_api_client.translate_text_field(session, text_field)
errors = tuple(
(record.message for record in caplog.records if record.levelname == "ERROR")
)
assert len(errors) >= 1
assert (
"SUMM.AI translation of <TextField (text: ein Text)> failed because of <class 'integreat_cms.summ_ai_api.utils.SummAiRuntimeError'>: Unexpected API result: 403 - {'jobid': '9999'}"
in errors
)
await session.close()
async def test_translate_text_with_empty_text_field(
settings: SettingsWrapper, aiohttp_raw_server: Callable, caplog: LogCaptureFixture
) -> None:
"""
see :func:`~test_translate_text_field_successful_translation`
"""
session = aiohttp.ClientSession()
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
session = aiohttp.ClientSession()
caplog.clear()
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"translated_text": "a text",
"jobid": "9999",
},
status=200,
)
)
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
text_field.text = ""
with pytest.raises(SummAiRuntimeError):
await my_api_client.translate_text_field(session, text_field)
await session.close()
@pytest.mark.django_db
async def test_auto_translate_easy_german(
login_role_user_async: tuple[AsyncClient, str],
settings: SettingsWrapper,
aiohttp_server: Callable,
caplog: LogCaptureFixture,
) -> None:
"""
This test checks whether the SUMM.AI API client works as expected
:param login_role_user_async: The fixture providing the http client and the current role (see :meth:`~tests.conftest.login_role_user`)
:param settings: The fixture providing the django settings
:param aiohttp_server: The fixture providing the dummy aiohttp server used for faking the SUMM.AI API server
:param caplog: The :fixture:`caplog` fixture
"""
# The region we want to use for testing
region_slug = "augsburg"
# Enable SUMM.AI in the test region
await enable_summ_api(region_slug)
# Setup a dummy server to fake responses from the SUMM.AI API server (an instance of aiohttp.test_utils.RawTestServer)
fake_server = await aiohttp_server(make_mock_summ_ai_server_rate_limited())
# Redirect call to the SUMM.AI API to the fake server
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
# Reduce rate limiting timeout
settings.SUMM_AI_RATE_LIMIT_COOLDOWN = 0.5
# Test for english messages
settings.LANGUAGE_CODE = "en"
# Log the user in
client, role = login_role_user_async
# Translate the pages
selected_ids = role_pages_mapping.get(role, [1])
translate_easy_german = reverse(
"machine_translation_pages",
kwargs={
"region_slug": region_slug,
"language_slug": settings.SUMM_AI_EASY_GERMAN_LANGUAGE_SLUG,
},
)
response = await client.post(
translate_easy_german, data={"selected_ids[]": selected_ids}
)
print(response.headers)
if role in PRIV_STAFF_ROLES:
# If the role should be allowed to access the view, we expect a successful result
assert response.status_code == 302
page_tree = reverse(
"pages",
kwargs={
"region_slug": region_slug,
"language_slug": settings.SUMM_AI_EASY_GERMAN_LANGUAGE_SLUG,
},
)
assert response.headers.get("Location") == page_tree
response = await client.get(page_tree)
print(response.headers)
# Get the page objects including their translations from the database
changed_pages = await get_changed_pages(settings, selected_ids)
# Check that the success message are present
if len(changed_pages) == 1:
assert_message_in_log(
f'SUCCESS Page "{changed_pages[0][settings.SUMM_AI_GERMAN_LANGUAGE_SLUG]}" has been successfully translated into Easy German.',
caplog,
)
else:
changed_pages_str = iter_to_string(
page[settings.SUMM_AI_GERMAN_LANGUAGE_SLUG] for page in changed_pages
)
assert_message_in_log(
f"SUCCESS The following pages have been successfully translated into Easy German: {changed_pages_str}",
caplog,
)
# Check that the page translation exists and really has the correct content
for page in changed_pages:
assert (
"Hier ist Ihre Leichte Sprache Übersetzung"
in page[settings.SUMM_AI_EASY_GERMAN_LANGUAGE_SLUG].content
)
elif role == ANONYMOUS:
# For anonymous users, we want to redirect to the login form instead of showing an error
assert response.status_code == 302
assert (
response.headers.get("location")
== f"{settings.LOGIN_URL}?next={translate_easy_german}"
)
else:
# For logged in users, we want to show an error if they get a permission denied
assert response.status_code == 403
@pytest.mark.django_db
async def test_summ_ai_error_handling(
login_role_user_async: tuple[AsyncClient, str],
settings: SettingsWrapper,
aiohttp_raw_server: Callable,
caplog: LogCaptureFixture,
) -> None:
"""
This test checks whether the error handling of the SUMM.AI API client works as expected.
:param login_role_user_async: The fixture providing the http client and the current role (see :meth:`~tests.conftest.login_role_user`)
:param settings: The fixture providing the django settings
:param aiohttp_raw_server: The fixture providing the dummy aiohttp server used for faking the SUMM.AI API server
:param caplog: The :fixture:`caplog` fixture
"""
# The region we want to use for testing
region_slug = "augsburg"
# Setup a dummy server to fake responses from the SUMM.AI API server (an instance of aiohttp.test_utils.RawTestServer)
fake_server = await aiohttp_raw_server(
make_mock_summ_ai_server(
response_data={
"error": "An error occurred",
},
status=500,
)
)
# Enable SUMM.AI in the test region
await enable_summ_api(region_slug)
# Redirect call to the SUMM.AI API to the fake server
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
# Test for english messages
settings.LANGUAGE_CODE = "en"
# Log the user in
client, role = login_role_user_async
# Translate the pages
translate_easy_german = reverse(
"machine_translation_pages",
kwargs={
"region_slug": region_slug,
"language_slug": settings.SUMM_AI_EASY_GERMAN_LANGUAGE_SLUG,
},
)
ready_for_mt_page_id = 14
response = await client.post(
translate_easy_german, data={"selected_ids[]": [ready_for_mt_page_id]}
)
print(response.headers)
if role in PRIV_STAFF_ROLES:
# If the role should be allowed to access the view, we expect a redirect to the page tree
assert response.status_code == 302
page_tree = reverse(
"pages",
kwargs={
"region_slug": region_slug,
"language_slug": settings.SUMM_AI_EASY_GERMAN_LANGUAGE_SLUG,
},
)
assert response.headers.get("Location") == page_tree
response = await client.get(page_tree)
print(response.headers)
# Check that the error message is present
assert_message_in_log(
'ERROR Page "Behörden und Beratung" could not be automatically translated into Easy German.',
caplog,
)
def test_validate_response_valid() -> None:
"""
Test for validate_response method.
Should return True when 'translated_text' is found in request.
"""
response_data_translation_succeeded = {
"dummy": "dummy",
"translated_text": "Das ist einfaches Deutsch.",
}
assert (
SummAiApiClient(MockedRequest(), PageTranslationForm).validate_response(
response_data_translation_succeeded, 200
)
is True
), "if translated_text found in reponse-data, validate_response should return True"
def test_validate_response_invalid() -> None:
"""
Test for validate_response method.
Should throw exception when 'translated_text' is not found in request.
"""
response_data_translation_failed = {
"dummy": "dummy",
"error": "no translation test error",
}
with pytest.raises(SummAiRuntimeError):
SummAiApiClient(MockedRequest(), PageTranslationForm).validate_response(
response_data_translation_failed, 200
)
async def test_unexpected_html(
settings: SettingsWrapper, aiohttp_raw_server: Callable
) -> None:
"""
Test correct handling for an unexpected HTML response by SUMM.AI.
Tests if SummAiInvalidJSONError is raised when unexpected HTML is sent as response.
"""
session = aiohttp.ClientSession()
fake_server = await aiohttp_raw_server(
make_rogue_summ_ai_server(
response_text="""
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
webmaster@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at localhost Port 8080</address>
</body></html>
""",
status=200,
)
)
# Redirect call to the SUMM.AI API to the fake server
settings.SUMM_AI_API_URL = (
f"{fake_server.scheme}://{fake_server.host}:{fake_server.port}"
)
my_api_client = SummAiApiClient(MockedRequest(), PageTranslationForm)
text_field = TextField("content", PageTranslation(content="ein Text"))
with pytest.raises(SummAiInvalidJSONError):
await my_api_client.translate_text_field(session, text_field)
async def test_missing_translation(
settings: SettingsWrapper, aiohttp_raw_server: Callable
) -> None:
"""
Test correct handling for a json response without the translated_text key by SUMM.AI.
Tests if SummAiRuntimeError is raised when invalid JSON is sent as response.
"""
response_data_translation_missing = {
"dummy": "dummy",
}
with pytest.raises(SummAiRuntimeError):
SummAiApiClient(MockedRequest(), PageTranslationForm).validate_response(
response_data_translation_missing, 200
)
def test_check_rate_limit_exceeded(
settings: SettingsWrapper, caplog: LogCaptureFixture
) -> None:
"""
Test for check_rate_limit_exceeded method. Tests if return is True when
response-status = 429 (too many requests) or 529 (site overloaded),
otherwise False
:param settings: The Django settings
:param caplog: Fixture for asserting log messages in tests (see :fixture:`pytest:caplog`)
"""
assert (
SummAiApiClient(MockedRequest(), PageTranslationForm).check_rate_limit_exceeded(
200
)
is False
), "if response-status not in (429, 529), check-rate-limit should return False"
with pytest.raises(SummAiRateLimitingExceeded):
SummAiApiClient(MockedRequest(), PageTranslationForm).check_rate_limit_exceeded(
429
)
with pytest.raises(SummAiRateLimitingExceeded):
SummAiApiClient(MockedRequest(), PageTranslationForm).check_rate_limit_exceeded(
529
)
errors = tuple(
(record.message for record in caplog.records if record.levelname == "ERROR")
)
assert len(errors) >= 2
assert (
f"SUMM.AI translation is waiting for {settings.SUMM_AI_RATE_LIMIT_COOLDOWN}s because the rate limit has been exceeded"
in errors
), "after check_rate_limit_exceeded() with 429 or 529, a logging entry should appear"
assert (
SummAiApiClient(MockedRequest(), PageTranslationForm).check_rate_limit_exceeded(
500
)
is False
), "if response-status not in (429, 529), check-rate-limit should return False"
async def test_patient_task_queue_normal_deque() -> None:
"""
Test for PatientTaskQueue Class. Tests that a first-in/first out (fifo) deque-queue is built from a task-list
Checks that __anext__() removes one element each from the queue
"""
tasks = [1, 2, 3]
task_generator = PatientTaskQueue(tasks, 1)
# we check that list comes reverse (fifo-queue)
i = 0
async for task in task_generator:
assert task == tasks[i]
i += 1
assert not task_generator
# we check that __anext__() did remove elements from the queue
with pytest.raises(StopAsyncIteration):
await anext(task_generator)
# pylint: disable=comparison-with-callable
async def test_patient_task_queue_hit_rate_limit() -> None:
"""
Test for PatientTaskQueue Class. It tests that when the maximum number of parallel requests
to the summ_ai_api has been exceeded (rate limit exceeded), a defined time is waited until the next task object is returned via __anext__().
"""
T = TypeVar("T")
async def identity(x: T) -> T:
return x
tasks = [
partial(identity, 1),
partial(identity, 2),
partial(identity, 3),
]
# for the test, waiting-time is reduced to 0.5s, default is defined in settings.SUMM_AI_RATE_LIMIT_COOLDOWN
task_generator = PatientTaskQueue(tasks, wait_time=0.5)
# Test first task
task = await anext(task_generator)
assert task == tasks[0], "PatientTaskQueue should return the first element"
# Test second task with timing
start = time.time()
task = await anext(task_generator)
end = time.time()
assert task == tasks[1], "PatientTaskQueue should return the second element"
assert (
end - start < 0.5
), "PatientTaskQueue should return the second element very fast"
# Hit the rate limit and check whether the second task is rescheduled
task_generator.hit_rate_limit(task)
start = time.time()
task = await anext(task_generator)
end = time.time()
assert task == tasks[1], "PatientTaskQueue should return the second element again"
assert end - start > 0.5, "PatientTaskQueue should return the second element slower"
# Test third task
start = time.time()
task = await anext(task_generator)
end = time.time()
assert task == tasks[2], "PatientTaskQueue should return the third element"
assert (
end - start < 0.5
), "PatientTaskQueue should return the third element very fast"
assert (
not task_generator
), "PatientTaskQueue should be empty after three tasks have been removed"
# pylint: disable=comparison-with-callable
async def test_patient_task_queue_max_retries() -> None:
"""
Test for PatientTaskQueue Class. Tests that it stops early
when requests are not successful after a cooldown, resulting in another cooldown repeatedly.
"""
T = TypeVar("T")
async def identity(x: T) -> T:
return x
tasks = [
partial(identity, 1),
partial(identity, 2),
partial(identity, 3),
]
# for the test, waiting_time is reduced to 0.5s, default is defined in settings.SUMM_AI_RATE_LIMIT_COOLDOWN
# max_retries is reduced to 3, default is defined in settings.SUMM_AI_MAX_RETRIES
task_generator = PatientTaskQueue(tasks, wait_time=0.5, max_retries=2)
# Test first task
task = await anext(task_generator)
assert task == tasks[0], "PatientTaskQueue should return the first element"
# Hit the rate limit, first retry
task_generator.hit_rate_limit(task)
task = await anext(task_generator)
assert task == tasks[0], "PatientTaskQueue should return the first element again"
# Report that task was successful to reset the retry count
task_generator.completed(task)
task = await anext(task_generator)
assert task == tasks[1], "PatientTaskQueue should return the second element"
# Hit the rate limit, first retry again
task_generator.hit_rate_limit(task)
task = await anext(task_generator)
assert task == tasks[1], "PatientTaskQueue should return the second element again"
# Hit the rate limit, second retry
task_generator.hit_rate_limit(task)
task = await anext(task_generator)
assert (
task == tasks[1]
), "PatientTaskQueue should return the second element for the third time"
# Hit the rate limit, there should be no more retries
task_generator.hit_rate_limit(task)
with pytest.raises(StopAsyncIteration):
task = await anext(task_generator)
assert (
len(task_generator) == 2
), "PatientTaskQueue should have two tasks left that could not be completed"
assert set(task_generator) == {
tasks[1],
tasks[2],
}, "PatientTaskQueue should have the second and third task left as they could not be completed"