|
19 | 19 |
|
20 | 20 |
|
21 | 21 | class _RecordingCursor: |
22 | | - def __init__(self): |
| 22 | + def __init__(self, *, lock_acquired=True): |
23 | 23 | self.calls = [] |
24 | 24 | self.rowcount = 1 |
| 25 | + self.lock_acquired = lock_acquired |
25 | 26 |
|
26 | 27 | def execute(self, sql, params=None): |
27 | 28 | self.calls.append((sql, params)) |
28 | 29 |
|
| 30 | + def fetchone(self): |
| 31 | + return {"acquired": self.lock_acquired} |
| 32 | + |
29 | 33 | def close(self): |
30 | 34 | return None |
31 | 35 |
|
@@ -58,20 +62,39 @@ def test_account_position_sync_serializes_each_credential_bucket(monkeypatch): |
58 | 62 | db = _RecordingDb(cursor) |
59 | 63 | monkeypatch.setattr(account_positions, "get_db_connection", lambda: _RecordingContext(db)) |
60 | 64 |
|
61 | | - account_positions.sync_account_positions( |
| 65 | + synced = account_positions.sync_account_positions( |
62 | 66 | user_id=7, |
63 | 67 | credential_id=33, |
64 | 68 | exchange_id="gate", |
65 | 69 | market_type="swap", |
66 | 70 | legs=[], |
67 | 71 | ) |
68 | 72 |
|
69 | | - assert "pg_advisory_xact_lock" in cursor.calls[0][0] |
| 73 | + assert synced is True |
| 74 | + assert "pg_try_advisory_xact_lock" in cursor.calls[0][0] |
70 | 75 | assert cursor.calls[0][1] == ("account-position-sync:7:33:swap",) |
71 | 76 | assert "DELETE FROM qd_account_positions" in cursor.calls[1][0] |
72 | 77 | assert db.committed is True |
73 | 78 |
|
74 | 79 |
|
| 80 | +def test_account_position_sync_skips_instead_of_waiting_for_busy_bucket(monkeypatch): |
| 81 | + cursor = _RecordingCursor(lock_acquired=False) |
| 82 | + db = _RecordingDb(cursor) |
| 83 | + monkeypatch.setattr(account_positions, "get_db_connection", lambda: _RecordingContext(db)) |
| 84 | + |
| 85 | + synced = account_positions.sync_account_positions( |
| 86 | + user_id=7, |
| 87 | + credential_id=33, |
| 88 | + exchange_id="gate", |
| 89 | + market_type="swap", |
| 90 | + legs=[], |
| 91 | + ) |
| 92 | + |
| 93 | + assert synced is False |
| 94 | + assert len(cursor.calls) == 1 |
| 95 | + assert db.committed is False |
| 96 | + |
| 97 | + |
75 | 98 | def test_fresh_snapshot_clears_only_older_recorded_drift(monkeypatch): |
76 | 99 | cursor = _RecordingCursor() |
77 | 100 | db = _RecordingDb(cursor) |
|
0 commit comments