-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_async_core.py
More file actions
1057 lines (789 loc) · 31 KB
/
test_async_core.py
File metadata and controls
1057 lines (789 loc) · 31 KB
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
"""Tests for async/coroutine support in Cachier."""
import asyncio
from datetime import timedelta
from time import sleep, time
import pytest
from cachier import cachier
# =============================================================================
# Basic Async Caching Tests
# =============================================================================
class TestBasicAsyncCaching:
"""Tests for basic async caching functionality."""
@pytest.mark.memory
@pytest.mark.asyncio
async def test_memory(self):
"""Test basic async caching with memory backend."""
@cachier(backend="memory")
async def async_func(x):
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
# First call should execute
result1 = await async_func(5)
assert result1 == 10
# Second call should use cache
start = time()
result2 = await async_func(5)
end = time()
assert result2 == 10
assert end - start < 0.05 # Should be much faster than 0.1s
async_func.clear_cache()
@pytest.mark.pickle
@pytest.mark.asyncio
async def test_pickle(self):
"""Test basic async caching with pickle backend."""
@cachier(backend="pickle")
async def async_func(x):
await asyncio.sleep(0.1)
return x * 3
async_func.clear_cache()
# First call should execute
result1 = await async_func(7)
assert result1 == 21
# Second call should use cache
start = time()
result2 = await async_func(7)
end = time()
assert result2 == 21
assert end - start < 0.05 # Should be much faster than 0.1s
async_func.clear_cache()
# =============================================================================
# Stale Cache Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestStaleCache:
"""Tests for stale_after and next_time functionality."""
async def test_recalculates_after_expiry(self):
"""Test that stale_after causes recalculation after expiry."""
call_count = 0
@cachier(
backend="memory",
stale_after=timedelta(seconds=0.5),
next_time=False,
)
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Wait for cache to become stale
await asyncio.sleep(0.6)
# Second call - should recalculate
result2 = await async_func(5)
assert result2 == 10
assert call_count == 2
async_func.clear_cache()
async def test_uses_cache_before_expiry(self):
"""Test that cache is used before stale_after expiry."""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(seconds=1), next_time=False)
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Second call - should use cache (no additional call)
previous_call_count = call_count
result2 = await async_func(5)
assert result2 == 10
assert call_count == previous_call_count # Verify cache was used
async_func.clear_cache()
async def test_next_time_returns_stale_and_updates_background(self):
"""Test next_time=True returns stale value and updates in bg."""
call_count = 0
@cachier(
backend="memory",
stale_after=timedelta(seconds=0.5),
next_time=True,
)
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return call_count * 10
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Wait for cache to become stale
await asyncio.sleep(0.6)
# Second call - should return stale value and trigger background update
result2 = await async_func(5)
assert result2 == 10 # Still returns old value
# Wait for background calculation to complete
await asyncio.sleep(0.5)
# Third call - should return new value
result3 = await async_func(5)
assert result3 == 20 # New value from background calculation
async_func.clear_cache()
# =============================================================================
# Cache Control Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestCacheControl:
"""Tests for cache control parameters - skip_cache & overwrite_cache."""
async def test_skip_cache(self):
"""Test async caching with cachier__skip_cache parameter."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return call_count * 10
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
# Second call with skip_cache
result2 = await async_func(5, cachier__skip_cache=True)
assert result2 == 20
# Third call - should use cache from first call
result3 = await async_func(5)
assert result3 == 10
async_func.clear_cache()
async def test_overwrite_cache(self):
"""Test async caching with cachier__overwrite_cache parameter."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return call_count * 10
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
# Second call with overwrite_cache
result2 = await async_func(5, cachier__overwrite_cache=True)
assert result2 == 20
# Third call - should use new cached value
result3 = await async_func(5)
assert result3 == 20
async_func.clear_cache()
# =============================================================================
# Class Method Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestAsyncMethod:
"""Tests for async caching on class methods."""
async def test_caches_result(self):
"""Test async caching on class methods returns cached result."""
class MyClass:
def __init__(self, value):
self.value = value
# allow_non_static_methods=True: cross-instance cache sharing
# is intentional in this test
@cachier(backend="memory", allow_non_static_methods=True)
async def async_method(self, x):
await asyncio.sleep(0.1)
return x * self.value
obj1 = MyClass(2)
obj1.async_method.clear_cache()
# First call on obj1
result1 = await obj1.async_method(5)
assert result1 == 10
# Second call on obj1 - should use cache
start = time()
result2 = await obj1.async_method(5)
end = time()
assert result2 == 10
assert end - start < 0.05
obj1.async_method.clear_cache()
async def test_shares_cache_across_instances(self):
"""Test that async method cache is shared across instances."""
class MyClass:
def __init__(self, value):
self.value = value
# allow_non_static_methods=True: cross-instance cache sharing
# is intentional in this test
@cachier(backend="memory", allow_non_static_methods=True)
async def async_method(self, x):
await asyncio.sleep(0.1)
return x * self.value
obj1 = MyClass(2)
obj2 = MyClass(3)
obj1.async_method.clear_cache()
# First call on obj1
result1 = await obj1.async_method(5)
assert result1 == 10
# Call on obj2 with same argument - should also use cache
# (because cache is based on method arguments, not instance)
result2 = await obj2.async_method(5)
assert result2 == 10 # Returns cached value from obj1
obj1.async_method.clear_cache()
async def test_guard_raises_without_opt_in(self):
"""Test that @cachier raises TypeError for async instance methods without opt-in."""
with pytest.raises(TypeError, match="allow_non_static_methods"):
class MyClass:
@cachier(backend="memory")
async def async_method(self, x):
return x
# =============================================================================
# Sync Function Compatibility Tests
# =============================================================================
class TestSyncCompatibility:
"""Tests to ensure sync functions still work."""
@pytest.mark.memory
def test_still_works(self):
"""Ensure sync functions still work after adding async support."""
@cachier(backend="memory")
def sync_func(x):
sleep(0.1)
return x * 2
sync_func.clear_cache()
# First call
result1 = sync_func(5)
assert result1 == 10
# Second call should use cache
start = time()
result2 = sync_func(5)
end = time()
assert result2 == 10
assert end - start < 0.05
sync_func.clear_cache()
@pytest.mark.memory
@pytest.mark.asyncio
async def test_sync_wrapper_exposes_async_clear_methods(self):
"""Ensure sync wrappers expose async clear helpers."""
@cachier(backend="memory")
def sync_func(x):
return x
assert sync_func(1) == 1
await sync_func.aclear_being_calculated()
await sync_func.aclear_cache()
@pytest.mark.memory
@pytest.mark.asyncio
class TestAsyncWrapperMaintenanceMethods:
"""Tests for clear helpers exposed on async wrappers."""
async def test_clear_methods_are_await_safe(self):
"""Async wrappers support both sync and awaited clear_cache usage."""
@cachier(backend="memory")
async def async_func(x):
return x
# Legacy sync usage should keep working.
async_func.clear_cache()
async_func.clear_being_calculated()
# Awaiting these methods should also work.
await async_func.clear_cache()
await async_func.clear_being_calculated()
# =============================================================================
# Argument Handling Tests
# =============================================================================
class TestArgumentHandling:
"""Tests for different argument types and patterns."""
@pytest.mark.parametrize(
("args", "kwargs", "expected"),
[
((1, 2), {}, 13), # positional args
((1,), {"y": 2}, 13), # keyword args
((1, 2), {"z": 5}, 8), # different default override
],
)
@pytest.mark.memory
@pytest.mark.asyncio
async def test_different_types(self, args, kwargs, expected):
"""Test async caching with different argument types."""
@cachier(backend="memory")
async def async_func(x, y, z=10):
await asyncio.sleep(0.1)
return x + y + z
async_func.clear_cache()
result = await async_func(*args, **kwargs)
assert result == expected
async_func.clear_cache()
# =============================================================================
# Max Age Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
@pytest.mark.maxage
class TestMaxAge:
"""Tests for max_age parameter functionality."""
async def test_recalculates_when_expired(self):
"""Test that max_age causes recalculation when cache is too old."""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(days=1))
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Wait a bit
await asyncio.sleep(0.5)
# Second call with max_age - should recalculate because cache is older
# than max_age
result2 = await async_func(5, max_age=timedelta(milliseconds=100))
assert result2 == 10
assert call_count == 2
async_func.clear_cache()
async def test_uses_cache_when_fresh(self):
"""Test that cache is used when within max_age."""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(days=1))
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Second call with max_age - should use cache
previous_call_count = call_count
result2 = await async_func(5, max_age=timedelta(seconds=10))
assert result2 == 10
assert call_count == previous_call_count # No additional call
async_func.clear_cache()
async def test_negative_max_age_forces_recalculation(self):
"""Test that negative max_age forces recalculation."""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(days=1))
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Second call with negative max_age - should recalculate
result2 = await async_func(5, max_age=timedelta(seconds=-1))
assert result2 == 10
assert call_count == 2
async_func.clear_cache()
# =============================================================================
# Concurrent Access Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestConcurrentAccess:
"""Tests for concurrent async call behavior."""
async def test_calls_execute_in_parallel(self):
"""Test that concurrent async calls execute in parallel."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.2)
return x * 2
async_func.clear_cache()
call_count = 0
# First concurrent calls - all will execute in parallel
results1 = await asyncio.gather(
async_func(5),
async_func(5),
async_func(5),
)
assert all(r == 10 for r in results1)
# All three calls executed
assert call_count == 3
async_func.clear_cache()
async def test_consequent_calls_use_cache(self):
"""Test that calls after caching use cached value."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.2)
return x * 2
async_func.clear_cache()
call_count = 0
# First call to populate cache
await async_func(5)
assert call_count == 1
# Subsequent calls should use cache
call_count = 0
results2 = await asyncio.gather(
async_func(5),
async_func(5),
async_func(5),
)
assert all(r == 10 for r in results2)
assert call_count == 0 # No new calls, all from cache
async_func.clear_cache()
async def test_stale_entry_being_processed_with_next_time(self):
"""Test concurrent calls with stale cache and next_time=True return stale values.
When cache is stale and next_time=True, concurrent calls should return the stale value while background
recalculation happens.
"""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(seconds=1), next_time=True)
async def slow_async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(1.0) # Long enough to create processing overlap
return call_count * 10
slow_async_func.clear_cache()
call_count = 0
# First call - populate cache
result1 = await slow_async_func(5)
assert result1 == 10
assert call_count == 1
# Wait for cache to become stale
await asyncio.sleep(1.5)
# Start a slow recalculation in background (don't await it yet)
task1 = asyncio.create_task(slow_async_func(5))
# Give it a moment to mark entry as being processed
await asyncio.sleep(0.1)
# Now make another call while first one is still processing
# This should return the stale value because entry._processing=True and next_time=True
result2 = await slow_async_func(5)
assert result2 == 10 # Should return stale value
# Wait for background task to complete
await task1
# Wait enough time for the background update to complete and cache to be updated
await asyncio.sleep(1.5)
# Next call should get an updated value (could be 20 or 30 depending on background tasks)
result3 = await slow_async_func(5)
assert result3 > 10 # Should be updated from background
slow_async_func.clear_cache()
# =============================================================================
# None Value Handling Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestNoneHandling:
"""Tests for allow_none parameter behavior."""
async def test_not_cached_by_default(self):
"""Test that None values are not cached when allow_none=False."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return None if x == 0 else x * 2
async_func.clear_cache()
call_count = 0
# First call returning None - should not be cached
result1 = await async_func(0)
assert result1 is None
assert call_count == 1
# Second call with same args - should recalculate (None not cached)
result2 = await async_func(0)
assert result2 is None
assert call_count == 2
async_func.clear_cache()
async def test_cached_when_allowed(self):
"""Test that None values are cached when allow_none=True."""
call_count = 0
@cachier(backend="memory", allow_none=True)
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return None if x == 0 else x * 2
async_func.clear_cache()
call_count = 0
# First call returning None - should be cached
result1 = await async_func(0)
assert result1 is None
assert call_count == 1
# Second call with same args - should use cached None
previous_call_count = call_count
result2 = await async_func(0)
assert result2 is None
assert call_count == previous_call_count # No additional call
async_func.clear_cache()
async def test_non_none_cached_with_allow_none_false(self):
"""Test that non-None values are cached even when allow_none=False."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return None if x == 0 else x * 2
async_func.clear_cache()
call_count = 0
# Call with non-None result - should be cached
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Call again - should use cache
previous_call_count = call_count
result2 = await async_func(5)
assert result2 == 10
assert call_count == previous_call_count # No additional call
async_func.clear_cache()
# =============================================================================
# Additional Coverage Tests
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestAsyncVerboseMode:
"""Tests for verbose_cache parameter with async functions."""
async def test_verbose_cache_parameter(self, capsys):
"""Test verbose_cache parameter prints debug info."""
import warnings
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call with verbose=True (deprecated but still works)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
result1 = await async_func(5, verbose_cache=True)
assert result1 == 10
captured = capsys.readouterr()
assert "No entry found" in captured.out or "Calling" in captured.out
# Second call with verbose=True
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
result2 = await async_func(5, verbose_cache=True)
assert result2 == 10
captured = capsys.readouterr()
assert "Entry found" in captured.out or "Cached result" in captured.out
async_func.clear_cache()
async def test_cachier_verbose_kwarg(self, capsys):
"""Test cachier__verbose keyword argument."""
@cachier(backend="memory")
async def async_func(x):
await asyncio.sleep(0.1)
return x * 3
async_func.clear_cache()
# Use cachier__verbose keyword
result = await async_func(7, cachier__verbose=True)
assert result == 21
captured = capsys.readouterr()
assert len(captured.out) > 0 # Should have printed something
async_func.clear_cache()
class TestAsyncGlobalCachingControl:
"""Tests for global caching enable/disable with async functions."""
@pytest.mark.memory
@pytest.mark.asyncio
async def test_disable_caching_globally(self):
"""Test disabling caching globally affects async functions."""
import cachier
call_count = 0
@cachier.cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# Enable caching (default)
cachier.enable_caching()
# First call - should cache
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Second call - should use cache
result2 = await async_func(5)
assert result2 == 10
assert call_count == 1
# Disable caching
cachier.disable_caching()
# Third call - should not use cache
result3 = await async_func(5)
assert result3 == 10
assert call_count == 2
# Fourth call - still should not use cache
result4 = await async_func(5)
assert result4 == 10
assert call_count == 3
# Re-enable caching
cachier.enable_caching()
async_func.clear_cache()
class TestAsyncCleanupStale:
"""Tests for cleanup_stale functionality with async functions."""
@pytest.mark.memory
@pytest.mark.asyncio
async def test_cleanup_stale_entries(self):
"""Test that stale entries are cleaned up with cleanup_stale=True."""
call_count = 0
@cachier(
backend="memory",
stale_after=timedelta(seconds=1),
cleanup_stale=True,
cleanup_interval=timedelta(milliseconds=100),
)
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.1)
return x * 2
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Wait for stale
await asyncio.sleep(1.5)
# Second call - triggers cleanup in background
result2 = await async_func(5)
assert result2 == 10
assert call_count == 2
# Give cleanup time to run
await asyncio.sleep(0.5)
async_func.clear_cache()
@pytest.mark.memory
@pytest.mark.asyncio
async def test_cleanup_interval_skips_submit_when_not_elapsed(self, monkeypatch):
"""Test async cleanup interval throttles background cleanup submits."""
class _DummyExecutor:
def __init__(self):
self.submits = 0
def submit(self, *args, **kwargs):
self.submits += 1
dummy = _DummyExecutor()
monkeypatch.setattr("cachier.core._get_executor", lambda: dummy)
@cachier(
backend="memory",
stale_after=timedelta(seconds=1),
cleanup_stale=True,
cleanup_interval=timedelta(hours=1),
)
async def async_func(x):
await asyncio.sleep(0.01)
return x * 2
async_func.clear_cache()
await async_func(5)
await async_func(5)
assert dummy.submits == 1
async_func.clear_cache()
@pytest.mark.memory
@pytest.mark.asyncio
class TestAsyncProcessingEntry:
"""Tests for entry being processed scenarios with async functions."""
async def test_entry_processing_without_value(self):
"""Test async recalculation when entry is processing but has no value."""
call_count = 0
@cachier(backend="memory")
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.3)
return x * 2
async_func.clear_cache()
call_count = 0
# Launch concurrent calls - they should all execute
results = await asyncio.gather(
async_func(10),
async_func(10),
async_func(10),
)
assert all(r == 20 for r in results)
# All three should have executed since async doesn't wait
assert call_count == 3
async_func.clear_cache()
async def test_stale_entry_processing_recalculates(self):
"""Test that stale entry being processed causes recalculation."""
call_count = 0
@cachier(backend="memory", stale_after=timedelta(seconds=1))
async def async_func(x):
nonlocal call_count
call_count += 1
await asyncio.sleep(0.5)
return call_count * 10
async_func.clear_cache()
call_count = 0
# First call
result1 = await async_func(5)
assert result1 == 10
assert call_count == 1
# Wait for stale
await asyncio.sleep(1.5)
# Launch concurrent calls on stale entry
# Both should recalculate (no waiting in async)
await asyncio.gather(
async_func(5),
async_func(5),
)
# Both should have executed
assert call_count >= 2
async_func.clear_cache()
# =============================================================================
# Exception Handling and Edge Cases
# =============================================================================
@pytest.mark.memory
@pytest.mark.asyncio
class TestAsyncExceptionHandling:
"""Tests for exception handling in async background tasks."""
async def test_function_thread_async_exception_handling(self, capsys):
"""Test that exceptions in background async tasks are caught and printed."""