Coverage for tests/test_syncify_no_raise.py: 100%

57 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 01:12 +0000

1import threading 1fghij

2from dataclasses import dataclass 1fghij

3from typing import List 1fghij

4 

5import anyio 1fghij

6from asyncer import asyncify, syncify 1fghij

7 

8 

9@dataclass 1fghij

10class Report: 1fghij

11 thread_id: int 1fghij

12 caller_func: str 1fghij

13 

14 

15def test_syncify_no_raise_async(): 1fghij

16 reports: List[Report] = [] 1abcde

17 

18 async def do_sub_async_work(): 1abcde

19 report = Report( 1abcde

20 thread_id=threading.get_ident(), 

21 caller_func="do_sub_async_work", 

22 ) 

23 reports.append(report) 1abcde

24 

25 def do_sub_sync_work(): 1abcde

26 report = Report( 1abcde

27 thread_id=threading.get_ident(), 

28 caller_func="do_sub_sync_work", 

29 ) 

30 reports.append(report) 1abcde

31 syncify(do_sub_async_work, raise_sync_error=False)() 1abcde

32 

33 async def do_async_work(): 1abcde

34 report = Report( 1abcde

35 thread_id=threading.get_ident(), 

36 caller_func="do_async_work", 

37 ) 

38 reports.append(report) 1abcde

39 await asyncify(do_sub_sync_work)() 1abcde

40 

41 def do_sync_work(): 1abcde

42 own_report = Report( 1abcde

43 thread_id=threading.get_ident(), 

44 caller_func="do_sync_work", 

45 ) 

46 reports.append(own_report) 1abcde

47 syncify(do_async_work, raise_sync_error=False)() 1abcde

48 

49 async def main(): 1abcde

50 own_report = Report( 1abcde

51 thread_id=threading.get_ident(), 

52 caller_func="main", 

53 ) 

54 reports.append(own_report) 1abcde

55 await asyncify(do_sync_work)() 1abcde

56 

57 def sync_main(): 1abcde

58 own_report = Report( 1abcde

59 thread_id=threading.get_ident(), 

60 caller_func="sync_main", 

61 ) 

62 reports.append(own_report) 1abcde

63 do_sync_work() 1abcde

64 

65 anyio.run(main) 1abcde

66 sync_main() 1abcde

67 main_thread_id = threading.get_ident() 1abcde

68 assert reports[0].caller_func == "main" 1abcde

69 assert reports[0].thread_id == main_thread_id 1abcde

70 assert reports[1].caller_func == "do_sync_work" 1abcde

71 assert reports[1].thread_id != main_thread_id 1abcde

72 assert reports[2].caller_func == "do_async_work" 1abcde

73 assert reports[2].thread_id == main_thread_id 1abcde

74 assert reports[3].caller_func == "do_sub_sync_work" 1abcde

75 assert reports[3].thread_id != main_thread_id 1abcde

76 assert reports[4].caller_func == "do_sub_async_work" 1abcde

77 assert reports[4].thread_id == main_thread_id 1abcde

78 assert reports[5].caller_func == "sync_main" 1abcde

79 assert reports[5].thread_id == main_thread_id 1abcde

80 assert reports[6].caller_func == "do_sync_work" 1abcde

81 assert reports[6].thread_id == main_thread_id 1abcde

82 assert reports[7].caller_func == "do_async_work" 1abcde

83 assert reports[7].thread_id == main_thread_id 1abcde

84 assert reports[8].caller_func == "do_sub_sync_work" 1abcde

85 assert reports[8].thread_id != main_thread_id 1abcde

86 assert reports[9].caller_func == "do_sub_async_work" 1abcde

87 assert reports[9].thread_id == main_thread_id 1abcde