forked from miguelgrinberg/python-socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simple_client.py
More file actions
190 lines (159 loc) · 7.03 KB
/
Copy pathtest_simple_client.py
File metadata and controls
190 lines (159 loc) · 7.03 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
import asyncio
import unittest
from unittest import mock
import pytest
from socketio import AsyncSimpleClient
from socketio.exceptions import SocketIOError, TimeoutError, DisconnectedError
from .helpers import AsyncMock, _run
class TestAsyncAsyncSimpleClient(unittest.TestCase):
def test_constructor(self):
client = AsyncSimpleClient(1, '2', a='3', b=4)
assert client.client_args == (1, '2')
assert client.client_kwargs == {'a': '3', 'b': 4}
assert client.client is None
assert client.input_buffer == []
assert not client.connected
def test_connect(self):
client = AsyncSimpleClient(123, a='b')
with mock.patch('socketio.async_simple_client.AsyncClient') \
as mock_client:
mock_client.return_value.connect = AsyncMock()
_run(client.connect('url', headers='h', auth='a', transports='t',
namespace='n', socketio_path='s',
wait_timeout='w'))
mock_client.assert_called_once_with(123, a='b')
assert client.client == mock_client()
mock_client().connect.mock.assert_called_once_with(
'url', headers='h', auth='a', transports='t',
namespaces=['n'], socketio_path='s', wait_timeout='w')
mock_client().event.call_count == 3
mock_client().on.assert_called_once_with('*', namespace='n')
assert client.namespace == 'n'
assert not client.input_event.is_set()
def test_connect_context_manager(self):
async def _t():
async with AsyncSimpleClient(123, a='b') as client:
with mock.patch('socketio.async_simple_client.AsyncClient') \
as mock_client:
mock_client.return_value.connect = AsyncMock()
await client.connect('url', headers='h', auth='a',
transports='t', namespace='n',
socketio_path='s', wait_timeout='w')
mock_client.assert_called_once_with(123, a='b')
assert client.client == mock_client()
mock_client().connect.mock.assert_called_once_with(
'url', headers='h', auth='a', transports='t',
namespaces=['n'], socketio_path='s', wait_timeout='w')
mock_client().event.call_count == 3
mock_client().on.assert_called_once_with(
'*', namespace='n')
assert client.namespace == 'n'
assert not client.input_event.is_set()
_run(_t())
def test_connect_twice(self):
client = AsyncSimpleClient(123, a='b')
client.client = mock.MagicMock()
client.connected = True
with pytest.raises(RuntimeError):
_run(client.connect('url'))
def test_properties(self):
client = AsyncSimpleClient()
client.client = mock.MagicMock(transport='websocket')
client.client.get_sid.return_value = 'sid'
client.connected_event.set()
client.connected = True
assert client.sid == 'sid'
assert client.transport == 'websocket'
def test_emit(self):
client = AsyncSimpleClient()
client.client = mock.MagicMock()
client.client.emit = AsyncMock()
client.namespace = '/ns'
client.connected_event.set()
client.connected = True
_run(client.emit('foo', 'bar'))
client.client.emit.mock.assert_called_once_with('foo', 'bar',
namespace='/ns')
def test_emit_disconnected(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = False
with pytest.raises(DisconnectedError):
_run(client.emit('foo', 'bar'))
def test_emit_retries(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = True
client.client = mock.MagicMock()
client.client.emit = AsyncMock()
client.client.emit.mock.side_effect = [SocketIOError(), None]
_run(client.emit('foo', 'bar'))
client.client.emit.mock.assert_called_with('foo', 'bar', namespace='/')
def test_call(self):
client = AsyncSimpleClient()
client.client = mock.MagicMock()
client.client.call = AsyncMock()
client.client.call.mock.return_value = 'result'
client.namespace = '/ns'
client.connected_event.set()
client.connected = True
assert _run(client.call('foo', 'bar')) == 'result'
client.client.call.mock.assert_called_once_with(
'foo', 'bar', namespace='/ns', timeout=60)
def test_call_disconnected(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = False
with pytest.raises(DisconnectedError):
_run(client.call('foo', 'bar'))
def test_call_retries(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = True
client.client = mock.MagicMock()
client.client.call = AsyncMock()
client.client.call.mock.side_effect = [SocketIOError(), 'result']
assert _run(client.call('foo', 'bar')) == 'result'
client.client.call.mock.assert_called_with('foo', 'bar', namespace='/',
timeout=60)
def test_receive_with_input_buffer(self):
client = AsyncSimpleClient()
client.input_buffer = ['foo', 'bar']
assert _run(client.receive()) == 'foo'
assert _run(client.receive()) == 'bar'
def test_receive_without_input_buffer(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = True
client.input_event = mock.MagicMock()
async def fake_wait(timeout=None):
client.input_buffer = ['foo']
return True
client.input_event.wait = fake_wait
assert _run(client.receive()) == 'foo'
def test_receive_with_timeout(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = True
client.input_event = mock.MagicMock()
async def fake_wait(timeout=None):
await asyncio.sleep(1)
client.input_event.wait = fake_wait
with pytest.raises(TimeoutError):
_run(client.receive(timeout=0.01))
def test_receive_disconnected(self):
client = AsyncSimpleClient()
client.connected_event.set()
client.connected = False
with pytest.raises(DisconnectedError):
_run(client.receive())
def test_disconnect(self):
client = AsyncSimpleClient()
mc = mock.MagicMock()
mc.disconnect = AsyncMock()
client.client = mc
client.connected = True
_run(client.disconnect())
_run(client.disconnect())
mc.disconnect.mock.assert_called_once_with()
assert client.client is None