Commit 4bde7d76 authored by Julien Muchembled's avatar Julien Muchembled

Code clean up: Connection

parent dfa346a6
...@@ -719,29 +719,17 @@ class MTClientConnection(ClientConnection): ...@@ -719,29 +719,17 @@ class MTClientConnection(ClientConnection):
with lock: with lock:
super(MTClientConnection, self).__init__(*args, **kwargs) super(MTClientConnection, self).__init__(*args, **kwargs)
# Alias without lock (cheaper than super())
_ask = ClientConnection.ask.__func__
def ask(self, packet, timeout=CRITICAL_TIMEOUT, on_timeout=None, def ask(self, packet, timeout=CRITICAL_TIMEOUT, on_timeout=None,
queue=None, **kw): queue=None, **kw):
with self.lock: with self.lock:
if self.isClosed():
raise ConnectionClosed
# XXX: Here, we duplicate Connection.ask because we need to call
# self.dispatcher.register after setId is called and before
# _addPacket is called.
msg_id = self._getNextId()
packet.setId(msg_id)
if queue is None: if queue is None:
if type(packet) is not Packets.Ping: if type(packet) is Packets.Ping:
raise TypeError, 'Only Ping packet can be asked ' \ return self._ask(packet, timeout, on_timeout, **kw)
'without a queue, got a %r.' % (packet, ) raise TypeError('Only Ping packet can be asked'
else: ' without a queue, got a %r.' % packet)
self.dispatcher.register(self, msg_id, queue) msg_id = self._ask(packet, timeout, on_timeout, **kw)
self._addPacket(packet) self.dispatcher.register(self, msg_id, queue)
handlers = self._handlers return msg_id
t = None if handlers.isPending() else time()
handlers.emit(packet, timeout, on_timeout, kw)
if not self._queue:
next_timeout = self._next_timeout
self.updateTimeout(t)
if self._next_timeout < next_timeout:
self.em.wakeup()
return msg_id
...@@ -25,12 +25,6 @@ from neo.master.handlers.election import ClientElectionHandler, \ ...@@ -25,12 +25,6 @@ from neo.master.handlers.election import ClientElectionHandler, \
from neo.lib.exception import ElectionFailure from neo.lib.exception import ElectionFailure
from neo.lib.connection import ClientConnection from neo.lib.connection import ClientConnection
# patch connection so that we can register _addPacket messages
# in mock object
def _addPacket(self, packet):
if self.connector is not None:
self.connector._addPacket(packet)
class MasterClientElectionTestBase(NeoUnitTestBase): class MasterClientElectionTestBase(NeoUnitTestBase):
def setUp(self): def setUp(self):
...@@ -67,13 +61,6 @@ class MasterClientElectionTests(MasterClientElectionTestBase): ...@@ -67,13 +61,6 @@ class MasterClientElectionTests(MasterClientElectionTestBase):
self.election = ClientElectionHandler(self.app) self.election = ClientElectionHandler(self.app)
self.app.unconnected_master_node_set = set() self.app.unconnected_master_node_set = set()
self.app.negotiating_master_node_set = set() self.app.negotiating_master_node_set = set()
# apply monkey patches
ClientConnection._addPacket = _addPacket
def _tearDown(self, success):
# restore patched methods
del ClientConnection._addPacket
NeoUnitTestBase._tearDown(self, success)
def _checkUnconnected(self, node): def _checkUnconnected(self, node):
addr = node.getAddress() addr = node.getAddress()
...@@ -223,13 +210,6 @@ class MasterServerElectionTests(MasterClientElectionTestBase): ...@@ -223,13 +210,6 @@ class MasterServerElectionTests(MasterClientElectionTestBase):
self.client_address = (self.local_ip, 1000) self.client_address = (self.local_ip, 1000)
self.storage_address = (self.local_ip, 2000) self.storage_address = (self.local_ip, 2000)
self.master_address = (self.local_ip, 3000) self.master_address = (self.local_ip, 3000)
# apply monkey patches
ClientConnection._addPacket = _addPacket
def _tearDown(self, success):
NeoUnitTestBase._tearDown(self, success)
# restore environment
del ClientConnection._addPacket
def test_requestIdentification1(self): def test_requestIdentification1(self):
""" A non-master node request identification """ """ A non-master node request identification """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment