Commit 0d1ac173 authored by Grégory Wisniewski's avatar Grégory Wisniewski

First part of replacement of handleUnexpectedPacket calls with raises of

UnexpectedPacketError exception. Update client (with tests) and admin nodes.
Those exceptions are now catched in dispatch() and redirect to
unexpectedPacket().


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@502 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 1ca2c441
...@@ -24,7 +24,7 @@ from neo.protocol import INVALID_UUID, RUNNING_STATE, BROKEN_STATE, \ ...@@ -24,7 +24,7 @@ from neo.protocol import INVALID_UUID, RUNNING_STATE, BROKEN_STATE, \
from neo.node import MasterNode, StorageNode, ClientNode from neo.node import MasterNode, StorageNode, ClientNode
from neo.connection import ClientConnection from neo.connection import ClientConnection
from neo import protocol from neo import protocol
from neo.protocol import Packet from neo.protocol import Packet, UnexpectedPacketError
from neo.pt import PartitionTable from neo.pt import PartitionTable
from neo.exception import PrimaryFailure from neo.exception import PrimaryFailure
from neo.util import dump from neo.util import dump
...@@ -49,7 +49,7 @@ class MonitoringEventHandler(BaseEventHandler): ...@@ -49,7 +49,7 @@ class MonitoringEventHandler(BaseEventHandler):
def connectionAccepted(self, conn, s, addr): def connectionAccepted(self, conn, s, addr):
"""Called when a connection is accepted.""" """Called when a connection is accepted."""
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
def connectionCompleted(self, conn): def connectionCompleted(self, conn):
app = self.app app = self.app
...@@ -220,8 +220,7 @@ class MonitoringEventHandler(BaseEventHandler): ...@@ -220,8 +220,7 @@ class MonitoringEventHandler(BaseEventHandler):
logging.warning("handleSendPartitionTable") logging.warning("handleSendPartitionTable")
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
app = self.app app = self.app
nm = app.nm nm = app.nm
...@@ -251,8 +250,7 @@ class MonitoringEventHandler(BaseEventHandler): ...@@ -251,8 +250,7 @@ class MonitoringEventHandler(BaseEventHandler):
pt = app.pt pt = app.pt
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
node = app.nm.getNodeByUUID(uuid) node = app.nm.getNodeByUUID(uuid)
# This must be sent only by primary master node # This must be sent only by primary master node
...@@ -281,8 +279,7 @@ class MonitoringEventHandler(BaseEventHandler): ...@@ -281,8 +279,7 @@ class MonitoringEventHandler(BaseEventHandler):
logging.warning("handleNotifyNodeInformation") logging.warning("handleNotifyNodeInformation")
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
app = self.app app = self.app
nm = app.nm nm = app.nm
......
...@@ -20,7 +20,7 @@ import logging ...@@ -20,7 +20,7 @@ import logging
from neo.handler import EventHandler from neo.handler import EventHandler
from neo.connection import MTClientConnection from neo.connection import MTClientConnection
from neo import protocol from neo import protocol
from neo.protocol import Packet, \ from neo.protocol import Packet, UnexpectedPacketError, \
MASTER_NODE_TYPE, STORAGE_NODE_TYPE, CLIENT_NODE_TYPE, \ MASTER_NODE_TYPE, STORAGE_NODE_TYPE, CLIENT_NODE_TYPE, \
INVALID_UUID, RUNNING_STATE, TEMPORARILY_DOWN_STATE, \ INVALID_UUID, RUNNING_STATE, TEMPORARILY_DOWN_STATE, \
BROKEN_STATE, FEEDING_STATE, DISCARDED_STATE BROKEN_STATE, FEEDING_STATE, DISCARDED_STATE
...@@ -156,8 +156,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler): ...@@ -156,8 +156,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
def handleAnswerPrimaryMaster(self, conn, packet, primary_uuid, known_master_list): def handleAnswerPrimaryMaster(self, conn, packet, primary_uuid, known_master_list):
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
app = self.app app = self.app
node = app.nm.getNodeByUUID(uuid) node = app.nm.getNodeByUUID(uuid)
...@@ -199,8 +198,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler): ...@@ -199,8 +198,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
def handleNotifyNodeInformation(self, conn, packet, node_list): def handleNotifyNodeInformation(self, conn, packet, node_list):
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
app = self.app app = self.app
nm = app.nm nm = app.nm
...@@ -256,8 +254,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler): ...@@ -256,8 +254,7 @@ class PrimaryBoostrapEventHandler(BaseClientEventHandler):
# sendPartitionTable is only triggered after askPrimaryMaster. # sendPartitionTable is only triggered after askPrimaryMaster.
uuid = conn.getUUID() uuid = conn.getUUID()
if uuid is None: if uuid is None:
self.handleUnexpectedPacket(conn, packet) raise UnexpectedPacketError
return
app = self.app app = self.app
nm = app.nm nm = app.nm
......
...@@ -21,7 +21,7 @@ import logging ...@@ -21,7 +21,7 @@ import logging
import threading import threading
from mock import Mock, ReturnValues from mock import Mock, ReturnValues
from neo import protocol from neo import protocol
from neo.protocol import Packet, INVALID_UUID from neo.protocol import Packet, UnexpectedPacketError, INVALID_UUID
from neo.protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICATION, \ from neo.protocol import ERROR, REQUEST_NODE_IDENTIFICATION, ACCEPT_NODE_IDENTIFICATION, \
PING, PONG, ASK_PRIMARY_MASTER, ANSWER_PRIMARY_MASTER, ANNOUNCE_PRIMARY_MASTER, \ PING, PONG, ASK_PRIMARY_MASTER, ANSWER_PRIMARY_MASTER, ANNOUNCE_PRIMARY_MASTER, \
REELECT_PRIMARY_MASTER, NOTIFY_NODE_INFORMATION, START_OPERATION, \ REELECT_PRIMARY_MASTER, NOTIFY_NODE_INFORMATION, START_OPERATION, \
...@@ -342,25 +342,18 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -342,25 +342,18 @@ class ClientEventHandlerTest(unittest.TestCase):
original_handleUnexpectedPacket = client_handler.__class__.handleUnexpectedPacket original_handleUnexpectedPacket = client_handler.__class__.handleUnexpectedPacket
client_handler.__class__.handleUnexpectedPacket = ClientHandler_handleUnexpectedPacket client_handler.__class__.handleUnexpectedPacket = ClientHandler_handleUnexpectedPacket
try: try:
method(*args, **dict(kw)) self.assertRaises(UnexpectedPacketError, method, *args, **dict(kw))
finally: finally:
# Restore original method # Restore original method
client_handler.__class__.handleUnexpectedPacket = original_handleUnexpectedPacket client_handler.__class__.handleUnexpectedPacket = original_handleUnexpectedPacket
# Check that packet was notified as unexpected.
self.assertEquals(len(call_list), 1)
# Return call_list in case caller wants to do extra checks on it.
return call_list
# Master node handler # Master node handler
def test_initialAnswerPrimaryMaster(self): def test_initialAnswerPrimaryMaster(self):
client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher()) client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher())
conn = Mock({'getUUID': None}) conn = Mock({'getUUID': None})
call_list = self._testHandleUnexpectedPacketCalledWithMedhod( self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleAnswerPrimaryMaster, client_handler, client_handler.handleAnswerPrimaryMaster,
args=(conn, None, 0, [])) args=(conn, None, 0, []))
# Nothing else happened, or a raise would have happened (app is None,
# and it's where things would happen)
self.assertEquals(call_list[0], (conn, None))
def test_nonMasterAnswerPrimaryMaster(self): def test_nonMasterAnswerPrimaryMaster(self):
for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE): for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
...@@ -553,10 +546,9 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -553,10 +546,9 @@ class ClientEventHandlerTest(unittest.TestCase):
def test_initialSendPartitionTable(self): def test_initialSendPartitionTable(self):
client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher()) client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher())
conn = Mock({'getUUID': None}) conn = Mock({'getUUID': None})
call_list = self._testHandleUnexpectedPacketCalledWithMedhod( self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleSendPartitionTable, client_handler, client_handler.handleSendPartitionTable,
args=(conn, None, None, None)) args=(conn, None, None, None))
self.assertEquals(call_list[0], (conn, None))
def test_nonMasterSendPartitionTable(self): def test_nonMasterSendPartitionTable(self):
for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE): for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
...@@ -644,10 +636,9 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -644,10 +636,9 @@ class ClientEventHandlerTest(unittest.TestCase):
def test_initialNotifyNodeInformation(self): def test_initialNotifyNodeInformation(self):
client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher()) client_handler = PrimaryBoostrapEventHandler(None, self.getDispatcher())
conn = Mock({'getUUID': None}) conn = Mock({'getUUID': None})
call_list = self._testHandleUnexpectedPacketCalledWithMedhod( self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleNotifyNodeInformation, client_handler, client_handler.handleNotifyNodeInformation,
args=(conn, None, None)) args=(conn, None, None))
self.assertEquals(call_list[0], (conn, None))
def test_nonMasterNotifyNodeInformation(self): def test_nonMasterNotifyNodeInformation(self):
for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE): for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
...@@ -760,10 +751,9 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -760,10 +751,9 @@ class ClientEventHandlerTest(unittest.TestCase):
app = App() app = App()
client_handler = PrimaryBoostrapEventHandler(app, self.getDispatcher()) client_handler = PrimaryBoostrapEventHandler(app, self.getDispatcher())
conn = Mock({'getUUID': None}) conn = Mock({'getUUID': None})
call_list = self._testHandleUnexpectedPacketCalledWithMedhod( self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleNotifyPartitionChanges, client_handler, client_handler.handleNotifyPartitionChanges,
args=(conn, None, None, None)) args=(conn, None, None, None))
self.assertEquals(call_list[0], (conn, None))
def test_nonMasterNotifyPartitionChanges(self): def test_nonMasterNotifyPartitionChanges(self):
for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE): for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
......
This diff is collapsed.
...@@ -320,6 +320,7 @@ UUID_NAMESPACES = { ...@@ -320,6 +320,7 @@ UUID_NAMESPACES = {
class ProtocolError(Exception): pass class ProtocolError(Exception): pass
class PacketMalformedError(ProtocolError): pass class PacketMalformedError(ProtocolError): pass
class UnexpectedPacketError(ProtocolError): pass
decode_table = {} decode_table = {}
......
...@@ -67,7 +67,7 @@ class testProtocol(unittest.TestCase): ...@@ -67,7 +67,7 @@ class testProtocol(unittest.TestCase):
def test_02_error(self): def test_02_error(self):
p = protocol._error(10, "error message") p = protocol._error(10, "error message")
code, msg = p._decodeError() code, msg = protocol._decodeError(p._body)
self.assertEqual(code, 10) self.assertEqual(code, 10)
self.assertEqual(msg, "error message") self.assertEqual(msg, "error message")
......
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