Coverage for tests / test_syncify_no_raise.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-05-05 09:41 +0000

1import threading 1hijklmn

2from dataclasses import dataclass 1hijklmn

3 

4import anyio 1hijklmn

5from asyncer import asyncify, syncify 1hijklmn

6 

7 

8@dataclass 1hijklmn

9class Report: 1hijklmn

10 thread_id: int 1hijklmn

11 caller_func: str 1hijklmn

12 

13 

14def test_syncify_no_raise_async(): 1hijklmn

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

16 

17 async def do_sub_async_work(): 1abcdefg

18 report = Report( 1abcdefg

19 thread_id=threading.get_ident(), 

20 caller_func="do_sub_async_work", 

21 ) 

22 reports.append(report) 1abcdefg

23 

24 def do_sub_sync_work(): 1abcdefg

25 report = Report( 1abcdefg

26 thread_id=threading.get_ident(), 

27 caller_func="do_sub_sync_work", 

28 ) 

29 reports.append(report) 1abcdefg

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

31 

32 async def do_async_work(): 1abcdefg

33 report = Report( 1abcdefg

34 thread_id=threading.get_ident(), 

35 caller_func="do_async_work", 

36 ) 

37 reports.append(report) 1abcdefg

38 await asyncify(do_sub_sync_work)() 1abcdefg

39 

40 def do_sync_work(): 1abcdefg

41 own_report = Report( 1abcdefg

42 thread_id=threading.get_ident(), 

43 caller_func="do_sync_work", 

44 ) 

45 reports.append(own_report) 1abcdefg

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

47 

48 async def main(): 1abcdefg

49 own_report = Report( 1abcdefg

50 thread_id=threading.get_ident(), 

51 caller_func="main", 

52 ) 

53 reports.append(own_report) 1abcdefg

54 await asyncify(do_sync_work)() 1abcdefg

55 

56 def sync_main(): 1abcdefg

57 own_report = Report( 1abcdefg

58 thread_id=threading.get_ident(), 

59 caller_func="sync_main", 

60 ) 

61 reports.append(own_report) 1abcdefg

62 do_sync_work() 1abcdefg

63 

64 anyio.run(main) 1abcdefg

65 sync_main() 1abcdefg

66 main_thread_id = threading.get_ident() 1abcdefg

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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