Commit fd11d103 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Use more getFakeConnection() to build a clean mock object.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2055 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 7b66b2c2
...@@ -149,10 +149,14 @@ class NeoTestBase(unittest.TestCase): ...@@ -149,10 +149,14 @@ class NeoTestBase(unittest.TestCase):
uuids = self.getNewUUID(), self.getNewUUID() uuids = self.getNewUUID(), self.getNewUUID()
return min(uuids), max(uuids) return min(uuids), max(uuids)
def getFakeConnection(self, uuid=None, address=('127.0.0.1', 10000)): def getFakeConnection(self, uuid=None, address=('127.0.0.1', 10000),
is_server=False):
return Mock({ return Mock({
'getUUID': uuid, 'getUUID': uuid,
'getAddress': address, 'getAddress': address,
'isServer': is_server,
'__repr__': 'FakeConnection',
'__nonzero__': 0,
}) })
def checkProtocolErrorRaised(self, method, *args, **kwargs): def checkProtocolErrorRaised(self, method, *args, **kwargs):
......
...@@ -62,11 +62,8 @@ class MasterClientElectionTests(NeoTestBase): ...@@ -62,11 +62,8 @@ class MasterClientElectionTests(NeoTestBase):
def identifyToMasterNode(self): def identifyToMasterNode(self):
node = self.app.nm.getMasterList()[0] node = self.app.nm.getMasterList()[0]
node.setUUID(self.getNewUUID()) node.setUUID(self.getNewUUID())
conn = Mock({ conn = self.getFakeConnection(uuid=node.getUUID(),
"getUUID": node.getUUID(), address=node.getAddress())
"getAddress": node.getAddress(),
"getConnector": Mock(),
})
return (node, conn) return (node, conn)
def _checkUnconnected(self, node): def _checkUnconnected(self, node):
......
...@@ -49,43 +49,31 @@ class StorageInitializationHandlerTests(NeoTestBase): ...@@ -49,43 +49,31 @@ class StorageInitializationHandlerTests(NeoTestBase):
def getLastUUID(self): def getLastUUID(self):
return self.uuid return self.uuid
def getClientConnection(self):
address = ("127.0.0.1", self.client_port)
return self.getFakeConnection(uuid=self.getNewUUID(), address=address)
def test_02_timeoutExpired(self): def test_02_timeoutExpired(self):
# client connection conn = self.getClientConnection()
uuid = self.getNewUUID()
conn = Mock({"getUUID" : uuid,
"getAddress" : ("127.0.0.1", self.client_port),
"isServer" : False})
self.assertRaises(PrimaryFailure, self.verification.timeoutExpired, conn,) self.assertRaises(PrimaryFailure, self.verification.timeoutExpired, conn,)
# nothing happens # nothing happens
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_03_connectionClosed(self): def test_03_connectionClosed(self):
# client connection conn = self.getClientConnection()
uuid = self.getNewUUID()
conn = Mock({"getUUID" : uuid,
"getAddress" : ("127.0.0.1", self.client_port),
"isServer" : False})
self.assertRaises(PrimaryFailure, self.verification.connectionClosed, conn,) self.assertRaises(PrimaryFailure, self.verification.connectionClosed, conn,)
# nothing happens # nothing happens
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_04_peerBroken(self): def test_04_peerBroken(self):
# client connection conn = self.getClientConnection()
uuid = self.getNewUUID()
conn = Mock({"getUUID" : uuid,
"getAddress" : ("127.0.0.1", self.client_port),
"isServer" : False})
self.assertRaises(PrimaryFailure, self.verification.peerBroken, conn,) self.assertRaises(PrimaryFailure, self.verification.peerBroken, conn,)
# nothing happens # nothing happens
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_09_sendPartitionTable(self): def test_09_sendPartitionTable(self):
uuid = self.getNewUUID()
# send a table # send a table
conn = Mock({"getUUID" : uuid, conn = self.getClientConnection()
"getAddress" : ("127.0.0.1", self.client_port),
"isServer" : False})
self.app.pt = PartitionTable(3, 2) self.app.pt = PartitionTable(3, 2)
node_1 = self.getNewUUID() node_1 = self.getNewUUID()
node_2 = self.getNewUUID() node_2 = self.getNewUUID()
......
...@@ -29,10 +29,7 @@ from neo.protocol import INVALID_TID, INVALID_OID ...@@ -29,10 +29,7 @@ from neo.protocol import INVALID_TID, INVALID_OID
class StorageMasterHandlerTests(NeoTestBase): class StorageMasterHandlerTests(NeoTestBase):
def checkHandleUnexpectedPacket(self, _call, _msg_type, _listening=True, **kwargs): def checkHandleUnexpectedPacket(self, _call, _msg_type, _listening=True, **kwargs):
conn = Mock({ conn = self.getMasterConnection(is_server=_listening)
"getAddress" : ("127.0.0.1", self.master_port),
"isServer": _listening,
})
# hook # hook
self.operation.peerBroken = lambda c: c.peerBrokendCalled() self.operation.peerBroken = lambda c: c.peerBrokendCalled()
self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs) self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs)
...@@ -58,41 +55,32 @@ class StorageMasterHandlerTests(NeoTestBase): ...@@ -58,41 +55,32 @@ class StorageMasterHandlerTests(NeoTestBase):
def tearDown(self): def tearDown(self):
NeoTestBase.tearDown(self) NeoTestBase.tearDown(self)
def getMasterConnection(self):
address = ("127.0.0.1", self.master_port)
return self.getFakeConnection(uuid=self.master_uuid, address=address)
def test_06_timeoutExpired(self): def test_06_timeoutExpired(self):
# client connection # client connection
conn = Mock({ conn = self.getMasterConnection()
"getUUID": self.master_uuid,
"getAddress" : ("127.0.0.1", self.master_port),
})
self.assertRaises(PrimaryFailure, self.operation.timeoutExpired, conn) self.assertRaises(PrimaryFailure, self.operation.timeoutExpired, conn)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_07_connectionClosed2(self): def test_07_connectionClosed2(self):
# primary has closed the connection # primary has closed the connection
conn = Mock({ conn = self.getMasterConnection()
"getUUID": self.master_uuid,
"getAddress" : ("127.0.0.1", self.master_port),
})
self.assertRaises(PrimaryFailure, self.operation.connectionClosed, conn) self.assertRaises(PrimaryFailure, self.operation.connectionClosed, conn)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_08_peerBroken(self): def test_08_peerBroken(self):
# client connection # client connection
conn = Mock({ conn = self.getMasterConnection()
"getUUID": self.master_uuid,
"getAddress" : ("127.0.0.1", self.master_port),
})
self.assertRaises(PrimaryFailure, self.operation.peerBroken, conn) self.assertRaises(PrimaryFailure, self.operation.peerBroken, conn)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
def test_14_notifyPartitionChanges1(self): def test_14_notifyPartitionChanges1(self):
# old partition change -> do nothing # old partition change -> do nothing
app = self.app app = self.app
conn = Mock({ conn = self.getMasterConnection()
"isServer": False,
"getAddress" : ("127.0.0.1", self.master_port),
})
app.replicator = Mock({}) app.replicator = Mock({})
self.app.pt = Mock({'getID': 1}) self.app.pt = Mock({'getID': 1})
count = len(self.app.nm.getList()) count = len(self.app.nm.getList())
...@@ -113,10 +101,7 @@ class StorageMasterHandlerTests(NeoTestBase): ...@@ -113,10 +101,7 @@ class StorageMasterHandlerTests(NeoTestBase):
(2, uuid3, CellStates.OUT_OF_DATE), (2, uuid3, CellStates.OUT_OF_DATE),
) )
# context # context
conn = Mock({ conn = self.getMasterConnection()
"isServer": False,
"getAddress" : ("127.0.0.1", self.master_port),
})
app = self.app app = self.app
# register nodes # register nodes
app.nm.createStorage(uuid=uuid1) app.nm.createStorage(uuid=uuid1)
......
...@@ -45,7 +45,7 @@ class HandlerTests(NeoTestBase): ...@@ -45,7 +45,7 @@ class HandlerTests(NeoTestBase):
self.assertEquals(len(calls), 1) self.assertEquals(len(calls), 1)
def test_dispatch(self): def test_dispatch(self):
conn = Mock({'getAddress': ('127.0.0.1', 10000)}) conn = self.getFakeConnection()
packet = self.getFakePacket() packet = self.getFakePacket()
# all is ok # all is ok
self.setFakeMethod(lambda c: None) self.setFakeMethod(lambda c: None)
......
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