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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
import errno
import logging
import socket
import unittest
from unittest.mock import Mock, call, patch, sentinel
import pykka
from gi.repository import GLib
from mopidy_mpd import network
from tests import any_int, any_unicode
class ConnectionTest(unittest.TestCase):
def setUp(self): # noqa: N802
self.mock = Mock(spec=network.Connection)
def test_init_ensure_nonblocking_io(self):
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock,
Mock(),
{},
sock,
(sentinel.host, sentinel.port),
sentinel.timeout,
)
sock.setblocking.assert_called_once_with(False)
def test_init_starts_actor(self):
protocol = Mock(spec=network.LineProtocol)
network.Connection.__init__(
self.mock,
protocol,
{},
Mock(),
(sentinel.host, sentinel.port),
sentinel.timeout,
)
protocol.start.assert_called_once_with(self.mock)
def test_init_enables_recv_and_timeout(self):
network.Connection.__init__(
self.mock,
Mock(),
{},
Mock(),
(sentinel.host, sentinel.port),
sentinel.timeout,
)
self.mock.enable_recv.assert_called_once_with()
self.mock.enable_timeout.assert_called_once_with()
def test_init_stores_values_in_attributes(self):
addr = (sentinel.host, sentinel.port)
protocol = Mock(spec=network.LineProtocol)
protocol_kwargs = {}
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
)
assert sock == self.mock._sock
assert protocol == self.mock.protocol
assert protocol_kwargs == self.mock.protocol_kwargs
assert sentinel.timeout == self.mock.timeout
assert sentinel.host == self.mock.host
assert sentinel.port == self.mock.port
def test_init_handles_ipv6_addr(self):
addr = (
sentinel.host,
sentinel.port,
sentinel.flowinfo,
sentinel.scopeid,
)
protocol = Mock(spec=network.LineProtocol)
protocol_kwargs = {}
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
)
assert sentinel.host == self.mock.host
assert sentinel.port == self.mock.port
def test_stop_disables_recv_send_and_timeout(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.disable_timeout.assert_called_once_with()
self.mock.disable_recv.assert_called_once_with()
self.mock.disable_send.assert_called_once_with()
def test_stop_closes_socket(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock._sock.close.assert_called_once_with()
def test_stop_closes_socket_error(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.close.side_effect = socket.error
network.Connection.stop(self.mock, sentinel.reason)
self.mock._sock.close.assert_called_once_with()
def test_stop_stops_actor(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.actor_ref.stop.assert_called_once_with(block=False)
def test_stop_handles_actor_already_being_stopped(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock.actor_ref.stop.side_effect = pykka.ActorDeadError()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.actor_ref.stop.assert_called_once_with(block=False)
def test_stop_sets_stopping_to_true(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
assert self.mock.stopping is True
def test_stop_does_not_proceed_when_already_stopping(self):
self.mock.stopping = True
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
assert 0 == self.mock.actor_ref.stop.call_count
assert 0 == self.mock._sock.close.call_count
@patch.object(network.logger, "log", new=Mock())
def test_stop_logs_reason(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
network.logger.log.assert_called_once_with(
logging.DEBUG, sentinel.reason
)
@patch.object(network.logger, "log", new=Mock())
def test_stop_logs_reason_with_level(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(
self.mock, sentinel.reason, level=sentinel.level
)
network.logger.log.assert_called_once_with(
sentinel.level, sentinel.reason
)
@patch.object(network.logger, "log", new=Mock())
def test_stop_logs_that_it_is_calling_itself(self):
self.mock.stopping = True
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
network.logger.log(any_int, any_unicode)
@patch.object(GLib, "io_add_watch", new=Mock())
def test_enable_recv_registers_with_glib(self):
self.mock.recv_id = None
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.fileno.return_value = sentinel.fileno
GLib.io_add_watch.return_value = sentinel.tag
network.Connection.enable_recv(self.mock)
GLib.io_add_watch.assert_called_once_with(
sentinel.fileno,
GLib.IO_IN | GLib.IO_ERR | GLib.IO_HUP,
self.mock.recv_callback,
)
assert sentinel.tag == self.mock.recv_id
@patch.object(GLib, "io_add_watch", new=Mock())
def test_enable_recv_already_registered(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock.recv_id = sentinel.tag
network.Connection.enable_recv(self.mock)
assert 0 == GLib.io_add_watch.call_count
def test_enable_recv_does_not_change_tag(self):
self.mock.recv_id = sentinel.tag
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.enable_recv(self.mock)
assert sentinel.tag == self.mock.recv_id
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_recv_deregisters(self):
self.mock.recv_id = sentinel.tag
network.Connection.disable_recv(self.mock)
GLib.source_remove.assert_called_once_with(sentinel.tag)
assert self.mock.recv_id is None
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_recv_already_deregistered(self):
self.mock.recv_id = None
network.Connection.disable_recv(self.mock)
assert 0 == GLib.source_remove.call_count
assert self.mock.recv_id is None
def test_enable_recv_on_closed_socket(self):
self.mock.recv_id = None
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.fileno.side_effect = socket.error(errno.EBADF, "")
network.Connection.enable_recv(self.mock)
self.mock.stop.assert_called_once_with(any_unicode)
assert self.mock.recv_id is None
@patch.object(GLib, "io_add_watch", new=Mock())
def test_enable_send_registers_with_glib(self):
self.mock.send_id = None
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.fileno.return_value = sentinel.fileno
GLib.io_add_watch.return_value = sentinel.tag
network.Connection.enable_send(self.mock)
GLib.io_add_watch.assert_called_once_with(
sentinel.fileno,
GLib.IO_OUT | GLib.IO_ERR | GLib.IO_HUP,
self.mock.send_callback,
)
assert sentinel.tag == self.mock.send_id
@patch.object(GLib, "io_add_watch", new=Mock())
def test_enable_send_already_registered(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock.send_id = sentinel.tag
network.Connection.enable_send(self.mock)
assert 0 == GLib.io_add_watch.call_count
def test_enable_send_does_not_change_tag(self):
self.mock.send_id = sentinel.tag
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.enable_send(self.mock)
assert sentinel.tag == self.mock.send_id
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_send_deregisters(self):
self.mock.send_id = sentinel.tag
network.Connection.disable_send(self.mock)
GLib.source_remove.assert_called_once_with(sentinel.tag)
assert self.mock.send_id is None
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_send_already_deregistered(self):
self.mock.send_id = None
network.Connection.disable_send(self.mock)
assert 0 == GLib.source_remove.call_count
assert self.mock.send_id is None
def test_enable_send_on_closed_socket(self):
self.mock.send_id = None
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.fileno.side_effect = socket.error(errno.EBADF, "")
network.Connection.enable_send(self.mock)
assert self.mock.send_id is None
@patch.object(GLib, "timeout_add_seconds", new=Mock())
def test_enable_timeout_clears_existing_timeouts(self):
self.mock.timeout = 10
network.Connection.enable_timeout(self.mock)
self.mock.disable_timeout.assert_called_once_with()
@patch.object(GLib, "timeout_add_seconds", new=Mock())
def test_enable_timeout_add_glib_timeout(self):
self.mock.timeout = 10
GLib.timeout_add_seconds.return_value = sentinel.tag
network.Connection.enable_timeout(self.mock)
GLib.timeout_add_seconds.assert_called_once_with(
10, self.mock.timeout_callback
)
assert sentinel.tag == self.mock.timeout_id
@patch.object(GLib, "timeout_add_seconds", new=Mock())
def test_enable_timeout_does_not_add_timeout(self):
self.mock.timeout = 0
network.Connection.enable_timeout(self.mock)
assert 0 == GLib.timeout_add_seconds.call_count
self.mock.timeout = -1
network.Connection.enable_timeout(self.mock)
assert 0 == GLib.timeout_add_seconds.call_count
self.mock.timeout = None
network.Connection.enable_timeout(self.mock)
assert 0 == GLib.timeout_add_seconds.call_count
def test_enable_timeout_does_not_call_disable_for_invalid_timeout(self):
self.mock.timeout = 0
network.Connection.enable_timeout(self.mock)
assert 0 == self.mock.disable_timeout.call_count
self.mock.timeout = -1
network.Connection.enable_timeout(self.mock)
assert 0 == self.mock.disable_timeout.call_count
self.mock.timeout = None
network.Connection.enable_timeout(self.mock)
assert 0 == self.mock.disable_timeout.call_count
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_timeout_deregisters(self):
self.mock.timeout_id = sentinel.tag
network.Connection.disable_timeout(self.mock)
GLib.source_remove.assert_called_once_with(sentinel.tag)
assert self.mock.timeout_id is None
@patch.object(GLib, "source_remove", new=Mock())
def test_disable_timeout_already_deregistered(self):
self.mock.timeout_id = None
network.Connection.disable_timeout(self.mock)
assert 0 == GLib.source_remove.call_count
assert self.mock.timeout_id is None
def test_queue_send_acquires_and_releases_lock(self):
self.mock.send_lock = Mock()
self.mock.send_buffer = b""
network.Connection.queue_send(self.mock, b"data")
self.mock.send_lock.acquire.assert_called_once_with(True)
self.mock.send_lock.release.assert_called_once_with()
def test_queue_send_calls_send(self):
self.mock.send_buffer = b""
self.mock.send_lock = Mock()
self.mock.send.return_value = b""
network.Connection.queue_send(self.mock, b"data")
self.mock.send.assert_called_once_with(b"data")
assert 0 == self.mock.enable_send.call_count
assert b"" == self.mock.send_buffer
def test_queue_send_calls_enable_send_for_partial_send(self):
self.mock.send_buffer = b""
self.mock.send_lock = Mock()
self.mock.send.return_value = b"ta"
network.Connection.queue_send(self.mock, b"data")
self.mock.send.assert_called_once_with(b"data")
self.mock.enable_send.assert_called_once_with()
assert b"ta" == self.mock.send_buffer
def test_queue_send_calls_send_with_existing_buffer(self):
self.mock.send_buffer = b"foo"
self.mock.send_lock = Mock()
self.mock.send.return_value = b""
network.Connection.queue_send(self.mock, b"bar")
self.mock.send.assert_called_once_with(b"foobar")
assert 0 == self.mock.enable_send.call_count
assert b"" == self.mock.send_buffer
def test_recv_callback_respects_io_err(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock.actor_ref = Mock()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_ERR)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_recv_callback_respects_io_hup(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock.actor_ref = Mock()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_HUP)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_recv_callback_respects_io_hup_and_io_err(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock.actor_ref = Mock()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, ((GLib.IO_IN | GLib.IO_HUP) | GLib.IO_ERR)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_recv_callback_sends_data_to_actor(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.recv.return_value = b"data"
self.mock.actor_ref = Mock()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.actor_ref.tell.assert_called_once_with({"received": b"data"})
def test_recv_callback_handles_dead_actors(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.recv.return_value = b"data"
self.mock.actor_ref = Mock()
self.mock.actor_ref.tell.side_effect = pykka.ActorDeadError()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_recv_callback_gets_no_data(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.recv.return_value = b""
self.mock.actor_ref = Mock()
assert network.Connection.recv_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
assert self.mock.mock_calls == [
call._sock.recv(any_int),
call.disable_recv(),
call.actor_ref.tell({"close": True}),
]
def test_recv_callback_recoverable_error(self):
self.mock._sock = Mock(spec=socket.SocketType)
for error in (errno.EWOULDBLOCK, errno.EINTR):
self.mock._sock.recv.side_effect = socket.error(error, "")
assert network.Connection.recv_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
assert 0 == self.mock.stop.call_count
def test_recv_callback_unrecoverable_error(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.recv.side_effect = socket.error
assert network.Connection.recv_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_send_callback_respects_io_err(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 1
self.mock.send_lock = Mock()
self.mock.actor_ref = Mock()
self.mock.send_buffer = b""
assert network.Connection.send_callback(
self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_ERR)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_send_callback_respects_io_hup(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 1
self.mock.send_lock = Mock()
self.mock.actor_ref = Mock()
self.mock.send_buffer = b""
assert network.Connection.send_callback(
self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_HUP)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_send_callback_respects_io_hup_and_io_err(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 1
self.mock.send_lock = Mock()
self.mock.actor_ref = Mock()
self.mock.send_buffer = b""
assert network.Connection.send_callback(
self.mock, sentinel.fd, ((GLib.IO_IN | GLib.IO_HUP) | GLib.IO_ERR)
)
self.mock.stop.assert_called_once_with(any_unicode)
def test_send_callback_acquires_and_releases_lock(self):
self.mock.send_lock = Mock()
self.mock.send_lock.acquire.return_value = True
self.mock.send_buffer = b""
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 0
assert network.Connection.send_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.send_lock.acquire.assert_called_once_with(False)
self.mock.send_lock.release.assert_called_once_with()
def test_send_callback_fails_to_acquire_lock(self):
self.mock.send_lock = Mock()
self.mock.send_lock.acquire.return_value = False
self.mock.send_buffer = b""
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 0
assert network.Connection.send_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.send_lock.acquire.assert_called_once_with(False)
assert 0 == self.mock._sock.send.call_count
def test_send_callback_sends_all_data(self):
self.mock.send_lock = Mock()
self.mock.send_lock.acquire.return_value = True
self.mock.send_buffer = b"data"
self.mock.send.return_value = b""
assert network.Connection.send_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.disable_send.assert_called_once_with()
self.mock.send.assert_called_once_with(b"data")
assert b"" == self.mock.send_buffer
def test_send_callback_sends_partial_data(self):
self.mock.send_lock = Mock()
self.mock.send_lock.acquire.return_value = True
self.mock.send_buffer = b"data"
self.mock.send.return_value = b"ta"
assert network.Connection.send_callback(
self.mock, sentinel.fd, GLib.IO_IN
)
self.mock.send.assert_called_once_with(b"data")
assert b"ta" == self.mock.send_buffer
def test_send_recoverable_error(self):
self.mock._sock = Mock(spec=socket.SocketType)
for error in (errno.EWOULDBLOCK, errno.EINTR):
self.mock._sock.send.side_effect = socket.error(error, "")
network.Connection.send(self.mock, b"data")
assert 0 == self.mock.stop.call_count
def test_send_calls_socket_send(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 4
assert b"" == network.Connection.send(self.mock, b"data")
self.mock._sock.send.assert_called_once_with(b"data")
def test_send_calls_socket_send_partial_send(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.return_value = 2
assert b"ta" == network.Connection.send(self.mock, b"data")
self.mock._sock.send.assert_called_once_with(b"data")
def test_send_unrecoverable_error(self):
self.mock._sock = Mock(spec=socket.SocketType)
self.mock._sock.send.side_effect = socket.error
assert b"" == network.Connection.send(self.mock, b"data")
self.mock.stop.assert_called_once_with(any_unicode)
def test_timeout_callback(self):
self.mock.timeout = 10
assert not network.Connection.timeout_callback(self.mock)
self.mock.stop.assert_called_once_with(any_unicode)
def test_str(self):
self.mock.host = "foo"
self.mock.port = 999
assert "[foo]:999" == network.Connection.__str__(self.mock)
def test_str_without_port(self):
self.mock.host = "foo"
self.mock.port = None
assert "[foo]" == network.Connection.__str__(self.mock)
|