-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_py.py
125 lines (104 loc) · 2.43 KB
/
test_py.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import signal
import ev
def pipe():
r, w = os.pipe()
return os.fdopen(r, 'r'), os.fdopen(w, 'w')
class TestSimpleIO:
"""
>>> r, w = pipe()
>>> reader = TestSimpleIO(r)
>>> reader.io.is_active()
True
>>> reader.io.is_pending()
False
>>> w.write("Hello, world!")
>>> w.close()
>>> ev.main()
>>> reader.data
'Hello, world!'
>>> r1, w1 = pipe()
>>> reader.io.set(r1)
>>> w1.write('another chunk')
>>> w1.close()
>>> ev.main()
>>> reader.data
'another chunk'
>>> reader.io.is_active()
True
>>> reader.io.stop()
>>> reader.io.is_active()
False
>>> r.close()
>>> r1.close()
"""
def __init__(self, fd):
self.io = ev.IO(fd, cb=self.on_input)
self.io.start()
def on_input(self, io, events):
self.data = os.read(io.fileno(), 4096)
ev.quit()
class TestTimerOneShoot:
"""
>>> t = TestTimerOneShoot()
>>> t.timer.is_active()
True
>>> t.timer.is_pending()
False
>>> ev.main()
>>> t.timedout
True
>>> t.timer.stop()
>>> t.timer.is_active()
False
"""
def __init__(self):
self.timer = ev.Timer(.1, cb=self.on_timeout)
self.timer.start()
self.timedout = False
def on_timeout(self, timer, events):
self.timedout = True
ev.quit()
class TestTimerPeriodic:
"""
>>> t = TestTimerPeriodic()
>>> t.timer.is_active()
True
>>> t.timer.is_pending()
False
>>> ev.main()
>>> t.counter
10
>>> t.timer.stop()
>>> t.timer.is_active()
False
"""
def __init__(self):
self.timer = ev.Timer(.001, .001, cb=self.on_timeout)
self.timer.start()
self.counter = 0
def on_timeout(self, timer, events):
self.counter += 1
if self.counter >= 10:
ev.quit()
class TestSignal:
"""
>>> o = TestSignal()
>>> ev.main()
>>> o.interrupted
True
"""
interrupted = False
def __init__(self):
self.timer = ev.Timer(.001, cb=self.timer_event)
self.signal = ev.Signal(signal.SIGHUP, cb=self.sigint_event)
self.signal.start()
self.timer.start()
def timer_event(self, timer, events):
os.kill(os.getpid(), signal.SIGHUP)
def sigint_event(self, sig, events):
self.interrupted = True
ev.quit()
if __name__ == "__main__":
import doctest
doctest.testmod()