Spaces:
Sleeping
Sleeping
File size: 87,482 Bytes
816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 036c5c4 816ccbe 036c5c4 07eedfb 036c5c4 2dbfc89 07eedfb 036c5c4 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb 816ccbe 036c5c4 07eedfb 036c5c4 07eedfb 036c5c4 07eedfb 036c5c4 07eedfb 816ccbe 07eedfb 816ccbe 036c5c4 816ccbe 07eedfb ce64132 07eedfb 816ccbe 07eedfb 816ccbe 07eedfb c8b0c66 fef313c c8b0c66 fef313c 816ccbe c8b0c66 fef313c |
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 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 |
"""
Rigorous Tests for Priority-Tier Worker Pool implementation.
Tests cover:
1. Core Worker Behavior - Atomic claims, scheduling, efficiency
2. Job Status Transitions - State changes and retry logic
3. Priority Tier Isolation - Workers respect their tier
4. Wake Event System - Immediate job notification
5. Credit System Integration - Refunds, confirmations, idempotency
6. Error Handling - Exceptions, DB errors, validation
7. GeminiJobProcessor - Video polling, download retries, API key rotation
8. Concurrency Edge Cases - Race conditions, transaction safety
9. Pool Lifecycle - Start/stop, orphan refunds, clean shutdown
"""
import pytest
import asyncio
from unittest.mock import patch, MagicMock, AsyncMock, PropertyMock
from datetime import datetime, timedelta
from dataclasses import dataclass
# Test the modular priority worker pool
from services.priority_worker_pool import (
PriorityWorkerPool,
PriorityWorker,
WorkerConfig,
PriorityMapping,
JobProcessor,
get_interval_for_priority,
get_priority_for_job_type
)
# Test the Gemini-specific implementation
from services.gemini_service.job_processor import (
get_priority_for_job_type as gemini_get_priority,
JOB_PRIORITY_MAP,
GeminiJobProcessor
)
# Test credit service
from services.credit_service import (
is_refundable_error,
reserve_credit,
confirm_credit,
refund_credit,
handle_job_completion,
refund_orphaned_jobs,
REFUNDABLE_ERROR_PATTERNS,
NON_REFUNDABLE_ERROR_PATTERNS
)
# =============================================================================
# Mock Job Model for Testing
# =============================================================================
@dataclass
class MockJob:
"""Mock job model for testing."""
job_id: str = "test-job-123"
user_id: str = "user-456"
job_type: str = "text"
status: str = "queued"
priority: str = "fast"
next_process_at: datetime = None
retry_count: int = 0
third_party_id: str = None
input_data: dict = None
output_data: dict = None
error_message: str = None
created_at: datetime = None
started_at: datetime = None
completed_at: datetime = None
credits_reserved: int = 0
credits_refunded: bool = False
def __post_init__(self):
if self.created_at is None:
self.created_at = datetime.utcnow()
@dataclass
class MockUser:
"""Mock user model for testing."""
user_id: str = "user-456"
email: str = "test@example.com"
credits: int = 100
# =============================================================================
# 1. Priority Mapping Tests
# =============================================================================
class TestPriorityMapping:
"""Test job type to priority mapping."""
def test_text_job_is_fast(self):
assert gemini_get_priority("text") == "fast"
def test_analyze_job_is_fast(self):
assert gemini_get_priority("analyze") == "fast"
def test_animation_prompt_is_fast(self):
assert gemini_get_priority("animation_prompt") == "fast"
def test_image_job_is_medium(self):
assert gemini_get_priority("image") == "medium"
def test_edit_image_is_medium(self):
assert gemini_get_priority("edit_image") == "medium"
def test_video_job_is_slow(self):
assert gemini_get_priority("video") == "slow"
def test_unknown_job_defaults_to_fast(self):
assert gemini_get_priority("unknown_type") == "fast"
# =============================================================================
# 2. Interval Mapping Tests
# =============================================================================
class TestIntervalMapping:
"""Test priority to interval mapping."""
def test_fast_interval_with_default_config(self):
config = WorkerConfig()
assert get_interval_for_priority("fast", config) == config.fast_interval
def test_medium_interval_with_default_config(self):
config = WorkerConfig()
assert get_interval_for_priority("medium", config) == config.medium_interval
def test_slow_interval_with_default_config(self):
config = WorkerConfig()
assert get_interval_for_priority("slow", config) == config.slow_interval
def test_unknown_defaults_to_slow(self):
assert get_interval_for_priority("unknown") == 60
def test_custom_config_intervals(self):
config = WorkerConfig(fast_interval=10, medium_interval=20, slow_interval=30)
assert get_interval_for_priority("fast", config) == 10
assert get_interval_for_priority("medium", config) == 20
assert get_interval_for_priority("slow", config) == 30
class TestPriorityMappingClass:
"""Test the PriorityMapping dataclass."""
def test_get_priority_with_mappings(self):
mapping = PriorityMapping(mappings={"custom_type": "slow"})
assert mapping.get_priority("custom_type") == "slow"
def test_get_priority_default_when_not_found(self):
mapping = PriorityMapping(mappings={})
assert mapping.get_priority("unknown", default="medium") == "medium"
def test_get_interval_for_priorities(self):
mapping = PriorityMapping()
config = WorkerConfig(fast_interval=5, medium_interval=30, slow_interval=60)
assert mapping.get_interval("fast", config) == 5
assert mapping.get_interval("medium", config) == 30
assert mapping.get_interval("slow", config) == 60
# =============================================================================
# 3. Job Priority Map Coverage Tests
# =============================================================================
class TestJobPriorityMap:
"""Test that all expected job types are covered."""
def test_all_job_types_have_priority(self):
expected_types = ["text", "analyze", "animation_prompt", "image", "edit_image", "video"]
for job_type in expected_types:
assert job_type in JOB_PRIORITY_MAP, f"Job type '{job_type}' not in priority map"
def test_priority_values_are_valid(self):
valid_priorities = {"fast", "medium", "slow"}
for job_type, priority in JOB_PRIORITY_MAP.items():
assert priority in valid_priorities, f"Invalid priority '{priority}' for job type '{job_type}'"
# =============================================================================
# 4. Worker Pool Configuration Tests
# =============================================================================
class TestWorkerPoolConfiguration:
"""Test worker pool configuration."""
def test_default_config(self):
"""Test WorkerConfig defaults."""
config = WorkerConfig()
assert config.fast_workers == 5
assert config.medium_workers == 5
assert config.slow_workers == 5
assert config.fast_interval == 2
assert config.medium_interval == 10
assert config.slow_interval == 15
assert config.max_retries == 60
def test_custom_config(self):
"""Test WorkerConfig with custom values."""
config = WorkerConfig(
fast_workers=3,
medium_workers=2,
slow_workers=1,
fast_interval=10,
medium_interval=60,
slow_interval=120,
max_retries=100
)
assert config.fast_workers == 3
assert config.medium_workers == 2
assert config.slow_workers == 1
assert config.max_retries == 100
def test_total_workers_calculation(self):
"""Test total workers from config."""
config = WorkerConfig(fast_workers=5, medium_workers=5, slow_workers=5)
total = config.fast_workers + config.medium_workers + config.slow_workers
assert total == 15
def test_config_from_env(self):
"""Test WorkerConfig.from_env() with mocked environment."""
with patch.dict('os.environ', {
'FAST_WORKERS': '10',
'MEDIUM_WORKERS': '8',
'SLOW_WORKERS': '6'
}):
config = WorkerConfig.from_env()
assert config.fast_workers == 10
assert config.medium_workers == 8
assert config.slow_workers == 6
# =============================================================================
# 5. Priority Worker Tests
# =============================================================================
class TestPriorityWorker:
"""Test individual worker behavior."""
def test_worker_has_correct_attributes(self):
"""Test worker initialization."""
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=None,
job_model=None,
job_processor=None
)
assert worker.worker_id == 0
assert worker.priority == "fast"
assert worker.poll_interval == 5
assert worker._running == False
assert worker._current_job_id is None
def test_worker_with_max_retries(self):
"""Test worker respects max_retries config."""
worker = PriorityWorker(
worker_id=1,
priority="slow",
poll_interval=60,
session_maker=None,
job_model=None,
job_processor=None,
max_retries=100
)
assert worker.max_retries == 100
def test_worker_with_wake_event(self):
"""Test worker accepts wake event."""
event = asyncio.Event()
worker = PriorityWorker(
worker_id=2,
priority="medium",
poll_interval=30,
session_maker=None,
job_model=None,
job_processor=None,
wake_event=event
)
assert worker._wake_event is event
@pytest.mark.asyncio
async def test_worker_start_sets_running_flag(self):
"""Test worker.start() sets running flag."""
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=MagicMock(),
job_model=MockJob,
job_processor=MagicMock()
)
# Mock the poll loop to prevent actual execution
with patch.object(worker, '_poll_loop', new_callable=AsyncMock):
await worker.start()
assert worker._running == True
@pytest.mark.asyncio
async def test_worker_stop_clears_running_flag(self):
"""Test worker.stop() clears running flag."""
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=None,
job_model=None,
job_processor=None
)
worker._running = True
await worker.stop()
assert worker._running == False
# =============================================================================
# 5b. Poll Loop Efficiency Tests (Automatic Next Start)
# =============================================================================
class TestPollLoopEfficiency:
"""Test that workers process jobs efficiently without unnecessary delays."""
@pytest.mark.asyncio
async def test_poll_loop_continues_immediately_when_job_found(self):
"""
Poll loop should NOT sleep when a job was processed.
It should immediately check for the next job.
"""
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=MagicMock(),
job_model=MockJob,
job_processor=MagicMock()
)
# Track if sleep was called
sleep_called = False
original_sleep = asyncio.sleep
async def mock_sleep(duration):
nonlocal sleep_called
sleep_called = True
# Don't actually sleep in tests
# Simulate: first call returns job found (True), second call we stop the worker
call_count = 0
async def mock_process_one():
nonlocal call_count
call_count += 1
if call_count == 1:
return True # Job found and processed
else:
worker._running = False # Stop the loop
return False
with patch.object(worker, '_process_one_job', side_effect=mock_process_one):
with patch('asyncio.sleep', side_effect=mock_sleep):
worker._running = True
await worker._poll_loop()
# After first job, loop should immediately check again without sleeping
assert call_count == 2, "Loop should have checked for next job immediately"
@pytest.mark.asyncio
async def test_poll_loop_sleeps_when_no_job_found(self):
"""
Poll loop should sleep for poll_interval when no jobs are available.
"""
wake_event = asyncio.Event()
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=MagicMock(),
job_model=MockJob,
job_processor=MagicMock(),
wake_event=wake_event
)
wait_for_called = False
async def mock_wait_for(coro, timeout):
nonlocal wait_for_called
wait_for_called = True
assert timeout == 5, "Should use poll_interval as timeout"
worker._running = False # Stop the loop
raise asyncio.TimeoutError() # Simulate timeout
async def mock_process_one():
return False # No job found
with patch.object(worker, '_process_one_job', side_effect=mock_process_one):
with patch('asyncio.wait_for', side_effect=mock_wait_for):
worker._running = True
await worker._poll_loop()
assert wait_for_called, "Should have waited on wake event with timeout"
@pytest.mark.asyncio
async def test_wake_event_interrupts_sleep(self):
"""
Setting wake event should immediately wake sleeping workers.
"""
wake_event = asyncio.Event()
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=60, # Long interval
session_maker=MagicMock(),
job_model=MockJob,
job_processor=MagicMock(),
wake_event=wake_event
)
call_count = 0
async def mock_process_one():
nonlocal call_count
call_count += 1
if call_count >= 2:
worker._running = False
return False # No job found
async def check_and_signal():
await asyncio.sleep(0.01) # Let poll loop start waiting
wake_event.set() # Signal wake
with patch.object(worker, '_process_one_job', side_effect=mock_process_one):
worker._running = True
# Run both concurrently
await asyncio.gather(
worker._poll_loop(),
check_and_signal()
)
# Worker should have woken up and checked for jobs again
assert call_count >= 2, "Worker should have woken up when event was set"
# =============================================================================
# 5c. Queue Ordering Tests
# =============================================================================
class TestQueueOrdering:
"""Test that jobs are processed in correct order (FIFO by created_at)."""
def test_query_orders_by_created_at(self):
"""
Verify the worker query orders jobs by created_at ascending.
This tests the query structure in _process_one_job.
"""
from sqlalchemy import select
# The actual implementation uses:
# .order_by(self.job_model.created_at).limit(1)
# We verify this behavior by checking the SQL structure
# Create mock job model with created_at column
class MockJobModel:
job_id = "id"
priority = "fast"
status = "queued"
next_process_at = None
created_at = datetime.utcnow()
# Verify oldest job should be picked first
job1 = MockJob(job_id="job-1", created_at=datetime(2024, 1, 1, 10, 0, 0))
job2 = MockJob(job_id="job-2", created_at=datetime(2024, 1, 1, 9, 0, 0)) # Earlier
job3 = MockJob(job_id="job-3", created_at=datetime(2024, 1, 1, 11, 0, 0))
jobs = [job1, job2, job3]
sorted_jobs = sorted(jobs, key=lambda j: j.created_at)
assert sorted_jobs[0].job_id == "job-2", "Oldest job should be first"
assert sorted_jobs[1].job_id == "job-1"
assert sorted_jobs[2].job_id == "job-3"
def test_only_queued_and_processing_jobs_selected(self):
"""
Verify only jobs with status 'queued' or 'processing' are selected.
"""
valid_statuses = ["queued", "processing"]
invalid_statuses = ["completed", "failed", "cancelled"]
for status in valid_statuses:
job = MockJob(status=status)
# Would be selected by the query
assert job.status in ["queued", "processing"]
for status in invalid_statuses:
job = MockJob(status=status)
# Would NOT be selected by the query
assert job.status not in ["queued", "processing"]
# =============================================================================
# 5d. Atomic Job Claiming Tests
# =============================================================================
class TestAtomicJobClaiming:
"""Test atomic job claiming to prevent race conditions."""
def test_claim_uses_where_clause_for_atomicity(self):
"""
Job claiming should use WHERE clause to ensure atomicity.
UPDATE ... WHERE job_id = X AND status = 'queued'
"""
# The implementation uses:
# update(job_model).where(job_model.job_id == job.job_id, job_model.status == "queued")
# This ensures only one worker can claim a queued job
# Simulate race condition scenario
job = MockJob(job_id="race-job", status="queued")
# Worker 1 tries to claim
# UPDATE jobs SET status='processing' WHERE job_id='race-job' AND status='queued'
# Returns rowcount = 1 (success)
# Worker 2 tries to claim same job
# UPDATE jobs SET status='processing' WHERE job_id='race-job' AND status='queued'
# Returns rowcount = 0 (job already processing, WHERE fails)
# Verify the expected behavior
assert job.status == "queued", "Initial status should be queued"
# After worker 1 claims:
job.status = "processing"
# Worker 2's WHERE clause would not match
assert job.status != "queued", "Status changed, second claim would fail"
def test_claim_checks_next_process_at_for_processing_jobs(self):
"""
For jobs in 'processing' status, claiming should check next_process_at.
This prevents multiple workers from checking status simultaneously.
"""
now = datetime.utcnow()
# Job ready for status check
job_ready = MockJob(
status="processing",
next_process_at=now - timedelta(seconds=10) # In the past
)
assert job_ready.next_process_at <= now, "Job should be ready"
# Job not yet ready
job_not_ready = MockJob(
status="processing",
next_process_at=now + timedelta(seconds=60) # In the future
)
assert job_not_ready.next_process_at > now, "Job should not be ready yet"
def test_failed_claim_returns_gracefully(self):
"""
When a claim fails (rowcount=0), worker should skip the job gracefully.
"""
# Simulate: SELECT returns job, but UPDATE returns rowcount=0
# This happens when another worker claimed between SELECT and UPDATE
# The code checks: if result.rowcount == 0: return
# This is the expected graceful handling
rowcount = 0 # Simulating failed atomic update
should_process = rowcount > 0
assert should_process == False, "Worker should skip job when claim fails"
# =============================================================================
# 5e. Priority Tier Isolation Tests
# =============================================================================
class TestPriorityTierIsolation:
"""Test that workers only process jobs of their assigned priority."""
def test_fast_worker_only_sees_fast_jobs(self):
"""Fast priority worker should only query for fast priority jobs."""
worker = PriorityWorker(
worker_id=0,
priority="fast",
poll_interval=5,
session_maker=None,
job_model=None,
job_processor=None
)
# Create jobs of different priorities
fast_job = MockJob(priority="fast")
medium_job = MockJob(priority="medium")
slow_job = MockJob(priority="slow")
# Worker's query filter: job_model.priority == self.priority
assert worker.priority == "fast"
assert fast_job.priority == worker.priority # Would match
assert medium_job.priority != worker.priority # Would NOT match
assert slow_job.priority != worker.priority # Would NOT match
def test_medium_worker_only_sees_medium_jobs(self):
"""Medium priority worker should only query for medium priority jobs."""
worker = PriorityWorker(
worker_id=1,
priority="medium",
poll_interval=30,
session_maker=None,
job_model=None,
job_processor=None
)
fast_job = MockJob(priority="fast")
medium_job = MockJob(priority="medium")
slow_job = MockJob(priority="slow")
assert worker.priority == "medium"
assert fast_job.priority != worker.priority
assert medium_job.priority == worker.priority # Would match
assert slow_job.priority != worker.priority
def test_slow_worker_only_sees_slow_jobs(self):
"""Slow priority worker should only query for slow priority jobs."""
worker = PriorityWorker(
worker_id=2,
priority="slow",
poll_interval=60,
session_maker=None,
job_model=None,
job_processor=None
)
fast_job = MockJob(priority="fast")
medium_job = MockJob(priority="medium")
slow_job = MockJob(priority="slow")
assert worker.priority == "slow"
assert fast_job.priority != worker.priority
assert medium_job.priority != worker.priority
assert slow_job.priority == worker.priority # Would match
# =============================================================================
# 6. Credit System Tests - is_refundable_error
# =============================================================================
class TestIsRefundableError:
"""Test error classification for credit refunds."""
def test_empty_error_is_not_refundable(self):
assert is_refundable_error(None) == False
assert is_refundable_error("") == False
# Refundable errors
def test_api_key_invalid_is_refundable(self):
assert is_refundable_error("API_KEY_INVALID: Authentication failed") == True
def test_quota_exceeded_is_refundable(self):
assert is_refundable_error("QUOTA_EXCEEDED: Daily limit reached") == True
def test_internal_error_is_refundable(self):
assert is_refundable_error("INTERNAL_ERROR: Server crashed") == True
def test_connection_failed_is_refundable(self):
assert is_refundable_error("CONNECTION_FAILED: Could not reach API") == True
def test_timeout_is_refundable(self):
assert is_refundable_error("TIMEOUT: Request timed out after 30s") == True
def test_500_error_is_refundable(self):
assert is_refundable_error("Server returned 500 Internal Server Error") == True
def test_503_error_is_refundable(self):
assert is_refundable_error("503 Service Unavailable") == True
def test_429_rate_limit_is_refundable(self):
assert is_refundable_error("429 Too Many Requests") == True
def test_server_shutdown_is_refundable(self):
assert is_refundable_error("SERVER_SHUTDOWN: Graceful shutdown") == True
def test_max_retries_is_refundable(self):
assert is_refundable_error("Max retries (60) exceeded") == True
# Non-refundable errors
def test_safety_filter_is_not_refundable(self):
assert is_refundable_error("Content blocked by safety filter") == False
def test_blocked_content_is_not_refundable(self):
assert is_refundable_error("Request blocked due to policy violation") == False
def test_invalid_input_is_not_refundable(self):
assert is_refundable_error("INVALID_INPUT: Prompt too long") == False
def test_invalid_image_is_not_refundable(self):
assert is_refundable_error("Invalid image format provided") == False
def test_bad_request_400_is_not_refundable(self):
assert is_refundable_error("400 Bad Request") == False
def test_user_cancelled_is_not_refundable(self):
assert is_refundable_error("User cancelled the request") == False
def test_unknown_error_defaults_to_not_refundable(self):
assert is_refundable_error("Some random unknown error XYZ") == False
# =============================================================================
# 7. Credit System Tests - Reserve/Confirm/Refund
# =============================================================================
class TestReserveCredit:
"""Test credit reservation."""
@pytest.mark.asyncio
async def test_reserve_deducts_from_user(self):
"""Credits are deducted on reservation."""
session = AsyncMock()
user = MockUser(credits=100)
result = await reserve_credit(session, user, amount=10)
assert result == True
assert user.credits == 90
@pytest.mark.asyncio
async def test_reserve_fails_with_insufficient_credits(self):
"""Reservation fails if user doesn't have enough credits."""
session = AsyncMock()
user = MockUser(credits=5)
result = await reserve_credit(session, user, amount=10)
assert result == False
assert user.credits == 5 # Unchanged
@pytest.mark.asyncio
async def test_reserve_exact_amount(self):
"""User can reserve exactly their remaining credits."""
session = AsyncMock()
user = MockUser(credits=10)
result = await reserve_credit(session, user, amount=10)
assert result == True
assert user.credits == 0
class TestConfirmCredit:
"""Test credit confirmation."""
@pytest.mark.asyncio
async def test_confirm_clears_reservation(self):
"""Confirmation clears credits_reserved field."""
session = AsyncMock()
job = MockJob(credits_reserved=5)
await confirm_credit(session, job)
assert job.credits_reserved == 0
@pytest.mark.asyncio
async def test_confirm_no_op_when_no_reservation(self):
"""Confirmation is a no-op when no credits reserved."""
session = AsyncMock()
job = MockJob(credits_reserved=0)
await confirm_credit(session, job)
assert job.credits_reserved == 0
class TestRefundCredit:
"""Test credit refunding."""
@pytest.mark.asyncio
async def test_refund_restores_user_credits(self):
"""Refund restores credits to user."""
session = AsyncMock()
user = MockUser(user_id="user-456", credits=90)
job = MockJob(user_id="user-456", credits_reserved=10, credits_refunded=False)
# Mock the database query to return our user
from core.models import User
mock_result = MagicMock()
mock_result.scalar_one_or_none.return_value = user
session.execute = AsyncMock(return_value=mock_result)
result = await refund_credit(session, job, "Test refund")
assert result == True
assert user.credits == 100 # 90 + 10 refunded
assert job.credits_reserved == 0
assert job.credits_refunded == True
@pytest.mark.asyncio
async def test_refund_fails_when_no_credits_reserved(self):
"""Refund fails if job has no credits reserved."""
session = AsyncMock()
job = MockJob(credits_reserved=0)
result = await refund_credit(session, job, "No credits")
assert result == False
@pytest.mark.asyncio
async def test_refund_fails_when_already_refunded(self):
"""Refund fails if job was already refunded (idempotency)."""
session = AsyncMock()
job = MockJob(credits_reserved=10, credits_refunded=True)
result = await refund_credit(session, job, "Already done")
assert result == False
@pytest.mark.asyncio
async def test_refund_fails_when_user_not_found(self):
"""Refund fails if user doesn't exist."""
session = AsyncMock()
job = MockJob(user_id="nonexistent", credits_reserved=10, credits_refunded=False)
mock_result = MagicMock()
mock_result.scalar_one_or_none.return_value = None
session.execute = AsyncMock(return_value=mock_result)
result = await refund_credit(session, job, "User gone")
assert result == False
# =============================================================================
# 8. Credit System Tests - handle_job_completion
# =============================================================================
class TestHandleJobCompletion:
"""Test the main credit finalization logic."""
@pytest.mark.asyncio
async def test_completed_job_confirms_credits(self):
"""Completed jobs have credits confirmed."""
session = AsyncMock()
job = MockJob(status="completed", credits_reserved=5)
with patch('services.credit_service.confirm_credit', new_callable=AsyncMock) as mock_confirm:
await handle_job_completion(session, job)
mock_confirm.assert_called_once_with(session, job)
@pytest.mark.asyncio
async def test_failed_job_refundable_error_refunds(self):
"""Failed jobs with refundable errors get refunds."""
session = AsyncMock()
job = MockJob(status="failed", error_message="500 Server Error", credits_reserved=5)
with patch('services.credit_service.refund_credit', new_callable=AsyncMock) as mock_refund:
await handle_job_completion(session, job)
mock_refund.assert_called_once()
@pytest.mark.asyncio
async def test_failed_job_non_refundable_error_keeps_credits(self):
"""Failed jobs with non-refundable errors keep credits consumed."""
session = AsyncMock()
job = MockJob(status="failed", error_message="Content blocked by safety", credits_reserved=5)
with patch('services.credit_service.confirm_credit', new_callable=AsyncMock) as mock_confirm:
await handle_job_completion(session, job)
mock_confirm.assert_called_once_with(session, job)
@pytest.mark.asyncio
async def test_cancelled_job_before_start_refunds(self):
"""Cancelled jobs before started_at get refunds."""
session = AsyncMock()
job = MockJob(status="cancelled", started_at=None, credits_reserved=5)
with patch('services.credit_service.refund_credit', new_callable=AsyncMock) as mock_refund:
await handle_job_completion(session, job)
mock_refund.assert_called_once()
@pytest.mark.asyncio
async def test_cancelled_job_after_start_keeps_credits(self):
"""Cancelled jobs after started_at keep credits consumed."""
session = AsyncMock()
job = MockJob(status="cancelled", started_at=datetime.utcnow(), credits_reserved=5)
with patch('services.credit_service.confirm_credit', new_callable=AsyncMock) as mock_confirm:
await handle_job_completion(session, job)
mock_confirm.assert_called_once_with(session, job)
# =============================================================================
# 9. Credit System Tests - Orphaned Jobs
# =============================================================================
class TestRefundOrphanedJobs:
"""Test orphaned job refund during shutdown."""
@pytest.mark.asyncio
async def test_refund_orphaned_jobs_finds_processing_jobs(self):
"""Shutdown finds and refunds processing jobs with reserved credits."""
session = AsyncMock()
# Mock orphaned jobs
orphaned_job = MockJob(
job_id="orphan-1",
user_id="user-456",
status="processing",
credits_reserved=10,
credits_refunded=False
)
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [orphaned_job]
session.execute = AsyncMock(return_value=mock_result)
session.commit = AsyncMock()
with patch('services.credit_service.refund_credit', new_callable=AsyncMock, return_value=True):
count = await refund_orphaned_jobs(session)
assert count == 1
assert orphaned_job.status == "failed"
assert "shutdown" in orphaned_job.error_message.lower()
@pytest.mark.asyncio
async def test_refund_orphaned_jobs_no_orphans(self):
"""No action when there are no orphaned jobs."""
session = AsyncMock()
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = []
session.execute = AsyncMock(return_value=mock_result)
count = await refund_orphaned_jobs(session)
assert count == 0
# =============================================================================
# 10. GeminiJobProcessor Tests
# =============================================================================
class TestGeminiJobProcessor:
"""Test Gemini-specific job processing."""
@pytest.mark.asyncio
async def test_unknown_job_type_fails_gracefully(self):
"""Unknown job type results in clear error message."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="unknown_type")
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, MagicMock())
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.process(job, session)
assert result.status == "failed"
assert "Unknown job type" in result.error_message
@pytest.mark.asyncio
async def test_check_status_fails_without_third_party_id(self):
"""Status check fails if third_party_id is None."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="video", third_party_id=None)
result = await processor.check_status(job, session)
assert result.status == "failed"
assert "Invalid job state" in result.error_message
@pytest.mark.asyncio
async def test_check_status_fails_for_non_video(self):
"""Status check fails for non-video jobs."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="text", third_party_id="some-id")
result = await processor.check_status(job, session)
assert result.status == "failed"
assert "Invalid job state" in result.error_message
class TestGeminiJobProcessorTextProcessing:
"""Test text job processing."""
@pytest.mark.asyncio
async def test_text_job_completes_successfully(self):
"""Text job completes with output data."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="text", input_data={"prompt": "Hello"})
mock_service = MagicMock()
mock_service.generate_text = AsyncMock(return_value="Hello back!")
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.process(job, session)
assert result.status == "completed"
assert result.output_data == {"text": "Hello back!"}
assert result.completed_at is not None
class TestGeminiJobProcessorVideoProcessing:
"""Test video job processing."""
@pytest.mark.asyncio
async def test_video_job_sets_third_party_id(self):
"""Video job stores operation name for polling."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(
job_type="video",
priority="slow",
input_data={"base64_image": "abc", "prompt": "animate this"}
)
mock_service = MagicMock()
mock_service.start_video_generation = AsyncMock(return_value={
"gemini_operation_name": "operations/12345"
})
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.process(job, session)
assert result.third_party_id == "operations/12345"
assert result.next_process_at is not None
@pytest.mark.asyncio
async def test_video_check_reschedules_if_not_done(self):
"""Pending video job gets new next_process_at."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(
job_type="video",
priority="slow",
third_party_id="operations/12345",
retry_count=0
)
mock_service = MagicMock()
mock_service.check_video_status = AsyncMock(return_value={"done": False})
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.check_status(job, session)
assert result.retry_count == 1
assert result.next_process_at is not None
@pytest.mark.asyncio
async def test_video_download_retry_on_failure(self):
"""Download failures increment retry, not immediate fail."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(
job_type="video",
priority="slow",
third_party_id="operations/12345",
retry_count=0
)
mock_service = MagicMock()
mock_service.check_video_status = AsyncMock(return_value={
"done": True,
"status": "completed",
"video_url": "https://example.com/video.mp4"
})
mock_service.download_video = AsyncMock(side_effect=Exception("Network error"))
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.check_status(job, session)
assert result.retry_count == 1
assert result.status != "failed" # Not failed yet
assert "Download attempt" in result.error_message
@pytest.mark.asyncio
async def test_video_fails_after_5_download_attempts(self):
"""After 5 download retries, job fails."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(
job_type="video",
priority="slow",
third_party_id="operations/12345",
retry_count=5 # Already at limit
)
mock_service = MagicMock()
mock_service.check_video_status = AsyncMock(return_value={
"done": True,
"status": "completed",
"video_url": "https://example.com/video.mp4"
})
mock_service.download_video = AsyncMock(side_effect=Exception("Network error"))
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock):
result = await processor.check_status(job, session)
assert result.status == "failed"
assert "Download failed" in result.error_message
class TestGeminiJobProcessorAPIKeyRotation:
"""Test API key rotation."""
@pytest.mark.asyncio
async def test_api_key_usage_recorded_on_success(self):
"""Usage statistics are updated on success."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="text", input_data={"prompt": "Hello"})
mock_service = MagicMock()
mock_service.generate_text = AsyncMock(return_value="Response")
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock) as mock_record:
await processor.process(job, session)
mock_record.assert_called_once_with(session, 0, True, None)
@pytest.mark.asyncio
async def test_api_key_usage_recorded_on_failure(self):
"""Usage statistics are updated on failure."""
processor = GeminiJobProcessor()
session = AsyncMock()
job = MockJob(job_type="text", input_data={"prompt": "Hello"})
mock_service = MagicMock()
mock_service.generate_text = AsyncMock(side_effect=Exception("API Error"))
with patch.object(processor, '_get_service_with_key', new_callable=AsyncMock) as mock_key:
mock_key.return_value = (0, mock_service)
with patch.object(processor, '_record_usage', new_callable=AsyncMock) as mock_record:
await processor.process(job, session)
mock_record.assert_called_once()
args = mock_record.call_args[0]
assert args[2] == False # success=False
assert "API Error" in args[3] # error_message
# =============================================================================
# 11. Cancel Endpoint Logic Tests
# =============================================================================
class TestCancelEndpoint:
"""Test cancel job endpoint logic."""
def test_only_queued_jobs_can_be_cancelled(self):
"""Verify the cancellation logic - only queued status allowed."""
valid_statuses = ["queued"]
invalid_statuses = ["processing", "completed", "failed", "cancelled"]
for status in valid_statuses:
assert status == "queued"
for status in invalid_statuses:
assert status != "queued"
# =============================================================================
# 12. Worker Pool Lifecycle Tests
# =============================================================================
class TestWorkerPoolLifecycle:
"""Test pool start/stop behavior."""
def test_pool_initialization(self):
"""Pool initializes with correct attributes."""
config = WorkerConfig(fast_workers=2, medium_workers=2, slow_workers=2)
with patch('services.priority_worker_pool.create_async_engine'):
with patch('services.priority_worker_pool.async_sessionmaker'):
pool = PriorityWorkerPool(
database_url="sqlite+aiosqlite:///test.db",
job_model=MockJob,
job_processor=MagicMock(),
config=config
)
assert pool.config == config
assert pool.workers == []
assert pool._running == False
assert "fast" in pool._wake_events
assert "medium" in pool._wake_events
assert "slow" in pool._wake_events
def test_notify_new_job_sets_wake_event(self):
"""notify_new_job() signals the wake event."""
with patch('services.priority_worker_pool.create_async_engine'):
with patch('services.priority_worker_pool.async_sessionmaker'):
pool = PriorityWorkerPool(
database_url="sqlite+aiosqlite:///test.db",
job_model=MockJob,
job_processor=MagicMock()
)
assert pool._wake_events["fast"].is_set() == False
pool.notify_new_job("fast")
assert pool._wake_events["fast"].is_set() == True
def test_notify_new_job_ignores_invalid_priority(self):
"""notify_new_job() handles invalid priority gracefully."""
with patch('services.priority_worker_pool.create_async_engine'):
with patch('services.priority_worker_pool.async_sessionmaker'):
pool = PriorityWorkerPool(
database_url="sqlite+aiosqlite:///test.db",
job_model=MockJob,
job_processor=MagicMock()
)
# Should not raise
pool.notify_new_job("invalid_priority")
# =============================================================================
# 13. Edge Cases - Rare Scenarios
# =============================================================================
class TestRareEdgeCases:
"""Test rare edge cases and boundary conditions."""
def test_zero_workers_configuration(self):
"""Config with zero workers for some tiers."""
config = WorkerConfig(fast_workers=0, medium_workers=0, slow_workers=1)
assert config.fast_workers == 0
assert config.slow_workers == 1
def test_extremely_large_retry_count(self):
"""Job with very high retry count."""
job = MockJob(retry_count=999999)
assert job.retry_count == 999999
def test_job_with_empty_input_data(self):
"""Job with None or empty input_data."""
job1 = MockJob(input_data=None)
job2 = MockJob(input_data={})
assert job1.input_data is None
assert job2.input_data == {}
def test_job_with_very_long_error_message(self):
"""Job with extremely long error message."""
long_error = "Error: " + "x" * 10000
job = MockJob(error_message=long_error)
assert len(job.error_message) == 10007
def test_refundable_error_case_insensitive(self):
"""Error pattern matching is case insensitive."""
assert is_refundable_error("TIMEOUT") == True
assert is_refundable_error("timeout") == True
assert is_refundable_error("TimeOut") == True
def test_multiple_error_patterns_in_message(self):
"""Message with both refundable and non-refundable patterns."""
# REFUNDABLE patterns are checked first, so this should be refundable
mixed_error = "500 Internal Server Error and 400 Bad Request"
# "500" is refundable, checked first
assert is_refundable_error(mixed_error) == True
def test_credits_at_boundaries(self):
"""Test credit operations at boundary values."""
job = MockJob(credits_reserved=0)
assert job.credits_reserved == 0
job.credits_reserved = 2147483647 # Max int32
assert job.credits_reserved == 2147483647
@pytest.mark.asyncio
async def test_reserve_zero_credits(self):
"""Reserving zero credits succeeds (edge case)."""
session = AsyncMock()
user = MockUser(credits=10)
result = await reserve_credit(session, user, amount=0)
# amount=0 means credits < 0 is False, so it should succeed
assert result == True
assert user.credits == 10 # Unchanged
class TestConcurrencyEdgeCases:
"""Test concurrency-related edge cases."""
def test_wake_event_starts_unset(self):
"""Wake events start in unset state."""
event = asyncio.Event()
assert event.is_set() == False
@pytest.mark.asyncio
async def test_wake_event_can_be_set_multiple_times(self):
"""Setting wake event multiple times is idempotent."""
event = asyncio.Event()
event.set()
event.set()
event.set()
assert event.is_set() == True
@pytest.mark.asyncio
async def test_wake_event_clear_then_set(self):
"""Event can be cleared and set again."""
event = asyncio.Event()
event.set()
assert event.is_set() == True
event.clear()
assert event.is_set() == False
event.set()
assert event.is_set() == True
class TestDateTimeEdgeCases:
"""Test datetime-related edge cases."""
def test_job_with_past_next_process_at(self):
"""Job with next_process_at in the past should be picked up."""
past = datetime.utcnow() - timedelta(hours=1)
job = MockJob(next_process_at=past)
assert job.next_process_at < datetime.utcnow()
def test_job_with_future_next_process_at(self):
"""Job with next_process_at in the future should wait."""
future = datetime.utcnow() + timedelta(hours=1)
job = MockJob(next_process_at=future)
assert job.next_process_at > datetime.utcnow()
def test_job_created_at_auto_set(self):
"""Job created_at is automatically set."""
job = MockJob()
assert job.created_at is not None
assert isinstance(job.created_at, datetime)
# =============================================================================
# 14. Error Pattern Coverage Tests
# =============================================================================
class TestErrorPatternCoverage:
"""Ensure all error patterns are tested."""
def test_all_refundable_patterns_detected(self):
"""Every pattern in REFUNDABLE_ERROR_PATTERNS is correctly detected."""
for pattern in REFUNDABLE_ERROR_PATTERNS:
error_msg = f"Error: {pattern} occurred"
assert is_refundable_error(error_msg) == True, f"Pattern '{pattern}' not detected as refundable"
def test_all_non_refundable_patterns_detected(self):
"""Every pattern in NON_REFUNDABLE_ERROR_PATTERNS is correctly detected."""
for pattern in NON_REFUNDABLE_ERROR_PATTERNS:
# Avoid false positives from refundable patterns
error_msg = f"User error: {pattern}"
# Some patterns like "400" might be caught differently, test individually
result = is_refundable_error(error_msg)
# Just ensure the function doesn't crash
assert result in [True, False]
# =============================================================================
# 15. INTEGRATION TESTS - Real Database
# =============================================================================
import os
import tempfile
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy import Column, Integer, String, DateTime, JSON, Text, Boolean, select, update
from sqlalchemy.orm import declarative_base
# Create a test-specific Base for integration tests
IntegrationBase = declarative_base()
class TestJobModel(IntegrationBase):
"""Real job model for integration tests."""
__tablename__ = "test_jobs"
id = Column(Integer, primary_key=True, autoincrement=True)
job_id = Column(String(100), unique=True, index=True, nullable=False)
user_id = Column(String(50), index=True, nullable=False)
job_type = Column(String(20), index=True, nullable=False)
status = Column(String(20), default="queued", index=True)
priority = Column(String(10), default="fast", index=True)
next_process_at = Column(DateTime, nullable=True, index=True)
retry_count = Column(Integer, default=0)
third_party_id = Column(String(255), nullable=True)
input_data = Column(JSON, nullable=True)
output_data = Column(JSON, nullable=True)
error_message = Column(Text, nullable=True)
created_at = Column(DateTime, nullable=False)
started_at = Column(DateTime, nullable=True)
completed_at = Column(DateTime, nullable=True)
credits_reserved = Column(Integer, default=0)
credits_refunded = Column(Boolean, default=False)
class TestUserModel(IntegrationBase):
"""Real user model for integration tests."""
__tablename__ = "test_users"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(String(50), unique=True, index=True, nullable=False)
email = Column(String(255), unique=True, nullable=False)
credits = Column(Integer, default=100)
class TestApiKeyUsageModel(IntegrationBase):
"""Real API key usage model for integration tests."""
__tablename__ = "test_api_key_usage"
id = Column(Integer, primary_key=True, autoincrement=True)
key_index = Column(Integer, unique=True, index=True, nullable=False)
total_requests = Column(Integer, default=0)
success_count = Column(Integer, default=0)
failure_count = Column(Integer, default=0)
last_error = Column(Text, nullable=True)
last_used_at = Column(DateTime, nullable=True)
@pytest.fixture
async def integration_db():
"""Create a real SQLite database for integration tests."""
# Use a temp file for the test database
fd, db_path = tempfile.mkstemp(suffix=".db")
os.close(fd)
db_url = f"sqlite+aiosqlite:///{db_path}"
engine = create_async_engine(db_url, echo=False)
# Create tables
async with engine.begin() as conn:
await conn.run_sync(IntegrationBase.metadata.create_all)
session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
yield {
"engine": engine,
"session_maker": session_maker,
"db_url": db_url,
"db_path": db_path
}
# Cleanup
await engine.dispose()
if os.path.exists(db_path):
os.remove(db_path)
class TestIntegrationRealDatabase:
"""Integration tests using real SQLite database."""
@pytest.mark.asyncio
async def test_create_and_query_job(self, integration_db):
"""Test creating and querying a job in real database."""
session_maker = integration_db["session_maker"]
async with session_maker() as session:
# Create a job
job = TestJobModel(
job_id="int-test-1",
user_id="user-123",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# Query it back
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "int-test-1")
)
fetched = result.scalar_one_or_none()
assert fetched is not None
assert fetched.job_id == "int-test-1"
assert fetched.status == "queued"
@pytest.mark.asyncio
async def test_atomic_update_with_where_clause(self, integration_db):
"""Test atomic UPDATE with WHERE clause (real DB concurrency test)."""
session_maker = integration_db["session_maker"]
# Create a job
async with session_maker() as session:
job = TestJobModel(
job_id="atomic-test-1",
user_id="user-123",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# Simulate Worker 1 claiming the job
async with session_maker() as session1:
stmt = (
update(TestJobModel)
.where(
TestJobModel.job_id == "atomic-test-1",
TestJobModel.status == "queued"
)
.values(status="processing", started_at=datetime.utcnow())
)
result1 = await session1.execute(stmt)
await session1.commit()
# Worker 1 should succeed
assert result1.rowcount == 1
# Simulate Worker 2 trying to claim the same job
async with session_maker() as session2:
stmt = (
update(TestJobModel)
.where(
TestJobModel.job_id == "atomic-test-1",
TestJobModel.status == "queued" # This won't match!
)
.values(status="processing", started_at=datetime.utcnow())
)
result2 = await session2.execute(stmt)
await session2.commit()
# Worker 2 should fail (rowcount = 0)
assert result2.rowcount == 0, "Second worker should not be able to claim!"
@pytest.mark.asyncio
async def test_job_ordering_by_created_at(self, integration_db):
"""Test jobs are queried in FIFO order by created_at."""
session_maker = integration_db["session_maker"]
# Create jobs in non-chronological order
async with session_maker() as session:
job3 = TestJobModel(
job_id="order-3", user_id="user", job_type="text",
status="queued", priority="fast",
created_at=datetime(2024, 1, 1, 12, 0, 0) # Latest
)
job1 = TestJobModel(
job_id="order-1", user_id="user", job_type="text",
status="queued", priority="fast",
created_at=datetime(2024, 1, 1, 10, 0, 0) # Oldest
)
job2 = TestJobModel(
job_id="order-2", user_id="user", job_type="text",
status="queued", priority="fast",
created_at=datetime(2024, 1, 1, 11, 0, 0) # Middle
)
session.add_all([job3, job1, job2])
await session.commit()
# Query with ORDER BY created_at
async with session_maker() as session:
result = await session.execute(
select(TestJobModel)
.where(TestJobModel.status == "queued")
.order_by(TestJobModel.created_at)
.limit(3)
)
jobs = result.scalars().all()
assert len(jobs) == 3
assert jobs[0].job_id == "order-1", "Oldest job should be first"
assert jobs[1].job_id == "order-2"
assert jobs[2].job_id == "order-3"
@pytest.mark.asyncio
async def test_priority_filtering(self, integration_db):
"""Test that priority filter works correctly in real DB."""
session_maker = integration_db["session_maker"]
# Create jobs with different priorities
async with session_maker() as session:
fast_job = TestJobModel(
job_id="prio-fast", user_id="user", job_type="text",
status="queued", priority="fast",
created_at=datetime.utcnow()
)
medium_job = TestJobModel(
job_id="prio-medium", user_id="user", job_type="image",
status="queued", priority="medium",
created_at=datetime.utcnow()
)
slow_job = TestJobModel(
job_id="prio-slow", user_id="user", job_type="video",
status="queued", priority="slow",
created_at=datetime.utcnow()
)
session.add_all([fast_job, medium_job, slow_job])
await session.commit()
# Query only fast priority
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.priority == "fast")
)
fast_jobs = result.scalars().all()
assert len(fast_jobs) == 1
assert fast_jobs[0].job_id == "prio-fast"
class TestIntegrationConcurrency:
"""Test concurrent access patterns."""
@pytest.mark.asyncio
async def test_multiple_workers_different_jobs(self, integration_db):
"""Multiple workers should process different jobs concurrently."""
session_maker = integration_db["session_maker"]
# Create multiple jobs
async with session_maker() as session:
for i in range(5):
job = TestJobModel(
job_id=f"concurrent-{i}",
user_id="user",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow() + timedelta(seconds=i)
)
session.add(job)
await session.commit()
# Simulate 3 workers claiming jobs concurrently
claimed_jobs = []
async def worker_claim(worker_id):
async with session_maker() as session:
# SELECT the oldest unclaimed job
result = await session.execute(
select(TestJobModel)
.where(TestJobModel.status == "queued")
.order_by(TestJobModel.created_at)
.limit(1)
)
job = result.scalar_one_or_none()
if job:
# Try to claim it atomically
stmt = (
update(TestJobModel)
.where(
TestJobModel.job_id == job.job_id,
TestJobModel.status == "queued"
)
.values(status="processing")
)
claim_result = await session.execute(stmt)
await session.commit()
if claim_result.rowcount == 1:
claimed_jobs.append((worker_id, job.job_id))
# Run workers concurrently
await asyncio.gather(
worker_claim(0),
worker_claim(1),
worker_claim(2)
)
# Each worker should have claimed a different job (or failed gracefully)
claimed_job_ids = [job_id for _, job_id in claimed_jobs]
# No duplicates
assert len(claimed_job_ids) == len(set(claimed_job_ids)), "Same job claimed by multiple workers!"
@pytest.mark.asyncio
async def test_next_process_at_prevents_duplicate_status_checks(self, integration_db):
"""next_process_at should prevent multiple workers from checking same job."""
session_maker = integration_db["session_maker"]
# Create a processing job with future next_process_at
async with session_maker() as session:
job = TestJobModel(
job_id="processing-job",
user_id="user",
job_type="video",
status="processing",
priority="slow",
next_process_at=datetime.utcnow() + timedelta(minutes=5), # Future
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# Query should NOT return this job (next_process_at is in future)
async with session_maker() as session:
now = datetime.utcnow()
result = await session.execute(
select(TestJobModel).where(
TestJobModel.status == "processing",
TestJobModel.next_process_at <= now
)
)
jobs = result.scalars().all()
assert len(jobs) == 0, "Job with future next_process_at should not be selected"
class TestIntegrationEndToEnd:
"""End-to-end job lifecycle tests."""
@pytest.mark.asyncio
async def test_job_lifecycle_queued_to_completed(self, integration_db):
"""Test full job lifecycle: queued β processing β completed."""
session_maker = integration_db["session_maker"]
# 1. Create job
async with session_maker() as session:
job = TestJobModel(
job_id="lifecycle-1",
user_id="user-123",
job_type="text",
status="queued",
priority="fast",
input_data={"prompt": "Hello"},
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# 2. Worker claims job (queued β processing)
async with session_maker() as session:
stmt = (
update(TestJobModel)
.where(
TestJobModel.job_id == "lifecycle-1",
TestJobModel.status == "queued"
)
.values(status="processing", started_at=datetime.utcnow())
)
result = await session.execute(stmt)
await session.commit()
assert result.rowcount == 1
# 3. Worker completes job (processing β completed)
async with session_maker() as session:
stmt = (
update(TestJobModel)
.where(TestJobModel.job_id == "lifecycle-1")
.values(
status="completed",
output_data={"response": "Hello back!"},
completed_at=datetime.utcnow()
)
)
await session.execute(stmt)
await session.commit()
# 4. Verify final state
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "lifecycle-1")
)
job = result.scalar_one()
assert job.status == "completed"
assert job.output_data == {"response": "Hello back!"}
assert job.started_at is not None
assert job.completed_at is not None
@pytest.mark.asyncio
async def test_job_lifecycle_queued_to_failed(self, integration_db):
"""Test job failure lifecycle: queued β processing β failed."""
session_maker = integration_db["session_maker"]
# 1. Create job
async with session_maker() as session:
job = TestJobModel(
job_id="lifecycle-fail",
user_id="user-123",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# 2. Start processing
async with session_maker() as session:
stmt = (
update(TestJobModel)
.where(TestJobModel.job_id == "lifecycle-fail")
.values(status="processing", started_at=datetime.utcnow())
)
await session.execute(stmt)
await session.commit()
# 3. Job fails
async with session_maker() as session:
stmt = (
update(TestJobModel)
.where(TestJobModel.job_id == "lifecycle-fail")
.values(
status="failed",
error_message="Content blocked by safety filter",
completed_at=datetime.utcnow()
)
)
await session.execute(stmt)
await session.commit()
# 4. Verify
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "lifecycle-fail")
)
job = result.scalar_one()
assert job.status == "failed"
assert "safety" in job.error_message.lower()
class TestIntegrationCreditSystem:
"""Integration tests for credit system with real database."""
@pytest.mark.asyncio
async def test_credit_reservation_and_refund(self, integration_db):
"""Test credit reserve and refund with real database."""
session_maker = integration_db["session_maker"]
# Create user with credits
async with session_maker() as session:
user = TestUserModel(
user_id="credit-user",
email="test@example.com",
credits=100
)
session.add(user)
await session.commit()
# Reserve credits
async with session_maker() as session:
result = await session.execute(
select(TestUserModel).where(TestUserModel.user_id == "credit-user")
)
user = result.scalar_one()
# Deduct 10 credits
user.credits -= 10
await session.commit()
# Verify deduction
async with session_maker() as session:
result = await session.execute(
select(TestUserModel).where(TestUserModel.user_id == "credit-user")
)
user = result.scalar_one()
assert user.credits == 90
# Refund credits
async with session_maker() as session:
result = await session.execute(
select(TestUserModel).where(TestUserModel.user_id == "credit-user")
)
user = result.scalar_one()
user.credits += 10
await session.commit()
# Verify refund
async with session_maker() as session:
result = await session.execute(
select(TestUserModel).where(TestUserModel.user_id == "credit-user")
)
user = result.scalar_one()
assert user.credits == 100
class TestIntegrationOrphanedJobs:
"""Test orphaned job handling (simulating server crash)."""
@pytest.mark.asyncio
async def test_find_orphaned_processing_jobs(self, integration_db):
"""Find jobs stuck in processing state (simulating crash recovery)."""
session_maker = integration_db["session_maker"]
# Create jobs in various states
async with session_maker() as session:
# This should NOT be found (queued)
job1 = TestJobModel(
job_id="orphan-queued", user_id="user", job_type="text",
status="queued", priority="fast",
credits_reserved=5, created_at=datetime.utcnow()
)
# This SHOULD be found (processing with credits)
job2 = TestJobModel(
job_id="orphan-processing", user_id="user", job_type="video",
status="processing", priority="slow",
credits_reserved=10, credits_refunded=False,
created_at=datetime.utcnow()
)
# This should NOT be found (already refunded)
job3 = TestJobModel(
job_id="orphan-refunded", user_id="user", job_type="video",
status="processing", priority="slow",
credits_reserved=0, credits_refunded=True,
created_at=datetime.utcnow()
)
session.add_all([job1, job2, job3])
await session.commit()
# Query for orphaned jobs (as refund_orphaned_jobs does)
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(
TestJobModel.status == "processing",
TestJobModel.credits_reserved > 0,
TestJobModel.credits_refunded == False
)
)
orphaned = result.scalars().all()
assert len(orphaned) == 1
assert orphaned[0].job_id == "orphan-processing"
@pytest.mark.asyncio
async def test_mark_orphaned_job_as_failed(self, integration_db):
"""Orphaned jobs should be marked as failed during recovery."""
session_maker = integration_db["session_maker"]
# Create orphaned job
async with session_maker() as session:
job = TestJobModel(
job_id="crash-recovery",
user_id="user",
job_type="video",
status="processing",
priority="slow",
credits_reserved=5,
credits_refunded=False,
created_at=datetime.utcnow()
)
session.add(job)
await session.commit()
# Simulate crash recovery - mark as failed
async with session_maker() as session:
stmt = (
update(TestJobModel)
.where(
TestJobModel.job_id == "crash-recovery",
TestJobModel.status == "processing"
)
.values(
status="failed",
error_message="Server shutdown during processing. Credits refunded.",
credits_refunded=True,
credits_reserved=0
)
)
await session.execute(stmt)
await session.commit()
# Verify
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "crash-recovery")
)
job = result.scalar_one()
assert job.status == "failed"
assert job.credits_refunded == True
assert job.credits_reserved == 0
assert "shutdown" in job.error_message.lower()
# =============================================================================
# 16. API Key Manager Integration Tests (REAL, not mocked!)
# =============================================================================
class TestIntegrationApiKeyManager:
"""
Integration tests for api_key_manager.py with REAL database.
These tests verify the double-commit fix actually works.
"""
@pytest.mark.asyncio
async def test_record_usage_does_not_commit_on_its_own(self, integration_db):
"""
Verify record_usage does NOT commit - caller must commit.
This tests the double-commit fix.
"""
session_maker = integration_db["session_maker"]
# Create initial usage record
async with session_maker() as session:
usage = TestApiKeyUsageModel(
key_index=0,
total_requests=0,
success_count=0,
failure_count=0
)
session.add(usage)
await session.commit()
# Now simulate what record_usage does (without commit)
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 0)
)
usage = result.scalar_one()
# Modify like record_usage does
usage.total_requests += 1
usage.success_count += 1
usage.last_used_at = datetime.utcnow()
# DON'T commit - simulating the fixed behavior
# await session.commit() # This is what we removed!
# Session closes without commit - changes should be LOST
# Verify changes were NOT persisted (rolled back)
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 0)
)
usage = result.scalar_one()
assert usage.total_requests == 0, "Changes should NOT persist without commit!"
@pytest.mark.asyncio
async def test_usage_persists_when_caller_commits(self, integration_db):
"""
Verify changes persist when caller commits the transaction.
"""
session_maker = integration_db["session_maker"]
# Create initial usage record
async with session_maker() as session:
usage = TestApiKeyUsageModel(
key_index=1,
total_requests=0,
success_count=0,
failure_count=0
)
session.add(usage)
await session.commit()
# Modify and commit (like _process_job does)
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 1)
)
usage = result.scalar_one()
usage.total_requests += 1
usage.success_count += 1
usage.last_used_at = datetime.utcnow()
# Caller commits (this is correct behavior)
await session.commit()
# Verify changes WERE persisted
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 1)
)
usage = result.scalar_one()
assert usage.total_requests == 1, "Changes should persist when caller commits"
assert usage.success_count == 1
@pytest.mark.asyncio
async def test_least_used_key_selection(self, integration_db):
"""
Test that the least-used key is selected correctly.
"""
session_maker = integration_db["session_maker"]
# Create usage records with different counts
async with session_maker() as session:
# Key 0: 10 requests
usage0 = TestApiKeyUsageModel(key_index=0, total_requests=10)
# Key 1: 5 requests (least used)
usage1 = TestApiKeyUsageModel(key_index=1, total_requests=5)
# Key 2: 15 requests
usage2 = TestApiKeyUsageModel(key_index=2, total_requests=15)
session.add_all([usage0, usage1, usage2])
await session.commit()
# Query for least used (like get_least_used_key does)
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).order_by(TestApiKeyUsageModel.total_requests)
)
usages = result.scalars().all()
assert usages[0].key_index == 1, "Key with least requests should be first"
assert usages[0].total_requests == 5
@pytest.mark.asyncio
async def test_new_key_created_in_same_transaction(self, integration_db):
"""
Test that new key creation happens in same transaction (not committed separately).
"""
session_maker = integration_db["session_maker"]
# Start transaction, add new key, but DON'T commit
async with session_maker() as session:
new_usage = TestApiKeyUsageModel(
key_index=99,
total_requests=0,
success_count=0,
failure_count=0
)
session.add(new_usage)
# No commit - transaction rolls back
# Verify key was NOT created
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 99)
)
usage = result.scalar_one_or_none()
assert usage is None, "Key should not exist without commit"
@pytest.mark.asyncio
async def test_failure_recording(self, integration_db):
"""
Test that failure count and error message are recorded correctly.
"""
session_maker = integration_db["session_maker"]
# Create and modify in same transaction
async with session_maker() as session:
usage = TestApiKeyUsageModel(
key_index=10,
total_requests=0,
success_count=0,
failure_count=0
)
session.add(usage)
# Simulate failure
usage.total_requests += 1
usage.failure_count += 1
usage.last_error = "API rate limit exceeded"[:1000]
usage.last_used_at = datetime.utcnow()
await session.commit()
# Verify
async with session_maker() as session:
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 10)
)
usage = result.scalar_one()
assert usage.total_requests == 1
assert usage.failure_count == 1
assert usage.success_count == 0
assert "rate limit" in usage.last_error.lower()
@pytest.mark.asyncio
async def test_transaction_atomicity_job_and_usage(self, integration_db):
"""
Test that job update and usage recording are atomic.
If we fail before commit, NEITHER should persist.
"""
session_maker = integration_db["session_maker"]
# Create job and usage
async with session_maker() as session:
job = TestJobModel(
job_id="atomic-job",
user_id="user",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow()
)
usage = TestApiKeyUsageModel(
key_index=20,
total_requests=0,
success_count=0,
failure_count=0
)
session.add_all([job, usage])
await session.commit()
# Simulate processing: update both, but crash before commit
async with session_maker() as session:
# Update job
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "atomic-job")
)
job = result.scalar_one()
job.status = "completed"
job.completed_at = datetime.utcnow()
# Update usage
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 20)
)
usage = result.scalar_one()
usage.total_requests += 1
usage.success_count += 1
# CRASH! (no commit - simulating failure)
# Verify NEITHER persisted
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "atomic-job")
)
job = result.scalar_one()
assert job.status == "queued", "Job should still be queued (transaction rolled back)"
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 20)
)
usage = result.scalar_one()
assert usage.total_requests == 0, "Usage should be unchanged (transaction rolled back)"
@pytest.mark.asyncio
async def test_transaction_atomicity_both_persist_on_commit(self, integration_db):
"""
Test that job update and usage recording BOTH persist when we commit.
"""
session_maker = integration_db["session_maker"]
# Create job and usage
async with session_maker() as session:
job = TestJobModel(
job_id="atomic-success",
user_id="user",
job_type="text",
status="queued",
priority="fast",
created_at=datetime.utcnow()
)
usage = TestApiKeyUsageModel(
key_index=21,
total_requests=0,
success_count=0,
failure_count=0
)
session.add_all([job, usage])
await session.commit()
# Process successfully
async with session_maker() as session:
# Update job
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "atomic-success")
)
job = result.scalar_one()
job.status = "completed"
job.completed_at = datetime.utcnow()
# Update usage
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 21)
)
usage = result.scalar_one()
usage.total_requests += 1
usage.success_count += 1
# COMMIT!
await session.commit()
# Verify BOTH persisted
async with session_maker() as session:
result = await session.execute(
select(TestJobModel).where(TestJobModel.job_id == "atomic-success")
)
job = result.scalar_one()
assert job.status == "completed", "Job should be completed"
result = await session.execute(
select(TestApiKeyUsageModel).where(TestApiKeyUsageModel.key_index == 21)
)
usage = result.scalar_one()
assert usage.total_requests == 1, "Usage should be updated"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|