File size: 11,992 Bytes
81e3673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Soak Tests for Cache Stability Under Concurrent Load

Extended duration tests (30 minutes) to validate cache behavior:
- Concurrent operations (10 workers, 10000 ops each)
- Cache consistency (no race conditions, no data loss)
- TTL expiration (entries expire correctly)
- Cache unbounded growth detection

Cache issues detected:
- Race conditions in concurrent get/set operations
- Cache corruption (data loss, incorrect values)
- TTL not working (cache grows unbounded)
- Deadlocks under concurrent load

Tests use ThreadPoolExecutor for realistic concurrent access patterns.
"""

import gc
import pytest
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

from core.governance_cache import GovernanceCache


@pytest.mark.soak
@pytest.mark.timeout(1800)  # 30 minutes
def test_cache_concurrent_operations_30min(
    memory_monitor,
    enable_gc_control,
    soak_test_config
):
    """
    Soak test for cache stability under concurrent operations (30 minutes).

    Validates:
    - Cache remains consistent under concurrent load (10 workers)
    - No race conditions or deadlocks
    - No cache corruption (data loss, incorrect values)
    - Zero cache errors (get returns None after set)

    Test pattern:
    - Run for 30 minutes (1800 seconds)
    - Use ThreadPoolExecutor with 10 workers
    - Each worker performs 10000 cache operations (set + get)
    - Validate cache consistency: if get returns None after set, fail
    - Log worker completions
    - Force GC every 10 iterations

    Duration: 30 minutes
    Workers: 10 concurrent threads
    Operations per worker: 10000 (set + get)
    Failure: Any cache inconsistency detected

    Race conditions detected:
    - Get returns None after set (data loss)
    - Incorrect value returned (corruption)
    - Deadlock (test hangs)
    """
    cache = GovernanceCache(max_size=10000, ttl_seconds=60)
    process = memory_monitor["process"]
    initial_memory = memory_monitor["initial_memory_mb"]

    errors = []
    iterations = 0

    def worker_operations(worker_id: int):
        """
        Worker function for concurrent cache operations.

        Each worker performs 10000 cache operations:
        - Set key with unique worker_id to avoid conflicts
        - Get key immediately after set
        - Validate value is not None (cache consistency check)

        Raises AssertionError if cache inconsistency detected.
        """
        for i in range(10000):
            # Concurrent write with unique key (worker_id avoids conflicts)
            cache.set(
                agent_id=f"agent_{worker_id}_{i}",
                action_type="test_action",
                data={"allowed": True, "maturity_level": "AUTONOMOUS"}
            )

            # Concurrent read
            result = cache.get(
                agent_id=f"agent_{worker_id}_{i}",
                action_type="test_action"
            )

            # Validate cache consistency (get should not return None after set)
            if result is None:
                raise AssertionError(
                    f"Cache inconsistency: key agent_{worker_id}_{i} not found after write"
                )

        return f"Worker {worker_id} completed"

    start_time = time.time()

    # Run concurrent workers for 30 minutes
    while time.time() - start_time < 1800:
        # Submit 10 concurrent workers
        with ThreadPoolExecutor(max_workers=10) as executor:
            futures = [
                executor.submit(worker_operations, worker_id)
                for worker_id in range(10)
            ]

            # Wait for all workers to complete, collect errors
            for future in as_completed(futures):
                try:
                    result = future.result()
                    print(result)
                except AssertionError as e:
                    errors.append(str(e))
                except Exception as e:
                    errors.append(f"Unexpected error: {e}")

        iterations += 1

        # Force garbage collection every 10 iterations
        if iterations % 10 == 0:
            enable_gc_control["collect"]()

        # Log memory growth every 60 iterations
        if iterations % 60 == 0:
            current_memory = process.memory_info().rss / 1024 / 1024
            memory_growth = current_memory - initial_memory

            print(
                f"Iteration {iterations}: "
                f"Memory growth = {memory_growth:.2f}MB, "
                f"Errors = {len(errors)}"
            )

            # Fail fast if memory growth > 500MB
            if memory_growth > soak_test_config["fail_fast_threshold_mb"]:
                pytest.fail(
                    f"FAIL-FAST: Memory leak detected - {memory_growth:.2f}MB growth "
                    f"(threshold: {soak_test_config['fail_fast_threshold_mb']}MB)"
                )

    # Final validation: zero cache errors
    assert len(errors) == 0, (
        f"Cache inconsistencies detected: {len(errors)} errors\n"
        f"Errors: {errors[:5]}"  # Show first 5 errors
    )

    print(f"\n✅ Soak test complete: {iterations} iterations, zero cache errors")


@pytest.mark.soak
@pytest.mark.timeout(900)  # 15 minutes
def test_cache_ttl_expiration_15min(memory_monitor, enable_gc_control):
    """
    Soak test for cache TTL expiration (15 minutes).

    Validates:
    - Cache entries expire after TTL (60 seconds)
    - Cache doesn't grow unbounded (TTL prevents memory leaks)
    - Expired entries are not returned (get returns None)

    Test pattern:
    - Create cache with TTL=60 seconds
    - Add 1000 entries per iteration
    - Validate entries exist immediately after adding
    - Wait 70 seconds, validate entries are gone (expired)
    - Repeat for 15 minutes
    - Monitor memory usage

    Duration: 15 minutes
    TTL: 60 seconds
    Validation: Entries expire correctly, cache doesn't grow unbounded

    Purpose: Detect TTL bugs that cause unbounded cache growth.
    """
    cache = GovernanceCache(max_size=10000, ttl_seconds=60)
    process = memory_monitor["process"]
    initial_memory = memory_monitor["initial_memory_mb"]

    iterations = 0

    start_time = time.time()

    # Run TTL validation for 15 minutes
    while time.time() - start_time < 900:
        # Add 1000 entries
        for i in range(1000):
            cache.set(
                agent_id=f"agent_{iterations}_{i}",
                action_type="test_action",
                data={"allowed": True, "maturity_level": "AUTONOMOUS"}
            )

        # Validate entries exist immediately
        for i in range(100):  # Check 100 random entries
            result = cache.get(
                agent_id=f"agent_{iterations}_{i}",
                action_type="test_action"
            )
            assert result is not None, f"Entry not found immediately after set: agent_{iterations}_{i}"

        print(f"Iteration {iterations}: Added 1000 entries, validated existence")

        # Wait 70 seconds (TTL=60, +10 buffer)
        time.sleep(70)

        # Validate entries are expired (get returns None)
        expired_count = 0
        for i in range(100):  # Check 100 random entries
            result = cache.get(
                agent_id=f"agent_{iterations}_{i}",
                action_type="test_action"
            )
            if result is None:
                expired_count += 1

        # All entries should be expired
        assert expired_count == 100, (
            f"TTL expiration failed: {expired_count}/100 entries expired "
            f"(expected: 100)"
        )

        print(f"Iteration {iterations}: All entries expired correctly (100/100)")

        iterations += 1

        # Force garbage collection
        enable_gc_control["collect"]()

        # Log memory growth
        current_memory = process.memory_info().rss / 1024 / 1024
        memory_growth = current_memory - initial_memory

        print(f"Iteration {iterations}: Memory growth = {memory_growth:.2f}MB")

        # Fail fast if memory growth > 500MB (indicates TTL not working)
        if memory_growth > 500:
            pytest.fail(
                f"TTL expiration not working: cache grew {memory_growth:.2f}MB "
                f"(entries should be expiring)"
            )

    # Final validation: memory growth should be reasonable (< 200MB for 15min)
    final_memory = process.memory_info().rss / 1024 / 1024
    memory_growth = final_memory - initial_memory

    assert memory_growth < 200, (
        f"Cache grew unbounded: {memory_growth:.2f}MB growth over 15 minutes "
        f"(TTL expiration may not be working)"
    )

    print(f"\n✅ Soak test complete: {iterations} iterations, TTL expiration working correctly")


@pytest.mark.soak
@pytest.mark.timeout(1800)  # 30 minutes
def test_cache_lru_eviction_stability_30min(memory_monitor, enable_gc_control):
    """
    Soak test for cache LRU eviction stability (30 minutes).

    Validates:
    - LRU eviction works correctly (cache doesn't exceed max_size)
    - Evicted entries are not returned (get returns None)
    - Cache performance remains stable under load

    Test pattern:
    - Create cache with max_size=1000
    - Add 2000 entries (exceeds max_size, triggers LRU eviction)
    - Validate cache size <= max_size
    - Verify oldest entries are evicted (get returns None)
    - Repeat for 30 minutes

    Duration: 30 minutes
    Max size: 1000 entries
    Validation: LRU eviction works, cache size stable

    Purpose: Detect LRU eviction bugs that cause unbounded cache growth.
    """
    cache = GovernanceCache(max_size=1000, ttl_seconds=60)
    process = memory_monitor["process"]
    initial_memory = memory_monitor["initial_memory_mb"]

    iterations = 0

    start_time = time.time()

    # Run LRU eviction validation for 30 minutes
    while time.time() - start_time < 1800:
        # Add 2000 entries (exceeds max_size=1000)
        for i in range(2000):
            cache.set(
                agent_id=f"agent_{iterations}_{i}",
                action_type="test_action",
                data={"allowed": True, "maturity_level": "AUTONOMOUS"}
            )

        # Validate oldest entries are evicted (first 1000 should be gone)
        evicted_count = 0
        for i in range(1000):  # Check oldest 1000 entries
            result = cache.get(
                agent_id=f"agent_{iterations}_{i}",
                action_type="test_action"
            )
            if result is None:
                evicted_count += 1

        # At least some entries should be evicted (LRU working)
        # Not all 1000 will be evicted (TTL may not have expired yet)
        # But at least 500 should be evicted due to LRU
        assert evicted_count >= 500, (
            f"LRU eviction not working: only {evicted_count}/1000 entries evicted "
            f"(expected: >= 500)"
        )

        print(f"Iteration {iterations}: LRU eviction working ({evicted_count}/1000 evicted)")

        iterations += 1

        # Force garbage collection
        enable_gc_control["collect"]()

        # Log memory growth
        current_memory = process.memory_info().rss / 1024 / 1024
        memory_growth = current_memory - initial_memory

        print(f"Iteration {iterations}: Memory growth = {memory_growth:.2f}MB")

        # Fail fast if memory growth > 500MB (indicates LRU not working)
        if memory_growth > 500:
            pytest.fail(
                f"LRU eviction not working: cache grew {memory_growth:.2f}MB "
                f"(entries should be evicted)"
            )

    # Final validation: memory growth should be reasonable
    final_memory = process.memory_info().rss / 1024 / 1024
    memory_growth = final_memory - initial_memory

    assert memory_growth < 200, (
        f"Cache grew unbounded: {memory_growth:.2f}MB growth over 30 minutes "
        f"(LRU eviction may not be working)"
    )

    print(f"\n✅ Soak test complete: {iterations} iterations, LRU eviction working correctly")