zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
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
U
Z±d4ã@s\ddlZddlmZddlmZddlmZddlmZddl    m
Z
Gd    d
„d
ee
d Z dS) éN)Úinfé)Ú_coreé)ÚGLOBAL_RUN_CONTEXT)ÚClock)ÚFinalc@s’eZdZdZdefdd„Zdd„Zedd„ƒZej    d    d„ƒZed
d „ƒZ
e
j    d d „ƒZ
d d„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„ZdS)Ú    MockClockañA user-controllable clock suitable for writing tests.
 
    Args:
      rate (float): the initial :attr:`rate`.
      autojump_threshold (float): the initial :attr:`autojump_threshold`.
 
    .. attribute:: rate
 
       How many seconds of clock time pass per second of real time. Default is
       0.0, i.e. the clock only advances through manuals calls to :meth:`jump`
       or when the :attr:`autojump_threshold` is triggered. You can assign to
       this attribute to change it.
 
    .. attribute:: autojump_threshold
 
       The clock keeps an eye on the run loop, and if at any point it detects
       that all tasks have been blocked for this many real seconds (i.e.,
       according to the actual clock, not this clock), then the clock
       automatically jumps ahead to the run loop's next scheduled
       timeout. Default is :data:`math.inf`, i.e., to never autojump. You can
       assign to this attribute to change it.
 
       Basically the idea is that if you have code or tests that use sleeps
       and timeouts, you can use this to make it run much faster, totally
       automatically. (At least, as long as those sleeps/timeouts are
       happening inside Trio; if your test involves talking to external
       service and waiting for it to timeout then obviously we can't help you
       there.)
 
       You should set this to the smallest value that lets you reliably avoid
       "false alarms" where some I/O is in flight (e.g. between two halves of
       a socketpair) but the threshold gets triggered and time gets advanced
       anyway. This will depend on the details of your tests and test
       environment. If you aren't doing any I/O (like in our sleeping example
       above) then just set it to zero, and the clock will jump whenever all
       tasks are blocked.
 
       .. note:: If you use ``autojump_threshold`` and
          `wait_all_tasks_blocked` at the same time, then you might wonder how
          they interact, since they both cause things to happen after the run
          loop goes idle for some time. The answer is:
          `wait_all_tasks_blocked` takes priority. If there's a task blocked
          in `wait_all_tasks_blocked`, then the autojump feature treats that
          as active task and does *not* jump the clock.
 
    çcCs0d|_d|_d|_d|_tj|_||_||_dS)Nr
)    Ú
_real_baseÚ _virtual_baseÚ_rateÚ_autojump_thresholdÚtimeÚ perf_counterÚ _real_clockÚrateÚautojump_threshold)Úselfrr©rúMd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\trio/_core/_mock_clock.pyÚ__init__AszMockClock.__init__cCsd | ¡|jt|ƒ¡S)Nz)<MockClock, time={:.7f}, rate={} @ {:#x}>)ÚformatÚ current_timer Úid©rrrrÚ__repr__Ps
ÿzMockClock.__repr__cCs|jS©N)r rrrrrUszMockClock.ratecCs>|dkrtdƒ‚n(| ¡}| |¡}||_||_t|ƒ|_dS)Nrzrate must be >= 0)Ú
ValueErrorrÚ_real_to_virtualr r Úfloatr )rZnew_rateÚrealZvirtualrrrrYs
 
cCs|jSr)rrrrrrdszMockClock.autojump_thresholdcCst|ƒ|_| ¡dSr)r rÚ_try_resync_autojump_threshold)rZnew_autojump_thresholdrrrrhs
cCs:ztj}|jr| ¡Wntk
r,Yn
X|j|_dSr)rÚrunnerZis_guestZforce_guest_tick_asapÚAttributeErrorrZclock_autojump_threshold)rr#rrrr"ss z(MockClock._try_resync_autojump_thresholdcCs4t ¡}|j}d|kr"tkr0nn
| |¡dS)Nr)rZcurrent_statisticsZseconds_to_next_deadlinerÚjump)rZ
statisticsr%rrrÚ    _autojumpszMockClock._autojumpcCs||j}|j|}|j|Sr)r r r )rr!Z real_offsetZvirtual_offsetrrrr…s
 
zMockClock._real_to_virtualcCs | ¡dSr)r"rrrrÚ start_clockŠszMockClock.start_clockcCs| | ¡¡Sr)rrrrrrrszMockClock.current_timecCs4|| ¡}|dkrdS|jdkr,||jSdSdS)Nriÿɚ;)rr )rÚdeadlineZvirtual_timeoutrrrÚdeadline_to_sleep_times  
 
z MockClock.deadline_to_sleep_timecCs"|dkrtdƒ‚|j|7_dS)zûManually advance the clock by the given number of seconds.
 
        Args:
          seconds (float): the number of seconds to jump the clock forward.
 
        Raises:
          ValueError: if you try to pass a negative value for ``seconds``.
 
        rztime can't go backwardsN)rr )rÚsecondsrrrr%™s
zMockClock.jumpN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrrÚpropertyrÚsetterrr"r&rr'rr)r%rrrrr    s$/
 
 
 
 
 
     r    )Ú    metaclass) rÚmathrÚrZ_runrÚ_abcrZ_utilrr    rrrrÚ<module>s