Commit 76b17baf authored by Grégory Wisniewski's avatar Grégory Wisniewski

Use getFakeConnection

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2065 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 64a63d76
......@@ -149,14 +149,23 @@ class NeoTestBase(unittest.TestCase):
uuids = self.getNewUUID(), self.getNewUUID()
return min(uuids), max(uuids)
def getFakeConnector(self, descriptor=None):
return Mock({
'__repr__': 'FakeConnector',
'getDescriptor': descriptor,
})
def getFakeConnection(self, uuid=None, address=('127.0.0.1', 10000),
is_server=False):
is_server=False, connector=None):
if connector is None:
connector = self.getFakeConnector()
return Mock({
'getUUID': uuid,
'getAddress': address,
'isServer': is_server,
'__repr__': 'FakeConnection',
'isServer': is_server,
'__repr__': 'FakeConnection',
'__nonzero__': 0,
'getConnector': connector,
})
def checkProtocolErrorRaised(self, method, *args, **kwargs):
......
......@@ -33,7 +33,7 @@ class MasterHandlerTests(NeoTestBase):
pass
def getConnection(self):
return Mock({'getAddress': ('fake-conn', 0)})
return self.getFakeConnection()
class MasterBootstrapHandlerTests(MasterHandlerTests):
......@@ -186,16 +186,19 @@ class MasterNotificationsHandlerTests(MasterHandlerTests):
(NodeTypes.STORAGE, addr, self.getNewUUID(), NodeStates.DOWN),
]
# XXX: it might be better to test with real node & node manager
conn1, conn2 = Mock({'__repr__': 'conn1'}), Mock({'__repr__': 'conn2'})
conn1 = self.getFakeConnection()
conn2 = self.getFakeConnection()
node1 = Mock({
'getConnection': conn1,
'__nonzero__': 1,
'isConnected': True,
'__repr__': 'Fake Node',
})
node2 = Mock({
'getConnection': conn2,
'__nonzero__': 1,
'isConnected': True,
'__repr__': 'Fake Node',
})
self.app.nm = Mock({'getByUUID': ReturnValues(node1, node2)})
self.app.cp = Mock()
......
......@@ -32,7 +32,7 @@ class StorageBootstrapHandlerTests(NeoTestBase):
self.handler = StorageBootstrapHandler(self.app)
def getConnection(self):
return Mock({'getAddress': ('fake-conn', 0)})
return self.getFakeConnection()
def test_notReady(self):
conn = self.getConnection()
......@@ -70,7 +70,7 @@ class StorageAnswerHandlerTests(NeoTestBase):
self.handler = StorageAnswersHandler(self.app)
def getConnection(self):
return Mock({'getAddress': ('fake-conn', 0)})
return self.getFakeConnection()
def test_answerObject(self):
conn = self.getConnection()
......@@ -229,7 +229,7 @@ class StorageAnswerHandlerTests(NeoTestBase):
tid1 = self.getNextTID()
tid2 = self.getNextTID(tid1)
tid_list = [tid1, tid2]
conn = Mock({'getUUID': uuid})
conn = self.getFakeConnection(uuid=uuid)
self.app.local_var.node_tids = {}
self.handler.answerTIDs(conn, tid_list)
self.assertTrue(uuid in self.app.local_var.node_tids)
......
......@@ -222,11 +222,10 @@ class MasterServerElectionTests(NeoTestBase):
if uuid is True:
uuid = self.getNewUUID()
node.setUUID(uuid)
conn = Mock({
"getUUID": node.getUUID(),
"getAddress": node.getAddress(),
"getConnector": Mock(),
})
conn = self.getFakeConnection(
uuid=node.getUUID(),
address=node.getAddress(),
)
return (node, conn)
......@@ -286,22 +285,14 @@ class MasterServerElectionTests(NeoTestBase):
def __getClient(self):
uuid = self.getNewUUID()
conn = Mock({
'getUUID': uuid,
'getAddress': self.client_address,
'getConnector': Mock(),
})
conn = self.getFakeConnection(uuid=uuid, address=self.client_address)
self.app.nm.createClient(uuid=uuid, address=self.client_address)
return conn
def __getMaster(self, port=1000, register=True):
uuid = self.getNewUUID()
address = ('127.0.0.1', port)
conn = Mock({
'getUUID': uuid,
'getAddress': address,
'getConnector': Mock(),
})
conn = self.getFakeConnection(uuid=uuid, address=address)
if register:
self.app.nm.createMaster(uuid=uuid, address=address)
return conn
......
......@@ -56,9 +56,9 @@ class MasterAppTests(NeoTestBase):
client_uuid = self.getNewUUID()
client = self.app.nm.createClient(uuid=client_uuid)
# create conn and patch em
master_conn = Mock()
storage_conn = Mock()
client_conn = Mock()
master_conn = self.getFakeConnection()
storage_conn = self.getFakeConnection()
client_conn = self.getFakeConnection()
master.setConnection(master_conn)
storage.setConnection(storage_conn)
client.setConnection(client_conn)
......
......@@ -29,10 +29,8 @@ from neo.protocol import Packets
class StorageClientHandlerTests(NeoTestBase):
def checkHandleUnexpectedPacket(self, _call, _msg_type, _listening=True, **kwargs):
conn = Mock({
"getAddress" : ("127.0.0.1", self.master_port),
"isServer": _listening,
})
conn = self.getFakeConnection(address=("127.0.0.1", self.master_port),
is_server=_listening)
# hook
self.operation.peerBroken = lambda c: c.peerBrokendCalled()
self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs)
......@@ -60,7 +58,7 @@ class StorageClientHandlerTests(NeoTestBase):
NeoTestBase.tearDown(self)
def _getConnection(self, uuid=None):
return Mock({'getUUID': uuid, 'getAddress': ('127.0.0.1', 1000)})
return self.getFakeConnection(uuid=uuid, address=('127.0.0.1', 1000))
def _checkTransactionsAborted(self, uuid):
calls = self.app.tm.mockGetNamedCalls('abortFor')
......@@ -87,13 +85,13 @@ class StorageClientHandlerTests(NeoTestBase):
def test_18_askTransactionInformation1(self):
# transaction does not exists
conn = Mock({ })
conn = self._getConnection()
self.operation.askTransactionInformation(conn, INVALID_TID)
self.checkErrorPacket(conn)
def test_18_askTransactionInformation2(self):
# answer
conn = Mock({ })
conn = self._getConnection()
dm = Mock({ "getTransaction": (INVALID_TID, 'user', 'desc', '', False), })
self.app.dm = dm
self.operation.askTransactionInformation(conn, INVALID_TID)
......@@ -101,7 +99,7 @@ class StorageClientHandlerTests(NeoTestBase):
def test_24_askObject1(self):
# delayed response
conn = Mock({})
conn = self._getConnection()
self.app.dm = Mock()
self.app.tm = Mock({'loadLocked': True})
self.app.load_lock_dict[INVALID_OID] = object()
......@@ -115,7 +113,7 @@ class StorageClientHandlerTests(NeoTestBase):
def test_24_askObject2(self):
# invalid serial / tid / packet not found
self.app.dm = Mock({'getObject': None})
conn = Mock({})
conn = self._getConnection()
self.assertEquals(len(self.app.event_queue), 0)
self.operation.askObject(conn, oid=INVALID_OID,
serial=INVALID_SERIAL, tid=INVALID_TID)
......@@ -128,7 +126,7 @@ class StorageClientHandlerTests(NeoTestBase):
def test_24_askObject3(self):
# object found => answer
self.app.dm = Mock({'getObject': ('', '', 0, 0, '', None)})
conn = Mock({})
conn = self._getConnection()
self.assertEquals(len(self.app.event_queue), 0)
self.operation.askObject(conn, oid=INVALID_OID,
serial=INVALID_SERIAL, tid=INVALID_TID)
......@@ -140,14 +138,14 @@ class StorageClientHandlerTests(NeoTestBase):
app = self.app
app.pt = Mock()
app.dm = Mock()
conn = Mock({})
conn = self._getConnection()
self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
self.assertEquals(len(app.pt.mockGetNamedCalls('getCellList')), 0)
self.assertEquals(len(app.dm.mockGetNamedCalls('getTIDList')), 0)
def test_25_askTIDs2(self):
# well case => answer
conn = Mock({})
conn = self._getConnection()
self.app.pt = Mock({'getPartitions': 1})
self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
self.operation.askTIDs(conn, 1, 2, 1)
......@@ -158,7 +156,7 @@ class StorageClientHandlerTests(NeoTestBase):
def test_25_askTIDs3(self):
# invalid partition => answer usable partitions
conn = Mock({})
conn = self._getConnection()
cell = Mock({'getUUID':self.app.uuid})
self.app.dm = Mock({'getTIDList': (INVALID_TID, )})
self.app.pt = Mock({
......@@ -177,19 +175,19 @@ class StorageClientHandlerTests(NeoTestBase):
# invalid offsets => error
app = self.app
app.dm = Mock()
conn = Mock({})
conn = self._getConnection()
self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn,
1, 1, None)
self.assertEquals(len(app.dm.mockGetNamedCalls('getObjectHistory')), 0)
def test_26_askObjectHistory2(self):
# first case: empty history
conn = Mock({})
conn = self._getConnection()
self.app.dm = Mock({'getObjectHistory': None})
self.operation.askObjectHistory(conn, INVALID_OID, 1, 2)
self.checkAnswerObjectHistory(conn)
# second case: not empty history
conn = Mock({})
conn = self._getConnection()
self.app.dm = Mock({'getObjectHistory': [('', 0, ), ]})
self.operation.askObjectHistory(conn, INVALID_OID, 1, 2)
self.checkAnswerObjectHistory(conn)
......
......@@ -122,7 +122,7 @@ class StorageMasterHandlerTests(NeoTestBase):
def test_16_stopOperation1(self):
# OperationFailure
conn = Mock({ 'isServer': False })
conn = self.getFakeConnection(is_server=False)
self.assertRaises(OperationFailure, self.operation.stopOperation, conn)
def _getConnection(self):
......@@ -169,7 +169,7 @@ class StorageMasterHandlerTests(NeoTestBase):
def test_30_answerLastIDs(self):
# set critical TID on replicator
conn = Mock()
conn = self.getFakeConnection()
self.app.replicator = Mock()
self.operation.answerLastIDs(
conn=conn,
......@@ -183,7 +183,7 @@ class StorageMasterHandlerTests(NeoTestBase):
def test_31_answerUnfinishedTransactions(self):
# set unfinished TID on replicator
conn = Mock()
conn = self.getFakeConnection()
self.app.replicator = Mock()
self.operation.answerUnfinishedTransactions(
conn=conn,
......
......@@ -27,10 +27,8 @@ from neo.protocol import INVALID_TID, INVALID_OID, INVALID_SERIAL
class StorageStorageHandlerTests(NeoTestBase):
def checkHandleUnexpectedPacket(self, _call, _msg_type, _listening=True, **kwargs):
conn = Mock({
"getAddress" : ("127.0.0.1", self.master_port),
"isServer": _listening,
})
conn = self.getFakeConnection(address=("127.0.0.1", self.master_port),
is_server=_listening)
# hook
self.operation.peerBroken = lambda c: c.peerBrokendCalled()
self.checkUnexpectedPacketRaised(_call, conn=conn, **kwargs)
......@@ -58,13 +56,13 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_18_askTransactionInformation1(self):
# transaction does not exists
conn = Mock({ })
conn = self.getFakeConnection()
self.operation.askTransactionInformation(conn, INVALID_TID)
self.checkErrorPacket(conn)
def test_18_askTransactionInformation2(self):
# answer
conn = Mock({ })
conn = self.getFakeConnection()
dm = Mock({"getTransaction": (INVALID_TID, 'user', 'desc', '', False), })
self.app.dm = dm
self.operation.askTransactionInformation(conn, INVALID_TID)
......@@ -72,7 +70,7 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_24_askObject1(self):
# delayed response
conn = Mock({})
conn = self.getFakeConnection()
self.app.dm = Mock()
self.app.tm = Mock({'loadLocked': True})
self.app.load_lock_dict[INVALID_OID] = object()
......@@ -86,7 +84,7 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_24_askObject2(self):
# invalid serial / tid / packet not found
self.app.dm = Mock({'getObject': None})
conn = Mock({})
conn = self.getFakeConnection()
self.assertEquals(len(self.app.event_queue), 0)
self.operation.askObject(conn, oid=INVALID_OID,
serial=INVALID_SERIAL, tid=INVALID_TID)
......@@ -100,7 +98,7 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_24_askObject3(self):
# object found => answer
self.app.dm = Mock({'getObject': ('', '', 0, 0, '', None)})
conn = Mock({})
conn = self.getFakeConnection()
self.assertEquals(len(self.app.event_queue), 0)
self.operation.askObject(conn, oid=INVALID_OID,
serial=INVALID_SERIAL, tid=INVALID_TID)
......@@ -112,14 +110,14 @@ class StorageStorageHandlerTests(NeoTestBase):
app = self.app
app.pt = Mock()
app.dm = Mock()
conn = Mock({})
conn = self.getFakeConnection()
self.checkProtocolErrorRaised(self.operation.askTIDs, conn, 1, 1, None)
self.assertEquals(len(app.pt.mockGetNamedCalls('getCellList')), 0)
self.assertEquals(len(app.dm.mockGetNamedCalls('getReplicationTIDList')), 0)
def test_25_askTIDs2(self):
# well case => answer
conn = Mock({})
conn = self.getFakeConnection()
self.app.dm = Mock({'getReplicationTIDList': (INVALID_TID, )})
self.app.pt = Mock({'getPartitions': 1})
self.operation.askTIDs(conn, 1, 2, 1)
......@@ -130,7 +128,7 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_25_askTIDs3(self):
# invalid partition => answer usable partitions
conn = Mock({})
conn = self.getFakeConnection()
cell = Mock({'getUUID':self.app.uuid})
self.app.dm = Mock({'getReplicationTIDList': (INVALID_TID, )})
self.app.pt = Mock({
......@@ -149,19 +147,19 @@ class StorageStorageHandlerTests(NeoTestBase):
# invalid offsets => error
app = self.app
app.dm = Mock()
conn = Mock({})
conn = self.getFakeConnection()
self.checkProtocolErrorRaised(self.operation.askObjectHistory, conn,
1, 1, None)
self.assertEquals(len(app.dm.mockGetNamedCalls('getObjectHistory')), 0)
def test_26_askObjectHistory2(self):
# first case: empty history
conn = Mock({})
conn = self.getFakeConnection()
self.app.dm = Mock({'getObjectHistory': None})
self.operation.askObjectHistory(conn, INVALID_OID, 1, 2)
self.checkAnswerObjectHistory(conn)
# second case: not empty history
conn = Mock({})
conn = self.getFakeConnection()
self.app.dm = Mock({'getObjectHistory': [('', 0, ), ]})
self.operation.askObjectHistory(conn, INVALID_OID, 1, 2)
self.checkAnswerObjectHistory(conn)
......@@ -171,14 +169,14 @@ class StorageStorageHandlerTests(NeoTestBase):
app = self.app
app.pt = Mock()
app.dm = Mock()
conn = Mock({})
conn = self.getFakeConnection()
self.checkProtocolErrorRaised(self.operation.askOIDs, conn, 1, 1, None)
self.assertEquals(len(app.pt.mockGetNamedCalls('getCellList')), 0)
self.assertEquals(len(app.dm.mockGetNamedCalls('getOIDList')), 0)
def test_25_askOIDs2(self):
# well case > answer OIDs
conn = Mock({})
conn = self.getFakeConnection()
self.app.pt = Mock({'getPartitions': 1})
self.app.dm = Mock({'getOIDList': (INVALID_OID, )})
self.operation.askOIDs(conn, 1, 2, 1)
......@@ -189,7 +187,7 @@ class StorageStorageHandlerTests(NeoTestBase):
def test_25_askOIDs3(self):
# invalid partition => answer usable partitions
conn = Mock({})
conn = self.getFakeConnection()
cell = Mock({'getUUID':self.app.uuid})
self.app.dm = Mock({'getOIDList': (INVALID_OID, )})
self.app.pt = Mock({
......
......@@ -45,15 +45,13 @@ class BootstrapManagerTests(NeoTestBase):
# Tests
def testConnectionCompleted(self):
uuid = self.getNewUUID()
conn = Mock({"getUUID" : uuid,
"getAddress" : ("127.0.0.1", self.master_port)})
conn = self.getFakeConnection(address=("127.0.0.1", self.master_port))
self.bootstrap.connectionCompleted(conn)
self.checkAskPrimary(conn)
def testHandleNotReady(self):
# the primary is not ready
conn = Mock({})
conn = self.getFakeConnection()
self.bootstrap.notReady(conn, '')
self.checkClosed(conn)
self.checkNoPacketSent(conn)
......
......@@ -39,8 +39,8 @@ class EventTests(NeoTestBase):
self.assertTrue(isinstance(em.epoll, Epoll))
# use a mock object instead of epoll
em.epoll = Mock()
connector = Mock({"getDescriptor" : 1014})
conn = Mock({'getConnector': connector})
connector = self.getFakeConnector(descriptor=1014)
conn = self.getFakeConnection(connector=connector)
self.assertEqual(len(em.getConnectionList()), 0)
# test register/unregister
......@@ -52,8 +52,8 @@ class EventTests(NeoTestBase):
self.assertEqual(data, 1014)
self.assertEqual(len(em.getConnectionList()), 1)
self.assertEqual(em.getConnectionList()[0].getDescriptor(), conn.getDescriptor())
connector = Mock({"getDescriptor" : 1014})
conn = Mock({'getConnector': connector})
connector = self.getFakeConnector(descriptor=1014)
conn = self.getFakeConnection(connector=connector)
em.unregister(conn)
self.assertEquals(len(connector.mockGetNamedCalls("getDescriptor")), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("unregister")), 1)
......@@ -63,8 +63,7 @@ class EventTests(NeoTestBase):
self.assertEqual(len(em.getConnectionList()), 0)
# add/removeReader
connector = Mock({"getDescriptor" : 1515})
conn = Mock({'getConnector': connector})
conn = self.getFakeConnection()
self.assertEqual(len(em.reader_set), 0)
em.addReader(conn)
self.assertEqual(len(em.reader_set), 1)
......@@ -80,8 +79,7 @@ class EventTests(NeoTestBase):
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 2)
# add/removeWriter
connector = Mock({"getDescriptor" : 1515})
conn = Mock({'getConnector': connector})
conn = self.getFakeConnection()
self.assertEqual(len(em.writer_set), 0)
em.addWriter(conn)
self.assertEqual(len(em.writer_set), 1)
......@@ -97,11 +95,11 @@ class EventTests(NeoTestBase):
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 4)
# poll
r_connector = Mock({"getDescriptor" : 14515})
r_conn = Mock({'getConnector': r_connector})
r_connector = self.getFakeConnector(descriptor=14515)
r_conn = self.getFakeConnection(connector=r_connector)
em.register(r_conn)
w_connector = Mock({"getDescriptor" : 351621})
w_conn = Mock({'getConnector': w_connector})
w_connector = self.getFakeConnector(descriptor=351621)
w_conn = self.getFakeConnection(connector=w_connector)
em.register(w_conn)
em.epoll = Mock({"poll":(
(r_connector.getDescriptor(),),
......
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