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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
"""
Database Connection Loss Tests

Test how the system handles database connection failures:
- Connection pool exhaustion
- Connection timeout
- Too many connections
- Connection closed mid-transaction (query, commit)
- Deadlock detection and retry
- Connection recovery after failure

Note: SQLite vs PostgreSQL differences documented where relevant.
"""

import pytest
import asyncio
from unittest.mock import patch, MagicMock, AsyncMock
from sqlalchemy.exc import OperationalError, DBAPIError, IntegrityError
from sqlalchemy.orm import Session
from sqlalchemy import text


class TestConnectionPoolExhaustion:
    """Test connection pool exhaustion handling."""

    def test_connection_pool_exhausted(self, mock_pool_exhausted):
        """
        FAILURE MODE: Connection pool exhausted (all connections in use).
        EXPECTED: Timeout error raised, pool recovers, no crash.
        """
        from core.database import SessionLocal

        # Mock connection pool to raise timeout
        # Note: SessionLocal() doesn't raise error until operation is executed
        with mock_pool_exhausted():
            with pytest.raises(OperationalError) as exc_info:
                db = SessionLocal()
                db.execute(text("SELECT 1"))

            # Should mention pool exhaustion or timeout
            error_msg = str(exc_info.value).lower()
            assert "pool" in error_msg or "exhausted" in error_msg or "connection" in error_msg

    def test_connection_timeout(self):
        """
        FAILURE MODE: Database connection times out.
        EXPECTED: OperationalError raised, timeout message present.
        """
        from core.database import SessionLocal

        # Mock connection to timeout
        # Note: Error occurs during query execution, not session creation
        with patch('core.database.engine.connect', side_effect=OperationalError("connection timeout", None, None)):
            with pytest.raises(OperationalError) as exc_info:
                db = SessionLocal()
                db.execute(text("SELECT 1"))

            # Should mention timeout
            assert "timeout" in str(exc_info.value).lower() or "connection" in str(exc_info.value).lower()

    def test_too_many_connections(self):
        """
        FAILURE MODE: Database returns "too many connections" error.
        EXPECTED: Error handled gracefully, system doesn't crash.
        """
        from core.database import SessionLocal

        # Mock too many connections error
        # Note: Error occurs during query execution
        with patch('core.database.engine.connect', side_effect=OperationalError("sorry, too many clients already", None, None)):
            with pytest.raises(OperationalError) as exc_info:
                db = SessionLocal()
                db.execute(text("SELECT 1"))

            # Should mention too many clients/connections
            error_msg = str(exc_info.value).lower()
            assert "too many" in error_msg or "client" in error_msg


class TestConnectionClosedMidTransaction:
    """Test connection closed during transaction handling."""

    def test_connection_closed_during_query(self):
        """
        FAILURE MODE: Connection closed during query execution.
        EXPECTED: DBAPIError raised, rollback called, resources released.
        """
        from core.database import get_db_session

        # Mock session.execute to raise connection closed error
        with patch('sqlalchemy.orm.Session.execute', side_effect=DBAPIError("connection closed", None, None)):
            with pytest.raises((DBAPIError, OperationalError)):
                with get_db_session() as db:
                    db.execute("SELECT * FROM agents")

            # Note: Verifying rollback called depends on implementation
            # The key is that the error is caught and handled

    def test_connection_closed_during_commit(self):
        """
        FAILURE MODE: Connection closed during commit.
        EXPECTED: DBAPIError raised, rollback executed, no partial commit.
        """
        from core.database import get_db_session

        # Mock session.commit to raise connection closed error
        with patch('sqlalchemy.orm.Session.commit', side_effect=DBAPIError("connection closed", None, None)):
            with pytest.raises((DBAPIError, OperationalError)) as exc_info:
                with get_db_session() as db:
                    # Simulate transaction
                    pass
                    db.commit()

            # Should mention connection
            assert "connection" in str(exc_info.value).lower()

            # Verify rollback was called (if implementation supports it)
            # This prevents partial commits

    def test_connection_leak(self):
        """
        FAILURE MODE: Many connections not closed properly (connection leak).
        EXPECTED: Pool recovers, new connections available after cleanup.
        """
        from core.database import SessionLocal

        # Simulate connection leak by creating many sessions
        sessions = []
        try:
            for i in range(10):
                db = SessionLocal()
                sessions.append(db)
        except OperationalError as e:
            # Pool exhausted is expected
            assert "pool" in str(e).lower() or "connection" in str(e).lower()
        finally:
            # Clean up sessions
            for db in sessions:
                try:
                    db.close()
                except:
                    pass

        # After cleanup, should be able to create new connection
        db = SessionLocal()
        assert db is not None
        db.close()


class TestDeadlockHandling:
    """Test deadlock detection and handling."""

    def test_database_deadlock(self, mock_deadlock):
        """
        FAILURE MODE: Database deadlock detected during commit.
        EXPECTED: OperationalError raised, deadlock mentioned, retry attempted or error raised.
        """
        from core.database import get_db_session

        # Mock session.commit to raise deadlock error
        with mock_deadlock():
            with pytest.raises(OperationalError) as exc_info:
                with get_db_session() as db:
                    # Simulate transaction
                    pass
                    db.commit()

            # Should mention deadlock
            assert "deadlock" in str(exc_info.value).lower()

    def test_deadlock_retry_succeeds(self):
        """
        FAILURE MODE: Deadlock occurs, retry succeeds on second attempt.
        EXPECTED: Retry logic works, transaction completes after retry.
        """
        from core.database import get_db_session

        # Mock commit to fail once, then succeed
        call_count = [0]
        def mock_commit_deadlock_then_success(*args, **kwargs):
            call_count[0] += 1
            if call_count[0] == 1:
                raise OperationalError("deadlock detected", None, None)
            # Second attempt succeeds
            return None

        with patch('sqlalchemy.orm.Session.commit', side_effect=mock_commit_deadlock_then_success):
            with get_db_session() as db:
                # First commit fails with deadlock
                with pytest.raises(OperationalError):
                    db.commit()

                # Note: Actual retry logic depends on implementation
                # This test documents the expected behavior

    def test_concurrent_update_deadlock(self):
        """
        FAILURE MODE: Concurrent updates to same row cause deadlock.
        EXPECTED: Deadlock detected, one transaction succeeds, one fails.

        Note: SQLite serializes writes (one-at-a-time), so deadlocks are rare.
          PostgreSQL with MVCC and SERIALIZABLE isolation can have true deadlocks.
        """
        from core.database import get_db_session
        import threading
        import time

        # Simulate concurrent updates
        results = {"success": 0, "failure": 0}
        errors = []

        def update_thread():
            """Thread that attempts concurrent update."""
            try:
                with get_db_session() as db:
                    # Simulate update
                    time.sleep(0.01)
                    # In SQLite, this would serialize, not deadlock
                    # In PostgreSQL with proper isolation, this could deadlock
                    pass
                    db.commit()
                results["success"] += 1
            except OperationalError as e:
                if "deadlock" in str(e).lower():
                    results["failure"] += 1
                    errors.append(str(e))
                else:
                    # Other operational errors are acceptable
                    results["success"] += 1

        # Run concurrent updates
        threads = []
        for _ in range(2):
            t = threading.Thread(target=update_thread)
            threads.append(t)
            t.start()

        for t in threads:
            t.join(timeout=5.0)

        # At least one should succeed (SQLite serialization)
        # Or both fail with deadlock (PostgreSQL with concurrent writes)
        assert results["success"] + results["failure"] == 2


class TestConnectionRecovery:
    """Test system recovery after connection failures."""

    def test_connection_recovers_after_timeout(self):
        """
        FAILURE MODE: Connection times out, then new connection succeeds.
        EXPECTED: System recovers, new connections work after timeout.
        """
        from core.database import SessionLocal

        # Mock timeout on first attempt, success on second
        call_count = [0]
        def mock_connect_then_success(*args, **kwargs):
            call_count[0] += 1
            if call_count[0] == 1:
                raise OperationalError("connection timeout", None, None)
            # Second attempt succeeds
            mock_conn = MagicMock()
            return mock_conn

        with patch('core.database.engine.connect', side_effect=mock_connect_then_success):
            # First attempt times out
            with pytest.raises(OperationalError):
                db = SessionLocal()
                db.execute("SELECT 1")

            # Note: Recovery depends on implementation retry logic
            # This test documents expected behavior

    def test_pool_recovers_after_exhaustion(self):
        """
        FAILURE MODE: Pool exhausted, then connections released.
        EXPECTED: Pool returns to healthy state, new connections available.
        """
        from core.database import SessionLocal

        # Simulate pool exhaustion
        connections = []
        try:
            # Create connections until pool exhausted
            for i in range(100):  # Exceed typical pool size
                db = SessionLocal()
                connections.append(db)
        except OperationalError as e:
            # Pool exhausted
            assert "pool" in str(e).lower() or "exhausted" in str(e).lower()
        finally:
            # Release all connections
            for db in connections:
                try:
                    db.close()
                except:
                    pass

        # Pool should recover, new connection available
        db = SessionLocal()
        assert db is not None
        db.close()

    def test_deadlock_does_not_hang_system(self):
        """
        FAILURE MODE: Deadlock occurs, verify system doesn't hang indefinitely.
        EXPECTED: Deadlock detected and raised, not infinite hang.
        """
        from core.database import get_db_session
        import threading
        import time

        # Mock deadlock that doesn't resolve
        with patch('sqlalchemy.orm.Session.commit', side_effect=OperationalError("deadlock detected", None, None)):
            hang_detected = [False]
            result = [None]

            def transaction_with_timeout():
                def deadlock_transaction():
                    start = time.time()
                    try:
                        with get_db_session() as db:
                            db.commit()
                    except OperationalError:
                        pass  # Expected
                    elapsed = time.time() - start
                    if elapsed > 5.0:  # 5 second timeout
                        hang_detected[0] = True
                    result[0] = elapsed

                thread = threading.Thread(target=deadlock_transaction)
                thread.start()
                thread.join(timeout=5.0)

                if thread.is_alive():
                    hang_detected[0] = True

            transaction_with_timeout()

            # Should not hang (deadlock should be raised quickly)
            assert not hang_detected[0], "Deadlock caused system to hang"


class TestDatabaseFailureEdgeCases:
    """Test edge cases in database failure handling."""

    def test_connection_closed_during_rollback(self):
        """
        FAILURE MODE: Connection closes during rollback.
        EXPECTED: Error handled gracefully, no crash.
        """
        from core.database import get_db_session

        # Mock rollback to raise connection error
        with patch('sqlalchemy.orm.Session.rollback', side_effect=DBAPIError("connection closed", None, None)):
            with pytest.raises((DBAPIError, OperationalError)):
                with get_db_session() as db:
                    # Trigger rollback
                    raise IntegrityError("constraint violation", None, None)

    def test_multiple_connection_failures_in_sequence(self):
        """
        FAILURE MODE: Multiple connection failures occur in sequence.
        EXPECTED: Each failure handled independently, no cascading crashes.
        """
        from core.database import SessionLocal

        # Mock sequence of connection failures
        call_count = [0]
        def mock_sequence_of_failures(*args, **kwargs):
            call_count[0] += 1
            errors = [
                OperationalError("connection timeout", None, None),
                OperationalError("pool exhausted", None, None),
                OperationalError("too many connections", None, None),
            ]
            if call_count[0] <= len(errors):
                raise errors[call_count[0] - 1]
            # Eventually succeed
            return MagicMock()

        with patch('core.database.engine.connect', side_effect=mock_sequence_of_failures):
            # First three attempts fail
            for i in range(3):
                with pytest.raises(OperationalError):
                    db = SessionLocal()
                    db.execute("SELECT 1")

            # Fourth attempt might succeed (depends on retry logic)

    def test_database_unreachable(self):
        """
        FAILURE MODE: Database server completely unreachable.
        EXPECTED: Connection error raised, graceful degradation.
        """
        from core.database import SessionLocal

        # Mock database unreachable
        with patch('core.database.engine.connect', side_effect=OperationalError("could not connect to server: Connection refused", None, None)):
            with pytest.raises(OperationalError) as exc_info:
                db = SessionLocal()
                db.execute("SELECT 1")

            # Should mention connection refused or unreachable
            error_msg = str(exc_info.value).lower()
            assert "connection" in error_msg or "refused" in error_msg or "unreachable" in error_msg

    def test_database_connection_during_migration(self):
        """
        FAILURE MODE: Connection attempted during schema migration.
        EXPECTED: Error handled, clear message about migration in progress.
        """
        from core.database import SessionLocal

        # Mock migration in progress error
        with patch('core.database.engine.connect', side_effect=OperationalError("database is locked (migration in progress)", None, None)):
            with pytest.raises(OperationalError) as exc_info:
                db = SessionLocal()
                db.execute("SELECT 1")

            # Should mention locked or migration
            error_msg = str(exc_info.value).lower()
            assert "locked" in error_msg or "migration" in error_msg


class TestSQLitevsPostgreSQL:
    """Test differences between SQLite and PostgreSQL behavior."""

    def test_sqlite_write_serialization(self):
        """
        SQLite serializes writes (one transaction at a time).
        PostgreSQL allows concurrent writes with MVCC.

        This test documents SQLite behavior.
        """
        from core.database import get_db_session
        import threading

        # SQLite will serialize these writes
        results = []

        def write_thread(value):
            try:
                with get_db_session() as db:
                    # SQLite: writes are serialized
                    # PostgreSQL: writes can be concurrent
                    pass
                    db.commit()
                results.append("success")
            except Exception as e:
                results.append(f"error: {e}")

        threads = []
        for i in range(3):
            t = threading.Thread(target=write_thread, args=(i,))
            threads.append(t)
            t.start()

        for t in threads:
            t.join(timeout=5.0)

        # All should complete (SQLite serializes)
        assert len(results) == 3

    def test_postgresql_mvcc_isolation(self):
        """
        Test PostgreSQL MVCC behavior (documented, not tested).

        PostgreSQL uses MVCC (Multi-Version Concurrency Control):
        - Readers don't block writers
        - Writers don't block readers
        - SERIALIZABLE isolation prevents phantom reads

        SQLite behavior:
        - Readers can block writers (locking)
        - Writes are serialized
        - No true MVCC

        This test documents the expected difference.
        """
        # Note: This is a documentation test
        # Actual PostgreSQL testing would require PostgreSQL connection
        pass