/
test_api.py
233 lines (185 loc) · 7.01 KB
/
test_api.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#
# Evy - a concurrent networking library for Python
#
# Unless otherwise noted, the files in Evy are under the following MIT license:
#
# Copyright (c) 2012, Alvaro Saurin
# Copyright (c) 2008-2010, Eventlet Contributors (see AUTHORS)
# Copyright (c) 2007-2010, Linden Research, Inc.
# Copyright (c) 2005-2006, Bob Ippolito
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import os
import os.path
import socket
from unittest import TestCase, main
import warnings
import evy
warnings.simplefilter('ignore', DeprecationWarning)
from evy import api
warnings.simplefilter('default', DeprecationWarning)
from evy import greenio, util, hubs, greenthread, spawn
from tests import skip_if_no_ssl
def check_hub ():
# Clear through the descriptor queue
api.sleep(0)
api.sleep(0)
hub = hubs.get_hub()
for nm in 'get_readers', 'get_writers':
dct = getattr(hub, nm)()
assert not dct, "hub.%s not empty: %s" % (nm, dct)
# Stop the runloop (unless it's twistedhub which does not support that)
if not getattr(hub, 'uses_twisted_reactor', None):
hub.abort(True)
assert not hub.running
class TestApi(TestCase):
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt')
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key')
def test_tcp_listener (self):
socket = evy.listen(('0.0.0.0', 0))
assert socket.getsockname()[0] == '0.0.0.0'
socket.close()
check_hub()
def test_connect_tcp (self):
def accept_once (listenfd):
try:
conn, addr = listenfd.accept()
fd = conn.makefile(mode = 'w')
conn.close()
fd.write('hello\n')
fd.close()
finally:
listenfd.close()
server = evy.listen(('0.0.0.0', 0))
api.spawn(accept_once, server)
client = evy.connect(('127.0.0.1', server.getsockname()[1]))
fd = client.makefile()
client.close()
assert fd.readline() == 'hello\n'
assert fd.read() == ''
fd.close()
check_hub()
@skip_if_no_ssl
def test_connect_ssl (self):
def accept_once (listenfd):
try:
conn, addr = listenfd.accept()
conn.write('hello\r\n')
greenio.shutdown_safe(conn)
conn.close()
finally:
greenio.shutdown_safe(listenfd)
listenfd.close()
server = api.ssl_listener(('0.0.0.0', 0),
self.certificate_file,
self.private_key_file)
api.spawn(accept_once, server)
raw_client = evy.connect(('127.0.0.1', server.getsockname()[1]))
client = util.wrap_ssl(raw_client)
fd = socket._fileobject(client, 'rb', 8192)
assert fd.readline() == 'hello\r\n'
try:
self.assertEquals('', fd.read(10))
except greenio.SSL.ZeroReturnError:
# if it's a GreenSSL object it'll do this
pass
greenio.shutdown_safe(client)
client.close()
check_hub()
def test_001_trampoline_timeout (self):
from evy import coros
server_sock = evy.listen(('127.0.0.1', 0))
bound_port = server_sock.getsockname()[1]
def server (sock):
client, addr = sock.accept()
api.sleep(0.1)
server_evt = spawn(server, server_sock)
api.sleep(0)
try:
desc = evy.connect(('127.0.0.1', bound_port))
api.trampoline(desc, read = True, write = False, timeout = 0.001)
except api.TimeoutError:
pass # test passed
else:
assert False, "Didn't timeout"
server_evt.wait()
check_hub()
def test_timeout_cancel (self):
server = evy.listen(('0.0.0.0', 0))
bound_port = server.getsockname()[1]
done = [False]
def client_closer (sock):
while True:
(conn, addr) = sock.accept()
conn.close()
def go ():
desc = evy.connect(('127.0.0.1', bound_port))
try:
api.trampoline(desc, read = True, timeout = 0.1)
except api.TimeoutError:
assert False, "Timed out"
server.close()
desc.close()
done[0] = True
greenthread.spawn_after_local(0, go)
server_coro = api.spawn(client_closer, server)
while not done[0]:
api.sleep(0)
api.kill(server_coro)
check_hub()
def test_named (self):
named_foo = api.named('tests.api_test.Foo')
self.assertEquals(
named_foo.__name__,
"Foo")
def test_naming_missing_class (self):
self.assertRaises(
ImportError, api.named, 'this_name_should_hopefully_not_exist.Foo')
def test_killing_dormant (self):
DELAY = 0.1
state = []
def test ():
try:
state.append('start')
api.sleep(DELAY)
except:
state.append('except')
# catching GreenletExit
pass
# when switching to hub, hub makes itself the parent of this greenlet,
# thus after the function's done, the control will go to the parent
api.sleep(0)
state.append('finished')
g = api.spawn(test)
api.sleep(DELAY / 2)
self.assertEquals(state, ['start'])
api.kill(g)
# will not get there, unless switching is explicitly scheduled by kill
self.assertEquals(state, ['start', 'except'])
api.sleep(DELAY)
self.assertEquals(state, ['start', 'except', 'finished'])
def test_nested_with_timeout (self):
def func ():
return api.with_timeout(0.2, api.sleep, 2, timeout_value = 1)
self.assertRaises(api.TimeoutError, api.with_timeout, 0.1, func)
class Foo(object):
pass
if __name__ == '__main__':
main()