2
2
import errno
3
3
import logging
4
4
import socket
5
+ from typing import Type
5
6
import unittest
6
7
from unittest .mock import Mock , patch , sentinel
7
8
8
9
import pykka
9
10
10
- from mopidy_mpd import network , uri_mapper
11
+ from mopidy_mpd import network , types , uri_mapper
12
+ from mopidy_mpd .session import MpdSession
11
13
from tests import IsA , any_int , any_unicode
12
14
13
15
14
16
class ConnectionTest (unittest .TestCase ):
17
+ _empty_config = types .Config ({}) # type: ignore
18
+
19
+ @property
20
+ def _mock_protocol (self ) -> Type [MpdSession ]:
21
+ return Mock (spec = network .LineProtocol ) # type: ignore
22
+
15
23
def setUp (self ):
16
24
self .mock = Mock (spec = network .Connection )
17
25
self .loop = asyncio .new_event_loop ()
18
26
asyncio .set_event_loop (self .loop )
19
27
20
28
def create_connection (self ):
21
29
conn = network .Connection (
22
- config = {}, # type: ignore
30
+ config = self . _empty_config ,
23
31
core = Mock (),
24
32
uri_map = Mock (spec = uri_mapper .MpdUriMapper ),
25
- protocol = Mock ( spec = network . LineProtocol ), # type: ignore
33
+ protocol = self . _mock_protocol ,
26
34
sock = Mock (spec = socket .SocketType ),
27
35
addr = (sentinel .host , sentinel .port ),
28
36
timeout = 1 ,
@@ -38,10 +46,10 @@ def test_init_ensure_nonblocking_io(self):
38
46
39
47
network .Connection .__init__ (
40
48
self .mock ,
41
- config = {}, # type: ignore
49
+ config = self . _empty_config ,
42
50
core = Mock (),
43
51
uri_map = Mock (spec = uri_mapper .MpdUriMapper ),
44
- protocol = Mock ( spec = network . LineProtocol ), # type: ignore
52
+ protocol = self . _mock_protocol ,
45
53
sock = sock ,
46
54
addr = (sentinel .host , sentinel .port ),
47
55
timeout = sentinel .timeout ,
@@ -50,14 +58,14 @@ def test_init_ensure_nonblocking_io(self):
50
58
sock .setblocking .assert_called_once_with (False )
51
59
52
60
def test_init_starts_actor (self ):
53
- protocol = Mock ( spec = network . LineProtocol )
61
+ protocol = self . _mock_protocol
54
62
55
63
network .Connection .__init__ (
56
64
self .mock ,
57
- config = {}, # type: ignore
65
+ config = self . _empty_config ,
58
66
core = Mock (),
59
67
uri_map = Mock (spec = uri_mapper .MpdUriMapper ),
60
- protocol = protocol , # type: ignore
68
+ protocol = protocol ,
61
69
sock = Mock (spec = socket .SocketType ),
62
70
addr = (sentinel .host , sentinel .port ),
63
71
timeout = sentinel .timeout ,
@@ -67,15 +75,15 @@ def test_init_starts_actor(self):
67
75
68
76
def test_init_stores_values_in_attributes (self ):
69
77
addr = (sentinel .host , sentinel .port )
70
- protocol = Mock ( spec = network . LineProtocol )
78
+ protocol = self . _mock_protocol
71
79
sock = Mock (spec = socket .SocketType )
72
80
73
81
network .Connection .__init__ (
74
82
self .mock ,
75
- config = {}, # type: ignore
83
+ config = self . _empty_config ,
76
84
core = Mock (),
77
85
uri_map = Mock (spec = uri_mapper .MpdUriMapper ),
78
- protocol = protocol , # type: ignore
86
+ protocol = protocol ,
79
87
sock = sock ,
80
88
addr = addr ,
81
89
timeout = sentinel .timeout ,
@@ -94,15 +102,14 @@ def test_init_handles_ipv6_addr(self):
94
102
sentinel .flowinfo ,
95
103
sentinel .scopeid ,
96
104
)
97
- protocol = Mock (spec = network .LineProtocol )
98
105
sock = Mock (spec = socket .SocketType )
99
106
100
107
network .Connection .__init__ (
101
108
self .mock ,
102
- config = {}, # type: ignore
109
+ config = self . _empty_config ,
103
110
core = Mock (),
104
111
uri_map = Mock (spec = uri_mapper .MpdUriMapper ),
105
- protocol = protocol , # type: ignore
112
+ protocol = self . _mock_protocol ,
106
113
sock = sock ,
107
114
addr = addr ,
108
115
timeout = sentinel .timeout ,
@@ -195,29 +202,27 @@ def test_queue_send_calls_send(self):
195
202
conn .send_buffer = b""
196
203
197
204
asyncio .run (conn .queue_send (b"data" ))
198
- conn ._loop .sock_sendall .assert_called_once_with ( # type: ignore
199
- IsA (Mock ), b"data"
200
- )
205
+ conn ._loop .sock_sendall .assert_called_once_with (IsA (Mock ), b"data" )
201
206
assert conn .send_buffer == b""
202
207
203
208
def test_recv_callback_sends_data_to_actor (self ):
204
209
conn = self .create_connection ()
205
- conn ._sock .recv .return_value = b"data" # type: ignore
210
+ conn ._sock .recv .return_value = b"data"
206
211
207
212
assert asyncio .run (conn .recv ())
208
213
conn .actor_ref .tell .assert_called_once_with ({"received" : b"data" })
209
214
210
215
def test_recv_callback_handles_dead_actors (self ):
211
216
conn = self .create_connection ()
212
- conn ._sock .recv .return_value = b"data" # type: ignore
217
+ conn ._sock .recv .return_value = b"data"
213
218
conn .actor_ref .tell .side_effect = pykka .ActorDeadError ()
214
219
215
220
assert not asyncio .run (conn .recv ())
216
221
conn .actor_ref .stop .assert_called_once ()
217
222
218
223
def test_recv_callback_gets_no_data (self ):
219
224
conn = self .create_connection ()
220
- conn ._sock .recv .return_value = b"" # type: ignore
225
+ conn ._sock .recv .return_value = b""
221
226
222
227
assert not asyncio .run (conn .recv ())
223
228
assert conn .actor_ref .mock_calls == [
@@ -229,7 +234,7 @@ def test_recv_callback_recoverable_error(self):
229
234
conn ._loop = Mock (spec = asyncio .AbstractEventLoop )
230
235
231
236
for error in (errno .EWOULDBLOCK , errno .EINTR ):
232
- conn ._loop .sock_recv .side_effect = OSError (error , "" ) # type: ignore
237
+ conn ._loop .sock_recv .side_effect = OSError (error , "" )
233
238
assert asyncio .run (conn .recv ())
234
239
assert conn .actor_ref .stop .call_count == 0
235
240
@@ -245,12 +250,10 @@ def test_send_callback_sends_all_data(self):
245
250
conn = self .create_connection ()
246
251
conn .send_buffer = b"data"
247
252
conn ._loop = Mock (spec = asyncio .AbstractEventLoop )
248
- conn ._loop .sock_sendall .return_value = None # type: ignore
253
+ conn ._loop .sock_sendall .return_value = None
249
254
250
255
asyncio .run (conn .send (conn .send_buffer ))
251
- conn ._loop .sock_sendall .assert_called_once_with ( # type: ignore
252
- IsA (Mock ), b"data"
253
- )
256
+ conn ._loop .sock_sendall .assert_called_once_with (IsA (Mock ), b"data" )
254
257
255
258
def test_send_recoverable_error (self ):
256
259
conn = self .create_connection ()
0 commit comments