|
| 1 | +import threading |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import anyio |
| 6 | +from asyncer import asyncify, syncify |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class Report: |
| 11 | + thread_id: int |
| 12 | + caller_func: str |
| 13 | + |
| 14 | + |
| 15 | +def test_syncify_no_raise_async(): |
| 16 | + reports: List[Report] = [] |
| 17 | + |
| 18 | + async def do_sub_async_work(): |
| 19 | + report = Report( |
| 20 | + thread_id=threading.get_ident(), |
| 21 | + caller_func="do_sub_async_work", |
| 22 | + ) |
| 23 | + reports.append(report) |
| 24 | + |
| 25 | + def do_sub_sync_work(): |
| 26 | + report = Report( |
| 27 | + thread_id=threading.get_ident(), |
| 28 | + caller_func="do_sub_sync_work", |
| 29 | + ) |
| 30 | + reports.append(report) |
| 31 | + syncify(do_sub_async_work, raise_sync_error=False)() |
| 32 | + |
| 33 | + async def do_async_work(): |
| 34 | + report = Report( |
| 35 | + thread_id=threading.get_ident(), |
| 36 | + caller_func="do_async_work", |
| 37 | + ) |
| 38 | + reports.append(report) |
| 39 | + await asyncify(do_sub_sync_work)() |
| 40 | + |
| 41 | + def do_sync_work(): |
| 42 | + own_report = Report( |
| 43 | + thread_id=threading.get_ident(), |
| 44 | + caller_func="do_sync_work", |
| 45 | + ) |
| 46 | + reports.append(own_report) |
| 47 | + syncify(do_async_work, raise_sync_error=False)() |
| 48 | + |
| 49 | + async def main(): |
| 50 | + own_report = Report( |
| 51 | + thread_id=threading.get_ident(), |
| 52 | + caller_func="main", |
| 53 | + ) |
| 54 | + reports.append(own_report) |
| 55 | + await asyncify(do_sync_work)() |
| 56 | + |
| 57 | + def sync_main(): |
| 58 | + own_report = Report( |
| 59 | + thread_id=threading.get_ident(), |
| 60 | + caller_func="sync_main", |
| 61 | + ) |
| 62 | + reports.append(own_report) |
| 63 | + do_sync_work() |
| 64 | + |
| 65 | + anyio.run(main) |
| 66 | + sync_main() |
| 67 | + main_thread_id = threading.get_ident() |
| 68 | + assert reports[0].caller_func == "main" |
| 69 | + assert reports[0].thread_id == main_thread_id |
| 70 | + assert reports[1].caller_func == "do_sync_work" |
| 71 | + assert reports[1].thread_id != main_thread_id |
| 72 | + assert reports[2].caller_func == "do_async_work" |
| 73 | + assert reports[2].thread_id == main_thread_id |
| 74 | + assert reports[3].caller_func == "do_sub_sync_work" |
| 75 | + assert reports[3].thread_id != main_thread_id |
| 76 | + assert reports[4].caller_func == "do_sub_async_work" |
| 77 | + assert reports[4].thread_id == main_thread_id |
| 78 | + assert reports[5].caller_func == "sync_main" |
| 79 | + assert reports[5].thread_id == main_thread_id |
| 80 | + assert reports[6].caller_func == "do_sync_work" |
| 81 | + assert reports[6].thread_id == main_thread_id |
| 82 | + assert reports[7].caller_func == "do_async_work" |
| 83 | + assert reports[7].thread_id == main_thread_id |
| 84 | + assert reports[8].caller_func == "do_sub_sync_work" |
| 85 | + assert reports[8].thread_id != main_thread_id |
| 86 | + assert reports[9].caller_func == "do_sub_async_work" |
| 87 | + assert reports[9].thread_id == main_thread_id |
0 commit comments