forked from 0rpc/zerorpc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client_heartbeat.py
More file actions
194 lines (133 loc) · 4.84 KB
/
test_client_heartbeat.py
File metadata and controls
194 lines (133 loc) · 4.84 KB
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
# -*- coding: utf-8 -*-
# Open Source Initiative OSI - The MIT License (MIT):Licensing
#
# The MIT License (MIT)
# Copyright (c) 2012 DotCloud Inc ([email protected])
#
# 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 gevent
import zerorpc
from testutils import teardown, random_ipc_endpoint
def test_client_server_hearbeat():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def lolita(self):
return 42
def slow(self):
gevent.sleep(10)
srv = MySrv(heartbeat=1)
srv.bind(endpoint)
gevent.spawn(srv.run)
client = zerorpc.Client(heartbeat=1)
client.connect(endpoint)
assert client.lolita() == 42
print 'GOT ANSWER'
def test_client_server_activate_heartbeat():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def lolita(self):
gevent.sleep(3)
return 42
srv = MySrv(heartbeat=1)
srv.bind(endpoint)
gevent.spawn(srv.run)
gevent.sleep(0)
client = zerorpc.Client(heartbeat=1)
client.connect(endpoint)
assert client.lolita() == 42
print 'GOT ANSWER'
def test_client_server_passive_hearbeat():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def lolita(self):
return 42
def slow(self):
gevent.sleep(3)
return 2
srv = MySrv(heartbeat=1)
srv.bind(endpoint)
gevent.spawn(srv.run)
client = zerorpc.Client(heartbeat=1, passive_heartbeat=True)
client.connect(endpoint)
assert client.slow() == 2
print 'GOT ANSWER'
def test_client_hb_doesnt_linger_on_streaming():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
@zerorpc.stream
def iter(self):
return xrange(42)
srv = MySrv(heartbeat=1, context=zerorpc.Context())
srv.bind(endpoint)
gevent.spawn(srv.run)
client1 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
def test_client():
assert list(client1.iter()) == list(xrange(42))
print 'sleep 3s'
gevent.sleep(3)
gevent.spawn(test_client).join()
def est_client_drop_few():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
def lolita(self):
return 42
srv = MySrv(heartbeat=1, context=zerorpc.Context())
srv.bind(endpoint)
gevent.spawn(srv.run)
client1 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
client2 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
client3 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
assert client1.lolita() == 42
assert client2.lolita() == 42
gevent.sleep(3)
assert client3.lolita() == 42
def test_client_drop_empty_stream():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
@zerorpc.stream
def iter(self):
return []
srv = MySrv(heartbeat=1, context=zerorpc.Context())
srv.bind(endpoint)
gevent.spawn(srv.run)
client1 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
def test_client():
print 'grab iter'
i = client1.iter()
print 'sleep 3s'
gevent.sleep(3)
gevent.spawn(test_client).join()
def test_client_drop_stream():
endpoint = random_ipc_endpoint()
class MySrv(zerorpc.Server):
@zerorpc.stream
def iter(self):
return xrange(500)
srv = MySrv(heartbeat=1, context=zerorpc.Context())
srv.bind(endpoint)
gevent.spawn(srv.run)
client1 = zerorpc.Client(endpoint, heartbeat=1, context=zerorpc.Context())
def test_client():
print 'grab iter'
i = client1.iter()
print 'consume some'
assert list(next(i) for x in xrange(142)) == list(xrange(142))
print 'sleep 3s'
gevent.sleep(3)
gevent.spawn(test_client).join()