Coverage for tests/test_syncify_no_raise.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-11 11:00 +0000

1import threading 1nopqrstuvwxyz

2from dataclasses import dataclass 1nopqrstuvwxyz

3 

4import anyio 1nopqrstuvwxyz

5from asyncer import asyncify, syncify 1nopqrstuvwxyz

6 

7 

8@dataclass 1nopqrstuvwxyz

9class Report: 1nopqrstuvwxyz

10 thread_id: int 1nopqrstuvwxyz

11 caller_func: str 1nopqrstuvwxyz

12 

13 

14def test_syncify_no_raise_async(): 1nopqrstuvwxyz

15 reports: list[Report] = [] 1abcdefghijklm

16 

17 async def do_sub_async_work(): 1abcdefghijklm

18 report = Report( 1abcdefghijklm

19 thread_id=threading.get_ident(), 

20 caller_func="do_sub_async_work", 

21 ) 

22 reports.append(report) 1abcdefghijklm

23 

24 def do_sub_sync_work(): 1abcdefghijklm

25 report = Report( 1abcdefghijklm

26 thread_id=threading.get_ident(), 

27 caller_func="do_sub_sync_work", 

28 ) 

29 reports.append(report) 1abcdefghijklm

30 syncify(do_sub_async_work, raise_sync_error=False)() 1abcdefghijklm

31 

32 async def do_async_work(): 1abcdefghijklm

33 report = Report( 1abcdefghijklm

34 thread_id=threading.get_ident(), 

35 caller_func="do_async_work", 

36 ) 

37 reports.append(report) 1abcdefghijklm

38 await asyncify(do_sub_sync_work)() 1abcdefghijklm

39 

40 def do_sync_work(): 1abcdefghijklm

41 own_report = Report( 1abcdefghijklm

42 thread_id=threading.get_ident(), 

43 caller_func="do_sync_work", 

44 ) 

45 reports.append(own_report) 1abcdefghijklm

46 syncify(do_async_work, raise_sync_error=False)() 1abcdefghijklm

47 

48 async def main(): 1abcdefghijklm

49 own_report = Report( 1abcdefghijklm

50 thread_id=threading.get_ident(), 

51 caller_func="main", 

52 ) 

53 reports.append(own_report) 1abcdefghijklm

54 await asyncify(do_sync_work)() 1abcdefghijklm

55 

56 def sync_main(): 1abcdefghijklm

57 own_report = Report( 1abcdefghijklm

58 thread_id=threading.get_ident(), 

59 caller_func="sync_main", 

60 ) 

61 reports.append(own_report) 1abcdefghijklm

62 do_sync_work() 1abcdefghijklm

63 

64 anyio.run(main) 1abcdefghijklm

65 sync_main() 1abcdefghijklm

66 main_thread_id = threading.get_ident() 1abcdefghijklm

67 assert reports[0].caller_func == "main" 1abcdefghijklm

68 assert reports[0].thread_id == main_thread_id 1abcdefghijklm

69 assert reports[1].caller_func == "do_sync_work" 1abcdefghijklm

70 assert reports[1].thread_id != main_thread_id 1abcdefghijklm

71 assert reports[2].caller_func == "do_async_work" 1abcdefghijklm

72 assert reports[2].thread_id == main_thread_id 1abcdefghijklm

73 assert reports[3].caller_func == "do_sub_sync_work" 1abcdefghijklm

74 assert reports[3].thread_id != main_thread_id 1abcdefghijklm

75 assert reports[4].caller_func == "do_sub_async_work" 1abcdefghijklm

76 assert reports[4].thread_id == main_thread_id 1abcdefghijklm

77 assert reports[5].caller_func == "sync_main" 1abcdefghijklm

78 assert reports[5].thread_id == main_thread_id 1abcdefghijklm

79 assert reports[6].caller_func == "do_sync_work" 1abcdefghijklm

80 assert reports[6].thread_id == main_thread_id 1abcdefghijklm

81 assert reports[7].caller_func == "do_async_work" 1abcdefghijklm

82 assert reports[7].thread_id == main_thread_id 1abcdefghijklm

83 assert reports[8].caller_func == "do_sub_sync_work" 1abcdefghijklm

84 assert reports[8].thread_id != main_thread_id 1abcdefghijklm

85 assert reports[9].caller_func == "do_sub_async_work" 1abcdefghijklm

86 assert reports[9].thread_id == main_thread_id 1abcdefghijklm