askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_user_execution_fairness.py · 3.9 KB · 118 lines
| 1 | """One user's flood must not take the shared execution pool from everyone. |
| 2 | |
| 3 | The sandbox simulation (2026-12-01) ran 40 slow requests as one user: 32 got |
| 4 | "Server is busy" and so did a different user's object. A per-user cap makes the |
| 5 | over-limit user the only one refused, with 429 and Retry-After. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import pytest |
| 10 | |
| 11 | import object_sandbox |
| 12 | import object_server |
| 13 | from runtime_modes import configure, write_object |
| 14 | from test_object_server import auth_headers, claim_limit_slot, create_identity_session, enable_admin_token, session_headers, request as _request |
| 15 | |
| 16 | |
| 17 | TOKENS = {} |
| 18 | |
| 19 | |
| 20 | def request(path, user=None): |
| 21 | """As a signed-in customer (user id given), or as the operator.""" |
| 22 | if user is None: |
| 23 | return _request(path, headers=auth_headers()) |
| 24 | return _request(path, headers=session_headers(TOKENS[user])) |
| 25 | |
| 26 | |
| 27 | @pytest.fixture |
| 28 | def two_users(tmp_path, monkeypatch): |
| 29 | _data_dir, root = configure(monkeypatch, tmp_path) |
| 30 | source = "def GET(request):\n return {'ok': True}\n" |
| 31 | write_object(root, "u_1_hello", source) |
| 32 | write_object(root, "u_2_hello", source) |
| 33 | write_object(root, "basics_ping", source) |
| 34 | enable_admin_token(monkeypatch) |
| 35 | for uid in (1, 2): |
| 36 | TOKENS[uid], _ = create_identity_session({"user_id": str(uid)}) |
| 37 | monkeypatch.setenv(object_sandbox.ISOLATION_ENV, object_sandbox.UNSAFE_DEV) |
| 38 | monkeypatch.setenv(object_server.MAX_CONCURRENT_EXECUTIONS_PER_USER_ENV, "2") |
| 39 | yield |
| 40 | object_sandbox.shutdown() |
| 41 | |
| 42 | |
| 43 | def _saturate(uid, n=2): |
| 44 | return [ |
| 45 | claim_limit_slot_keyed(str(uid), n) for _ in range(n) |
| 46 | ] |
| 47 | |
| 48 | |
| 49 | def claim_limit_slot_keyed(key, limit): |
| 50 | token = object_server._user_execution_limiter.try_acquire(key, limit) |
| 51 | assert token is not None |
| 52 | return token |
| 53 | |
| 54 | |
| 55 | def test_over_limit_user_gets_429_with_retry_after(two_users): |
| 56 | tokens = _saturate(1) |
| 57 | try: |
| 58 | status, headers, payload = request("/objects/u_1_hello", 1) |
| 59 | finally: |
| 60 | for t in tokens: |
| 61 | t.release() |
| 62 | assert status == 429 |
| 63 | assert headers[b"retry-after"] == b"1" |
| 64 | assert payload["limit"] == "user_object_executions" |
| 65 | |
| 66 | |
| 67 | def test_other_user_and_system_objects_still_run_when_one_user_saturates(two_users): |
| 68 | tokens = _saturate(1) |
| 69 | try: |
| 70 | assert request("/objects/u_2_hello", 2)[0] == 200 |
| 71 | assert request("/objects/basics_ping")[0] == 200 |
| 72 | finally: |
| 73 | for t in tokens: |
| 74 | t.release() |
| 75 | |
| 76 | |
| 77 | def test_slot_is_released_after_execution(two_users): |
| 78 | for _ in range(5): |
| 79 | assert request("/objects/u_1_hello", 1)[0] == 200 |
| 80 | assert object_server._user_execution_limiter.snapshot() == {} |
| 81 | |
| 82 | |
| 83 | def test_user_cap_does_not_hold_a_global_slot_when_refused(two_users, monkeypatch): |
| 84 | monkeypatch.setenv(object_server.MAX_CONCURRENT_EXECUTIONS_ENV, "1") |
| 85 | tokens = _saturate(1) |
| 86 | try: |
| 87 | assert request("/objects/u_1_hello", 1)[0] == 429 |
| 88 | # the pool's only slot is still free for another user |
| 89 | assert request("/objects/u_2_hello", 2)[0] == 200 |
| 90 | finally: |
| 91 | for t in tokens: |
| 92 | t.release() |
| 93 | |
| 94 | |
| 95 | def test_global_busy_releases_the_user_slot(two_users, monkeypatch): |
| 96 | monkeypatch.setenv(object_server.MAX_CONCURRENT_EXECUTIONS_ENV, "1") |
| 97 | token = claim_limit_slot(object_server._execution_limiter, 1) |
| 98 | try: |
| 99 | assert request("/objects/u_1_hello", 1)[0] == 503 |
| 100 | finally: |
| 101 | token.release() |
| 102 | assert object_server._user_execution_limiter.snapshot() == {} |
| 103 | |
| 104 | |
| 105 | def test_per_user_cap_zero_disables(two_users, monkeypatch): |
| 106 | monkeypatch.setenv(object_server.MAX_CONCURRENT_EXECUTIONS_PER_USER_ENV, "0") |
| 107 | tokens = [object_server._user_execution_limiter.try_acquire("1", 0) for _ in range(5)] |
| 108 | assert all(t is not None for t in tokens) |
| 109 | assert request("/objects/u_1_hello", 1)[0] == 200 |
| 110 | |
| 111 | |
| 112 | def test_admin_is_not_capped_per_user(two_users): |
| 113 | tokens = _saturate(1) |
| 114 | try: |
| 115 | assert request("/objects/u_1_hello")[0] == 200 # the operator |
| 116 | finally: |
| 117 | for t in tokens: |
| 118 | t.release() |