From 63cd153b1b2b3a58a4b5741c935c86735928f56f Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 28 Sep 2012 00:45:09 +0200 Subject: [PATCH] Let NetworkServer pass protocol_kwargs on --- mopidy/utils/network.py | 19 +++++++++++++++---- tests/utils/network/connection_test.py | 13 ++++++++----- tests/utils/network/server_test.py | 3 ++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/mopidy/utils/network.py b/mopidy/utils/network.py index 9cb8d74c..d2e0690b 100644 --- a/mopidy/utils/network.py +++ b/mopidy/utils/network.py @@ -11,11 +11,14 @@ from pykka.registry import ActorRegistry from mopidy.utils import locale_decode + logger = logging.getLogger('mopidy.utils.server') + class ShouldRetrySocketCall(Exception): """Indicate that attempted socket call should be retried""" + def try_ipv6_socket(): """Determine if system really supports IPv6""" if not socket.has_ipv6: @@ -28,9 +31,11 @@ def try_ipv6_socket(): 'creation failed, disabling: %s', locale_decode(error)) return False + #: Boolean value that indicates if creating an IPv6 socket will succeed. has_ipv6 = try_ipv6_socket() + def create_socket(): """Create a TCP socket with or without IPv6 depending on system support""" if has_ipv6: @@ -42,17 +47,21 @@ def create_socket(): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return sock + def format_hostname(hostname): """Format hostname for display.""" if (has_ipv6 and re.match('\d+.\d+.\d+.\d+', hostname) is not None): hostname = '::ffff:%s' % hostname return hostname + class Server(object): """Setup listener and register it with gobject's event loop.""" - def __init__(self, host, port, protocol, max_connections=5, timeout=30): + def __init__(self, host, port, protocol, protocol_kwargs=None, + max_connections=5, timeout=30): self.protocol = protocol + self.protocol_kwargs = protocol_kwargs or {} self.max_connections = max_connections self.timeout = timeout self.server_socket = self.create_server_socket(host, port) @@ -105,7 +114,8 @@ class Server(object): pass def init_connection(self, sock, addr): - Connection(self.protocol, sock, addr, self.timeout) + Connection(self.protocol, self.protocol_kwargs, + sock, addr, self.timeout) class Connection(object): @@ -117,13 +127,14 @@ class Connection(object): # false return value would only tell us that what we thought was registered # is already gone, there is really nothing more we can do. - def __init__(self, protocol, sock, addr, timeout): + def __init__(self, protocol, protocol_kwargs, sock, addr, timeout): sock.setblocking(False) self.host, self.port = addr[:2] # IPv6 has larger addr self.sock = sock self.protocol = protocol + self.protocol_kwargs = protocol_kwargs self.timeout = timeout self.send_lock = threading.Lock() @@ -135,7 +146,7 @@ class Connection(object): self.send_id = None self.timeout_id = None - self.actor_ref = self.protocol.start(self) + self.actor_ref = self.protocol.start(self, **self.protocol_kwargs) self.enable_recv() self.enable_timeout() diff --git a/tests/utils/network/connection_test.py b/tests/utils/network/connection_test.py index 96ddb833..25ae1940 100644 --- a/tests/utils/network/connection_test.py +++ b/tests/utils/network/connection_test.py @@ -17,19 +17,19 @@ class ConnectionTest(unittest.TestCase): def test_init_ensure_nonblocking_io(self): sock = Mock(spec=socket.SocketType) - network.Connection.__init__(self.mock, Mock(), sock, + 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(), + 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(), + 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() @@ -37,12 +37,14 @@ class ConnectionTest(unittest.TestCase): 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, sock, addr, sentinel.timeout) + self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout) self.assertEqual(sock, self.mock.sock) self.assertEqual(protocol, self.mock.protocol) + self.assertEqual(protocol_kwargs, self.mock.protocol_kwargs) self.assertEqual(sentinel.timeout, self.mock.timeout) self.assertEqual(sentinel.host, self.mock.host) self.assertEqual(sentinel.port, self.mock.port) @@ -51,10 +53,11 @@ class ConnectionTest(unittest.TestCase): 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, sock, addr, sentinel.timeout) + self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout) self.assertEqual(sentinel.host, self.mock.host) self.assertEqual(sentinel.port, self.mock.port) diff --git a/tests/utils/network/server_test.py b/tests/utils/network/server_test.py index e0399525..268b5dbd 100644 --- a/tests/utils/network/server_test.py +++ b/tests/utils/network/server_test.py @@ -164,10 +164,11 @@ class ServerTest(unittest.TestCase): @patch.object(network, 'Connection', new=Mock()) def test_init_connection(self): self.mock.protocol = sentinel.protocol + self.mock.protocol_kwargs = {} self.mock.timeout = sentinel.timeout network.Server.init_connection(self.mock, sentinel.sock, sentinel.addr) - network.Connection.assert_called_once_with(sentinel.protocol, + network.Connection.assert_called_once_with(sentinel.protocol, {}, sentinel.sock, sentinel.addr, sentinel.timeout) def test_reject_connection(self):