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
U
Z±dZbã@sÎddlZddlZddlZddlZddlZddlZddlmZddl    m
Z
m Z ddl m Z ddl mZmZmZddlmZddlmZdd    lmZmZmZmZdd
lmZd d „Zd d„Zdd„Zdd„Zdd„Z dd„Z!dd„Z"ej# $ddddg¡ej# $dddg¡ej# $d ddg¡d!d"„ƒƒƒZ%d#d$„Z&d%d&„Z'd'd(„Z(d)d*„Z)d+d,„Z*e +d-¡Z,d.d/„Z-d0d1„Z.d2d3„Z/d4d5„Z0d6d7„Z1d8d9„Z2d:d;„Z3d<d=„Z4d>d?„Z5d@dA„Z6ej#j7edBdCdDdE„ƒZ8dFdG„Z9dHdI„Z:dS)JéN)Úcurrent_async_library_cvar)Ú    TrioTokenÚcurrent_trio_tokené)Ú_core)ÚEventÚCapacityLimiterÚsleep)Úwait_all_tasks_blocked)Úbuggy_pypy_asyncgens)Úto_thread_run_syncÚcurrent_default_thread_limiterÚfrom_thread_runÚfrom_thread_run_sync)Úki_selfcƒs¢t ¡‰d ‡fdd„    }t ¡}dd„}|t|d|dIdHdd„}|t|dtf|dIdHd    d„}|t|d
|dIdHd d„}|t|dtf|dIdHdS) Nc“sjg‰‡‡‡‡fdd„}tj|dd}| ¡| ¡rLtdƒtdƒIdHq,ˆd|fdˆf|gksft‚dS)    Nc
stz2ˆ dt ¡f¡ˆˆˆˆd}ˆ d|f¡Wn<tk
rn}zt|ƒˆ dt|ƒf¡W5d}~XYnXdS)NÚstart©Ú
trio_tokenÚgotÚerror)ÚappendÚ    threadingÚcurrent_threadÚ BaseExceptionÚprintÚtype)ÚxÚexc©Údo_in_trio_threadÚfnÚrecordr©úNd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\trio/tests/test_threads.pyÚthreadfnsz<test_do_in_trio_thread.<locals>.check_case.<locals>.threadfnT)ÚtargetÚdaemonZyawnç{®Gáz„?rÚf)rÚThreadrÚis_aliverr    ÚAssertionError)rr Úexpectedrr$Ú child_thread©Ú trio_threadrr#Ú
check_cases    z*test_do_in_trio_thread.<locals>.check_casecSs"t ¡r t‚| dt ¡f¡dS)Nr(r)rÚcurrently_ki_protectedr+rrr©r!r"r"r#r(1s z!test_do_in_trio_thread.<locals>.f)rrrcSs&t ¡r t‚| dt ¡f¡t‚dS©Nr()rr1r+rrrÚ
ValueErrorr2r"r"r#r(8s rcÓs0t ¡r t‚t ¡IdH| dt ¡f¡dS)Nr(é)rr1r+Ú
checkpointrrrr2r"r"r#r(?s )rr5cÓs4t ¡r t‚t ¡IdH| dt ¡f¡t‚dSr3)rr1r+r6rrrÚKeyErrorr2r"r"r#r(Gs )N)rrrrrr4rr7)r0Útokenr(r"r.r#Útest_do_in_trio_threadsr9c    ÃsLt t¡tdd„ƒW5QRXdd„}t t¡t|ƒW5QRXdS)NcSsdS©Nr"r"r"r"r#Ú<lambda>Róz9test_do_in_trio_thread_from_trio_thread.<locals>.<lambda>cÓsdSr:r"r"r"r"r#ÚfooTsz4test_do_in_trio_thread_from_trio_thread.<locals>.foo)ÚpytestÚraisesÚ RuntimeErrorrr)r=r"r"r#Ú'test_do_in_trio_thread_from_trio_threadPs
  rAcs0tƒ‰‡fdd„}t |¡ˆddhks,t‚dS)Nc“s~t ¡‰dd„‰‡fdd„‰‡‡‡‡fdd„}tj|d}| ¡tdƒ| ¡rbtd    ƒIdHqJtd
ƒ| ¡td ƒdS) Nc
SsDtdƒt ¡rt‚tdƒz
tƒW5ddl}td| ¡ƒXdS)Nzin Trio threadrrÚfinally)rrr1r+ÚsysÚexc_infor)rCr"r"r#Útrio_thread_fncs 
zTtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_fnc“s
ˆƒdSr:r"r")rEr"r#Útrio_thread_afnnszUtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.trio_thread_afncs|ztdƒtˆˆdWn&tk
r>tdƒˆ d¡YnXztˆˆdWn&tk
rvtdƒˆ d¡YnXdS)NÚrunningrÚok1Úok2)rrÚKeyboardInterruptÚaddrr")r!r8rFrEr"r#Úexternal_thread_fnqszXtest_run_in_trio_thread_ki.<locals>.check_run_in_trio_thread.<locals>.external_thread_fn)r%Zwaitingr'zwaited, joiningÚdone)    rrrr)rrr*r    Újoin)rLÚthreadr2)r8rFrEr#Úcheck_run_in_trio_thread`s  z<test_run_in_trio_thread_ki.<locals>.check_run_in_trio_threadrHrI)ÚsetrÚrunr+)rPr"r2r#Útest_run_in_trio_thread_ki[s '
rScs\g‰tƒ‰‡‡fdd„‰‡‡fdd„‰‡‡‡fdd„}t |¡}| ¡ˆddgksXt‚dS)    Nc“s*ˆ d¡ˆ ¡t dd„¡IdHdS)NÚsleepingcSstjjSr:)rZAbortZ    SUCCEEDED)Ú_r"r"r#r;’r<zMtest_await_in_trio_thread_while_main_exits.<locals>.trio_fn.<locals>.<lambda>)rrQrZwait_task_rescheduledr")Úevr!r"r#Útrio_fns
z;test_await_in_trio_thread_while_main_exits.<locals>.trio_fncs6ztˆ|dWn tjk
r0ˆ d¡YnXdS)NrÚ    cancelled)rrZ    Cancelledr)r8)r!rWr"r#Ú    thread_fn”sz=test_await_in_trio_thread_while_main_exits.<locals>.thread_fnc“s@t ¡}tjˆ|fd}| ¡ˆ ¡IdHˆdgks<t‚|S)N)r%ÚargsrT)rrrr)rÚwaitr+)r8rO)rVr!rYr"r#Úmainšs z8test_await_in_trio_thread_while_main_exits.<locals>.mainrTrX)rrrRrNr+)r\rOr")rVr!rYrWr#Ú*test_await_in_trio_thread_while_main_exits‹s
r]c    ÃsŒt ¡}dd„}t|dƒIdH\}}|dks0t‚||ks<t‚dd„}t t¡}t|ƒIdHW5QRXt|jj    ƒ|jj    d|ksˆt‚dS)NcSs |t ¡fSr:)rr)rr"r"r#r(ªsz$test_run_in_worker_thread.<locals>.fécSstt ¡ƒ‚dSr:)r4rrr"r"r"r#Úg±sz$test_run_in_worker_thread.<locals>.gr)
rrr r+r>r?r4rÚvaluerZ)r/r(rr-r_Úexcinfor"r"r#Útest_run_in_worker_thread§s    rbc
ƒsdg‰‡fdd„‰‡‡fdd„}g‰t ¡}t ¡4IdHš8}| ||d¡tƒIdHˆdgksft‚|j ¡W5QIdHRXˆddkst‚|     d¡ˆddkr²t
  d    ¡qšg‰dˆd<t ¡4IdHšz}| ||d
¡tƒIdH|j ¡tj dd $t d ƒD]}t ¡IdHq
W5QRXˆdgks:t‚|     d¡W5QIdHRXt  ¡}| ¡||d
ƒIdHW5QRX|jsŒt‚dS) Ncsdˆd<| ¡dˆd<dS)NÚblockingrÚfinished)Úget)Úq)Úregisterr"r#r(½sz1test_run_in_worker_thread_cancellation.<locals>.fc    “s4ˆ d¡ztˆ||dIdHW¢Sˆ d¡XdS)NrÚexit©Ú cancellable)rr )rfrj)r(r!r"r#ÚchildÃs
z5test_run_in_worker_thread_cancellation.<locals>.childTrrrdr'F©Zshieldé
)Ú stdlib_queueÚQueuerÚ open_nurseryÚ
start_soonr
r+Ú cancel_scopeÚcancelÚputÚtimer    Ú CancelScopeÚranger6Úcancelled_caught)rkrfÚnurseryrUZscoper")r(r!rgr#Ú&test_run_in_worker_thread_cancellationºs:  
 
 
rzcs’| tjdd¡t ¡‰t ¡‰‡‡fdd„‰‡fdd„}t |¡ˆ d¡ˆ ¡}| ¡rjt     
d¡qV|  ¡\}}d|ks‚t ‚d|ksŽt ‚dS)NZ IDLE_TIMEOUTr'csˆ ¡ˆ t ¡¡dSr:)rertrrr")Úq1Úq2r"r#rYüsz6test_run_in_worker_thread_abandoned.<locals>.thread_fnc
“sR‡fdd„}t ¡4IdHš&}| |¡tƒIdH|j ¡W5QIdHRXdS)Nc“stˆddIdHdS)NTri)r r"©rYr"r#rksz@test_run_in_worker_thread_abandoned.<locals>.main.<locals>.child)rrprqr
rrrs)rkryr}r"r#r\s
 
 z1test_run_in_worker_thread_abandoned.<locals>.mainzException in thread) ÚsetattrrÚ _thread_cachernrorRrtrer*rur    Z
readouterrr+)ZcapfdÚ monkeypatchr\rOÚoutÚerrr")r{r|rYr#Ú#test_run_in_worker_thread_abandonedös     
 
   rƒÚMAXr5érmrsFTÚuse_default_limiterc
 
ƒsÂd|}t ¡‰t ¡‰|r4tƒ}|j}||_d‰nt|ƒ}|}|‰znGdd„dƒ‰dˆ_dˆ_dˆ_dˆ_    t
  ¡‰‡‡‡‡fdd„‰‡‡‡fdd„}t
  ¡4IdHšª}t d    ƒg}t|ƒD],}    | tƒ¡| ||d
¡tƒIdHq¾ˆrt d ƒ|d ¡IdHtƒIdHˆj    |ks4| ¡j|krFtd ƒIdHqˆ ¡W5QIdHRXˆj|ksnt‚ˆr’|jdkr’td ƒIdHqtˆj|ks¢t‚ˆjdks²t‚W5||_XdS) Nrc@s eZdZdS)z0test_run_in_worker_thread_limiter.<locals>.stateN)Ú__name__Ú
__module__Ú __qualname__r"r"r"r#Ústate9srŠrc    s tdƒt|jˆdˆ@ˆjd7_ˆjd7_tˆjˆjƒˆ_ˆjd7_W5QRXˆ ¡ˆ"ˆjd8_ˆjd8_W5QRXtdƒdS)Nzthread_fn startrr^zthread_fn exiting)    rrrsÚranrGÚmaxÚ
high_waterÚparkedr[)rr)ÚgateÚlockrŠr8r"r#rYCsz4test_run_in_worker_thread_limiter.<locals>.thread_fnc    “sBt ¡}tˆ|ˆˆdIdHW5QRXtd|jƒ| ¡dS)N)Úlimiterrjzrun_thread finished, cancelled:)rrvr rrxrQ)Úeventrr)rsÚ limiter_argrYr"r#Ú
run_threadSs
ÿ z5test_run_in_worker_thread_limiter.<locals>.run_threadZspawningéÿÿÿÿz'waiting for first cancellation to clearr')rrÚLockr Z total_tokensrr‹rrGrŽrrrprrwrrqr
r[Z
statisticsZ tasks_waitingr    rQr+Úborrowed_tokens)
r„rsr†ZCOUNTÚcZorig_total_tokensr”ryÚeventsÚir")rsrr“rrŠrYr8r#Ú!test_run_in_worker_thread_limitersR         r›cƒsBg‰G‡fdd„dƒ}tdd„|ƒdIdHˆddgks>t‚dS)Ncs$eZdZ‡fdd„Z‡fdd„ZdS)z?test_run_in_worker_thread_custom_limiter.<locals>.CustomLimiterc“sˆ d¡||_dS©NÚacquire)rÚ    _borrower©ÚselfZborrowerr2r"r#Úacquire_on_behalf_ofŠs
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.acquire_on_behalf_ofcsˆ d¡||jkst‚dS©NÚrelease)rržr+rŸr2r"r#Úrelease_on_behalf_ofŽs
zTtest_run_in_worker_thread_custom_limiter.<locals>.CustomLimiter.release_on_behalf_ofN©r‡rˆr‰r¡r¤r"r2r"r#Ú CustomLimiter‰s r¦cSsdSr:r"r"r"r"r#r;’r<z:test_run_in_worker_thread_custom_limiter.<locals>.<lambda>©r‘rr£)r r+)r¦r"r2r#Ú(test_run_in_worker_thread_custom_limiter„s    r¨c    ƒsÆg‰G‡fdd„dƒ}|ƒ}t t¡}tdd„|dIdHW5QRX|jjdksXt‚ˆddgksht‚g‰i‰t t¡ }t‡fdd„|dIdHW5QRXt|jjtƒs²t‚ˆddgksÂt‚dS)    Ncs$eZdZ‡fdd„Z‡fdd„ZdS)zCtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiterc“sˆ d¡dSrœ)rrŸr2r"r#r¡šszXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.acquire_on_behalf_ofcsˆ d¡t‚dSr¢)rr4rŸr2r"r#r¤s
zXtest_run_in_worker_thread_limiter_error.<locals>.BadCapacityLimiter.release_on_behalf_ofNr¥r"r2r"r#ÚBadCapacityLimiter™s r©cSsdSr:r"r"r"r"r#r;¤r<z9test_run_in_worker_thread_limiter_error.<locals>.<lambda>r§rr£csˆdS)Nrr"r")Údr"r#r;­r<)    r>r?r4r r`Ú __context__r+Ú
isinstancer7)r©Úbsrar")rªr!r#Ú'test_run_in_worker_thread_limiter_error–s   $r®c    Ãszdd„}| tjjd|¡tƒ}|jdks.t‚t t    ¡}t
dd„ƒIdHW5QRXdt |j ƒksht‚|jdksvt‚dS)NcWs tdƒ‚dS)Nz!the engines canna take it captain)r@)r rZr"r"r#Ú    bad_start´sz:test_run_in_worker_thread_fail_to_spawn.<locals>.bad_startZstart_thread_soonrcSsdSr:r"r"r"r"r#r;¾r<z9test_run_in_worker_thread_fail_to_spawn.<locals>.<lambda>Zengines) r~rrZ ThreadCacher r—r+r>r?r@r Ústrr`)r€r¯r‘rar"r"r#Ú'test_run_in_worker_thread_fail_to_spawn²s r±cÃs.dd„}t ¡}t|ƒIdH}||ks*t‚dS)NcSsttjƒ}|Sr:©rrr©Ú callee_tokenr"r"r#rYÇs
z5test_trio_to_thread_run_sync_token.<locals>.thread_fn©rrr r+©rYZ caller_tokenr´r"r"r#Ú"test_trio_to_thread_run_sync_tokenÄsr·c    Ãs4dd„}tjtddt|ƒIdHW5QRXdS)NcÓsdSr:r"r"r"r"r#Úasync_fnÒsz=test_trio_to_thread_run_sync_expected_error.<locals>.async_fnúexpected a sync function©Úmatch)r>r?Ú    TypeErrorr ©r¸r"r"r#Ú+test_trio_to_thread_run_sync_expected_errorÐsr¾Útrio_test_contextvarc
ÃsÄt ¡}t d¡dd„}t|ƒIdH\}}}|dks:t‚|dksFt‚||ksRt‚dd„}t|ƒIdH\}}}}t ¡}t ¡}    |dksŒt‚|dks˜t‚|dks¨tdƒ‚|dks´t‚|    dksÀt‚dS)    Nr\cSst ¡}t ¡}||t ¡fSr:)r¿rerrr)r`Úsniffio_cvar_valuer"r"r#r(àsz3test_trio_to_thread_run_sync_contextvars.<locals>.fcSs2t ¡}t d¡t ¡}t ¡}|||t ¡fS)NÚworker)r¿rerQrrr)Ú parent_valueÚ inner_valuerÀr"r"r#r_ês
üz3test_trio_to_thread_run_sync_contextvars.<locals>.grÁzRThe contextvar value set on the worker would not propagate back to the main threadÚtrio)rrr¿rQr r+rer)
r/r(r`rÀr-r_rÂrÃÚ current_valueZsniffio_outer_valuer"r"r#Ú(test_trio_to_thread_run_sync_contextvarsÜs0
    û  ÿþ rÆc    ƒsddd„}t|ƒIdH}t|tƒs$t‚dd„‰‡fdd„}tjtddt|ƒIdHW5QRXdS)NcSsttjƒ}|Sr:)rrÚ current_time)Ú    trio_timer"r"r#rY
s
z1test_trio_from_thread_run_sync.<locals>.thread_fncÓsdSr:r"r"r"r"r#r¸sz0test_trio_from_thread_run_sync.<locals>.async_fncs tˆƒdSr:)rr"r½r"r#rYsr¹rº)r r¬Úfloatr+r>r?r¼)rYrÈr"r½r#Útest_trio_from_thread_run_syncs rÊc    ƒsrg‰‡fdd„‰‡‡fdd„}t|ƒIdHˆddgks<t‚dd„}tjtd    d
tt|ƒIdHW5QRXdS) Nc“st ¡ˆ d¡dS)Nú back in trio)rrÇrr"r2r"r#Úback_in_trio_fn!sz2test_trio_from_thread_run.<locals>.back_in_trio_fncsˆ d¡tˆƒdS)Nú    in thread)rrr"©rÌr!r"r#rY%s
z,test_trio_from_thread_run.<locals>.thread_fnrÍrËcSsdSr:r"r"r"r"r#Úsync_fn-sz*test_trio_from_thread_run.<locals>.sync_fnzappears to be synchronousrº)r r+r>r?r¼r)rYrÏr"rÎr#Útest_trio_from_thread_runs rÐcÃs.dd„}t ¡}t|ƒIdH}||ks*t‚dS)NcSsttjƒ}|Sr:r²r³r"r"r#rY7s
z.test_trio_from_thread_token.<locals>.thread_fnrµr¶r"r"r#Útest_trio_from_thread_token4srÑcÃs0dd„}t ¡}t||ƒIdH}||ks,t‚dS)NcSsttj|d}|S)Nrr²)r8r´r"r"r#rYCsz4test_trio_from_thread_token_kwarg.<locals>.thread_fnrµr¶r"r"r#Ú!test_trio_from_thread_token_kwarg@srÒc    Ãs$t t¡ttjƒW5QRXdSr:©r>r?r@rrrÇr"r"r"r#Útest_from_thread_no_tokenLs rÔc ÃsÂt d¡dd„}t|ƒIdH\}}}}}}}}t ¡}    t ¡}
|    |krTdksZnt‚||krx|krxdks~nt‚|dksŠt‚|
|krždks¤nt‚||kr¸dks¾nt‚dS)Nr\c    Ss\t ¡}t d¡t ¡}t ¡}dd„}t|ƒ\}}}t ¡}t ¡}||||||||fS)NrÁcSs,t ¡}t d¡t ¡}t ¡}|||fS©NÚ back_in_main©r¿rerQr©Úback_parent_valueÚback_current_valueÚsniffio_cvar_back_valuer"r"r#rÖ]s
 
zStest_trio_from_thread_run_sync_contextvars.<locals>.thread_fn.<locals>.back_in_main)r¿rerQrr)    Úthread_parent_valueÚthread_current_valueÚsniffio_cvar_thread_pre_valuerÖrÙrÚrÛÚthread_after_valueÚsniffio_cvar_thread_after_valuer"r"r#rYWs*
 üøz=test_trio_from_thread_run_sync_contextvars.<locals>.thread_fnrÁrÖrÄ)r¿rQr rerr+) rYrÜrÝrßrÞràrÙrÚrÛrÅZsniffio_cvar_out_valuer"r"r#Ú*test_trio_from_thread_run_sync_contextvarsTs&
( ÷$ rác
Ãs¬t d¡dd„}t|ƒIdH\}}}}}}}}t ¡}    |    |krLdksRnt‚||krp|krpdksvnt‚|dks‚t‚||kr–dksœnt‚|dks¨t‚dS)Nr\c    Ss\t ¡}t d¡t ¡}t ¡}dd„}t|ƒ\}}}t ¡}t ¡}||||||||fS)NrÁcÓs,t ¡}t d¡t ¡}t ¡}|||fSrÕr×rØr"r"r#Úasync_back_in_main’s
 
zTtest_trio_from_thread_run_contextvars.<locals>.thread_fn.<locals>.async_back_in_main)r¿rerQrr)    rÜrÝrÞrârÙrÚrÛrßràr"r"r#rYŒs*
 üøz8test_trio_from_thread_run_contextvars.<locals>.thread_fnrÁrÖrÄ)r¿rQr rer+)
rYrÜrÝrßrÞràrÙrÚrÛrÅr"r"r#Ú%test_trio_from_thread_run_contextvars‰s$
( ÷$ rãc    Cs(t t¡ttjddW5QRXdS)NzNot TrioTokentyperrÓr"r"r"r#Ú4test_run_fn_as_system_task_catched_badly_typed_token½s räc    Ãs6dd„}t ¡}t t¡t||dW5QRXdS)NcSs dst‚dS)NF)r+r"r"r"r#Ú
not_calledÃsz7test_from_thread_inside_trio_thread.<locals>.not_calledr)rrr>r?r@r)rårr"r"r#Ú#test_from_thread_inside_trio_threadÂs ræzpypy 7.2.0 is buggy)Úreasoncs>g‰g‰‡fdd„‰‡‡fdd„}t |¡ˆdgks:t‚dS)Ncs^z
dVW5t tj¡0tjddtttdƒIdHW5QRXW5QRXˆ d¡XdS)NTrlrÚok)    r>r?rZRunFinishedErrorrvr rr    rr"r2r"r#ÚagenÐs
 
&z2test_from_thread_run_during_shutdown.<locals>.agenc“s$ˆ ˆƒ¡ˆd d¡IdHdS)Nr•)rÚasendr")réÚsaver"r#r\Øs z2test_from_thread_run_during_shutdown.<locals>.mainrè)rrRr+)r\r")rér!rër#Ú$test_from_thread_run_during_shutdownËs  
rìcÃs0tƒ}t|tƒst‚t |¡}||ƒks,t‚dSr:)rr¬rr+ÚweakrefÚref)r8Zweak_referencer"r"r#Ú"test_trio_token_weak_referenceableàs
rïc    Ãs<Gdd„dƒ}t t¡tt|ƒdIdHW5QRXdS)Nc@seZdZdd„ZdS)z.test_unsafe_cancellable_kwarg.<locals>.BadBoolcSst‚dSr:)ÚNotImplementedError)r r"r"r#Ú__bool__ìsz7test_unsafe_cancellable_kwarg.<locals>.BadBool.__bool__N)r‡rˆr‰rñr"r"r"r#ÚBadBoolësròri)r>r?rðr Úint)ròr"r"r#Útest_unsafe_cancellable_kwargçs rô);Z contextvarsrÚqueuernrurír>ZsniffiorZ
trio._corerrÚrrrr    Ztestingr
Z_core.tests.tutilr Ú_threadsr r rrZ_core.tests.test_kirr9rArSr]rbrzrƒÚmarkZ parametrizer›r¨r®r±r·r¾Z
ContextVarr¿rÆrÊrÐrÑrÒrÔrárãräræZskipifrìrïrôr"r"r"r#Ú<module>sX     7 0<#h     
+  54