Commit 912e0a4d authored by Grégory Wisniewski's avatar Grégory Wisniewski

Fix bad indentation in tests modules.


git-svn-id: https://svn.erp5.org/repos/neo/trunk@1345 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent e3926ad4
...@@ -309,50 +309,45 @@ class NeoTestBase(unittest.TestCase): ...@@ -309,50 +309,45 @@ class NeoTestBase(unittest.TestCase):
connector_cpt = 0 connector_cpt = 0
class DoNothingConnector(Mock): class DoNothingConnector(Mock):
def __init__(self, s=None): def __init__(self, s=None):
logging.info("initializing connector") logging.info("initializing connector")
self.desc = globals()['connector_cpt'] self.desc = globals()['connector_cpt']
globals()['connector_cpt'] = globals()['connector_cpt']+ 1 globals()['connector_cpt'] = globals()['connector_cpt']+ 1
self.packet_cpt = 0 self.packet_cpt = 0
Mock.__init__(self) Mock.__init__(self)
def getAddress(self): def getAddress(self):
return self.addr return self.addr
def makeClientConnection(self, addr):
self.addr = addr
def getDescriptor(self): def makeClientConnection(self, addr):
return self.desc self.addr = addr
def getDescriptor(self):
return self.desc
class TestElectionConnector(DoNothingConnector): class TestElectionConnector(DoNothingConnector):
def receive(self):
""" simulate behavior of election """ def receive(self):
if self.packet_cpt == 0: """ simulate behavior of election """
# first : identify if self.packet_cpt == 0:
logging.info("in patched analyse / IDENTIFICATION") # first : identify
p = protocol.Packet() logging.info("in patched analyse / IDENTIFICATION")
self.uuid = getNewUUID() p = protocol.Packet()
p.acceptNodeIdentification(1, self.uuid = getNewUUID()
NodeType.MASTER, p.acceptNodeIdentification(1, NodeType.MASTER, self.uuid,
self.uuid, self.getAddress()[0], self.getAddress()[1], 1009, 2)
self.getAddress()[0], self.packet_cpt += 1
self.getAddress()[1], return p.encode()
1009, elif self.packet_cpt == 1:
2 # second : answer primary master nodes
) logging.info("in patched analyse / ANSWER PM")
self.packet_cpt += 1 p = protocol.Packet()
return p.encode() p.answerPrimaryMaster(2, protocol.INVALID_UUID, [])
elif self.packet_cpt == 1: self.packet_cpt += 1
# second : answer primary master nodes return p.encode()
logging.info("in patched analyse / ANSWER PM") else:
p = protocol.Packet() # then do nothing
p.answerPrimaryMaster(2, protocol.INVALID_UUID, []) from neo.connector import ConnectorTryAgainException
self.packet_cpt += 1 raise ConnectorTryAgainException
return p.encode()
else:
# then do nothing
from neo.connector import ConnectorTryAgainException
raise ConnectorTryAgainException
...@@ -47,7 +47,7 @@ class ClientHandlerTests(NeoTestBase): ...@@ -47,7 +47,7 @@ class ClientHandlerTests(NeoTestBase):
'unlock': None}) 'unlock': None})
def getDispatcher(self, queue=None): def getDispatcher(self, queue=None):
return Mock({'getQueue': queue, 'connectToPrimaryMasterNode': None}) return Mock({'getQueue': queue, 'connectToPrimaryMasterNode': None})
def buildHandler(self, handler_class, app, dispatcher): def buildHandler(self, handler_class, app, dispatcher):
# some handlers do not accept the second argument # some handlers do not accept the second argument
......
...@@ -296,13 +296,12 @@ class NEOCluster(object): ...@@ -296,13 +296,12 @@ class NEOCluster(object):
for master in self.getMasterProcessList(): for master in self.getMasterProcessList():
master_uuid = master.getUUID() master_uuid = master.getUUID()
is_primary = master_uuid == primary_uuid is_primary = master_uuid == primary_uuid
if primary and is_primary or \ if primary and is_primary or not (primary or is_primary):
not (primary or is_primary): killed_uuid_list.append(master_uuid)
killed_uuid_list.append(master_uuid) master.kill()
master.kill() master.wait()
master.wait() if not all:
if not all: break
break
return killed_uuid_list return killed_uuid_list
def killPrimaryMaster(self): def killPrimaryMaster(self):
...@@ -367,7 +366,7 @@ class NEOCluster(object): ...@@ -367,7 +366,7 @@ class NEOCluster(object):
opaque_history.append(opaque) opaque_history.append(opaque)
time.sleep(delay) time.sleep(delay)
else: else:
raise AssertionError, 'Timeout while expecting condition. ' \ raise AssertionError, 'Timeout while expecting condition. ' \
'History: %s' % (opaque_history, ) 'History: %s' % (opaque_history, )
def expectAllMasters(self, node_count, state=None, timeout=0, delay=1): def expectAllMasters(self, node_count, state=None, timeout=0, delay=1):
......
...@@ -33,99 +33,99 @@ class MasterAppTests(NeoTestBase): ...@@ -33,99 +33,99 @@ class MasterAppTests(NeoTestBase):
NeoTestBase.tearDown(self) NeoTestBase.tearDown(self)
def test_02_getNextOID(self): def test_02_getNextOID(self):
# must raise as we don"t have one # must raise as we don"t have one
self.assertEqual(self.app.loid, None) self.assertEqual(self.app.loid, None)
self.app.loid = None self.app.loid = None
self.assertRaises(RuntimeError, self.app.getNextOID) self.assertRaises(RuntimeError, self.app.getNextOID)
# set one # set one
self.app.loid = p64(23) self.app.loid = p64(23)
noid = self.app.getNextOID() noid = self.app.getNextOID()
self.assertEqual(self.app.loid, noid) self.assertEqual(self.app.loid, noid)
self.failUnless(u64(self.app.loid) > 23) self.failUnless(u64(self.app.loid) > 23)
self.assertEqual(u64(self.app.loid), 24) self.assertEqual(u64(self.app.loid), 24)
def test_03_getNextTID(self): def test_03_getNextTID(self):
self.assertEqual(self.app.ltid, None) self.assertEqual(self.app.ltid, None)
ntid = self.app.getNextTID() ntid = self.app.getNextTID()
self.assertEqual(self.app.ltid, ntid) self.assertEqual(self.app.ltid, ntid)
# generate new one # generate new one
tid = self.app.getNextTID() tid = self.app.getNextTID()
self.assertEqual(self.app.ltid, tid) self.assertEqual(self.app.ltid, tid)
self.failUnless(tid > ntid) self.failUnless(tid > ntid)
def test_04_getPartition(self): def test_04_getPartition(self):
self.app.pt.num_partitions = 3 self.app.pt.num_partitions = 3
p = self.app.getPartition(p64(1)) p = self.app.getPartition(p64(1))
self.assertEqual(p, 1) self.assertEqual(p, 1)
p = self.app.getPartition(p64(2)) p = self.app.getPartition(p64(2))
self.assertEqual(p, 2) self.assertEqual(p, 2)
p = self.app.getPartition(p64(1009)) # 1009 defined in config p = self.app.getPartition(p64(1009)) # 1009 defined in config
self.assertEqual(p, 0) self.assertEqual(p, 0)
def test_05_getNewOIDList(self): def test_05_getNewOIDList(self):
self.app.loid = p64(1) self.app.loid = p64(1)
oid_list = self.app.getNewOIDList(15) oid_list = self.app.getNewOIDList(15)
self.assertEqual(len(oid_list), 15) self.assertEqual(len(oid_list), 15)
i = 2 i = 2
# begin from 0, so generated oid from 1 to 15 # begin from 0, so generated oid from 1 to 15
for oid in oid_list: for oid in oid_list:
self.assertEqual(u64(oid), i) self.assertEqual(u64(oid), i)
i+=1 i+=1
def test_06_broadcastNodeInformation(self): def test_06_broadcastNodeInformation(self):
# defined some nodes to which data will be send # defined some nodes to which data will be send
master_uuid = self.getNewUUID() master_uuid = self.getNewUUID()
self.app.nm.createMaster(uuid=master_uuid) self.app.nm.createMaster(uuid=master_uuid)
storage_uuid = self.getNewUUID() storage_uuid = self.getNewUUID()
storage = self.app.nm.createStorage(uuid=storage_uuid) storage = self.app.nm.createStorage(uuid=storage_uuid)
client_uuid = self.getNewUUID() client_uuid = self.getNewUUID()
client = self.app.nm.createClient(uuid=client_uuid) client = self.app.nm.createClient(uuid=client_uuid)
self.app.nm.add(storage) self.app.nm.add(storage)
self.app.nm.add(client) self.app.nm.add(client)
# create conn and patch em # create conn and patch em
master_conn = Mock({"getUUID" : master_uuid}) master_conn = Mock({"getUUID" : master_uuid})
storage_conn = Mock({"getUUID" : storage_uuid}) storage_conn = Mock({"getUUID" : storage_uuid})
client_conn = Mock({"getUUID" : client_uuid}) client_conn = Mock({"getUUID" : client_uuid})
self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)}) self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)})
# no address defined, not send to client node # no address defined, not send to client node
c_node = self.app.nm.createClient(uuid = self.getNewUUID()) c_node = self.app.nm.createClient(uuid = self.getNewUUID())
self.app.broadcastNodeInformation(c_node) self.app.broadcastNodeInformation(c_node)
# check conn # check conn
self.checkNoPacketSent(client_conn) self.checkNoPacketSent(client_conn)
self.checkNotifyNodeInformation(master_conn) self.checkNotifyNodeInformation(master_conn)
self.checkNotifyNodeInformation(storage_conn) self.checkNotifyNodeInformation(storage_conn)
# address defined and client type # address defined and client type
master_conn = Mock({"getUUID" : master_uuid}) master_conn = Mock({"getUUID" : master_uuid})
storage_conn = Mock({"getUUID" : storage_uuid}) storage_conn = Mock({"getUUID" : storage_uuid})
client_conn = Mock({"getUUID" : client_uuid}) client_conn = Mock({"getUUID" : client_uuid})
self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)}) self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)})
s_node = self.app.nm.createClient( s_node = self.app.nm.createClient(
uuid = self.getNewUUID(), uuid = self.getNewUUID(),
address=("127.1.0.1", 3361) address=("127.1.0.1", 3361)
) )
self.app.broadcastNodeInformation(c_node) self.app.broadcastNodeInformation(c_node)
# check conn # check conn
self.checkNoPacketSent(client_conn) self.checkNoPacketSent(client_conn)
self.checkNotifyNodeInformation(master_conn) self.checkNotifyNodeInformation(master_conn)
self.checkNotifyNodeInformation(storage_conn) self.checkNotifyNodeInformation(storage_conn)
# address defined and storage type # address defined and storage type
master_conn = Mock({"getUUID" : master_uuid}) master_conn = Mock({"getUUID" : master_uuid})
storage_conn = Mock({"getUUID" : storage_uuid}) storage_conn = Mock({"getUUID" : storage_uuid})
client_conn = Mock({"getUUID" : client_uuid}) client_conn = Mock({"getUUID" : client_uuid})
self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)}) self.app.em = Mock({"getConnectionList" : (master_conn, storage_conn, client_conn)})
s_node = self.app.nm.createStorage( s_node = self.app.nm.createStorage(
uuid=self.getNewUUID(), uuid=self.getNewUUID(),
address=("127.0.0.1", 1351) address=("127.0.0.1", 1351)
) )
self.app.broadcastNodeInformation(s_node) self.app.broadcastNodeInformation(s_node)
# check conn # check conn
self.checkNotifyNodeInformation(client_conn) self.checkNotifyNodeInformation(client_conn)
self.checkNotifyNodeInformation(master_conn) self.checkNotifyNodeInformation(master_conn)
self.checkNotifyNodeInformation(storage_conn) self.checkNotifyNodeInformation(storage_conn)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -37,112 +37,112 @@ class StorageAppTests(NeoTestBase): ...@@ -37,112 +37,112 @@ class StorageAppTests(NeoTestBase):
NeoTestBase.tearDown(self) NeoTestBase.tearDown(self)
def test_01_loadPartitionTable(self): def test_01_loadPartitionTable(self):
self.assertEqual(len(self.app.dm.getPartitionTable()), 0) self.assertEqual(len(self.app.dm.getPartitionTable()), 0)
self.assertEqual(self.app.pt, None) self.assertEqual(self.app.pt, None)
num_partitions = 3 num_partitions = 3
num_replicas = 2 num_replicas = 2
self.app.pt = PartitionTable(num_partitions, num_replicas) self.app.pt = PartitionTable(num_partitions, num_replicas)
self.assertEqual(self.app.pt.getNodeList(), []) self.assertEqual(self.app.pt.getNodeList(), [])
self.assertFalse(self.app.pt.filled()) self.assertFalse(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
self.assertFalse(self.app.pt.hasOffset(x)) self.assertFalse(self.app.pt.hasOffset(x))
# load an empty table # load an empty table
self.app.loadPartitionTable() self.app.loadPartitionTable()
self.assertEqual(self.app.pt.getNodeList(), []) self.assertEqual(self.app.pt.getNodeList(), [])
self.assertFalse(self.app.pt.filled()) self.assertFalse(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
self.assertFalse(self.app.pt.hasOffset(x)) self.assertFalse(self.app.pt.hasOffset(x))
# add some node, will be remove when loading table # add some node, will be remove when loading table
master_uuid = self.getNewUUID() master_uuid = self.getNewUUID()
master = self.app.nm.createMaster(uuid=master_uuid) master = self.app.nm.createMaster(uuid=master_uuid)
storage_uuid = self.getNewUUID() storage_uuid = self.getNewUUID()
storage = self.app.nm.createStorage(uuid=storage_uuid) storage = self.app.nm.createStorage(uuid=storage_uuid)
client_uuid = self.getNewUUID() client_uuid = self.getNewUUID()
client = self.app.nm.createClient(uuid=client_uuid) client = self.app.nm.createClient(uuid=client_uuid)
self.app.pt.setCell(0, master, CellStates.UP_TO_DATE) self.app.pt.setCell(0, master, CellStates.UP_TO_DATE)
self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE) self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE)
self.assertEqual(len(self.app.pt.getNodeList()), 2) self.assertEqual(len(self.app.pt.getNodeList()), 2)
self.assertFalse(self.app.pt.filled()) self.assertFalse(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
if x == 0: if x == 0:
self.assertTrue(self.app.pt.hasOffset(x)) self.assertTrue(self.app.pt.hasOffset(x))
else: else:
self.assertFalse(self.app.pt.hasOffset(x)) self.assertFalse(self.app.pt.hasOffset(x))
# load an empty table, everything removed # load an empty table, everything removed
self.assertEqual(len(self.app.dm.getPartitionTable()), 0) self.assertEqual(len(self.app.dm.getPartitionTable()), 0)
self.app.loadPartitionTable() self.app.loadPartitionTable()
self.assertEqual(self.app.pt.getNodeList(), []) self.assertEqual(self.app.pt.getNodeList(), [])
self.assertFalse(self.app.pt.filled()) self.assertFalse(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
self.assertFalse(self.app.pt.hasOffset(x)) self.assertFalse(self.app.pt.hasOffset(x))
# add some node # add some node
self.app.pt.setCell(0, master, CellStates.UP_TO_DATE) self.app.pt.setCell(0, master, CellStates.UP_TO_DATE)
self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE) self.app.pt.setCell(0, storage, CellStates.UP_TO_DATE)
self.assertEqual(len(self.app.pt.getNodeList()), 2) self.assertEqual(len(self.app.pt.getNodeList()), 2)
self.assertFalse(self.app.pt.filled()) self.assertFalse(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
if x == 0: if x == 0:
self.assertTrue(self.app.pt.hasOffset(x)) self.assertTrue(self.app.pt.hasOffset(x))
else: else:
self.assertFalse(self.app.pt.hasOffset(x)) self.assertFalse(self.app.pt.hasOffset(x))
# fill partition table # fill partition table
self.app.dm.setPTID(1) self.app.dm.setPTID(1)
self.app.dm.query('delete from pt;') self.app.dm.query('delete from pt;')
self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" % self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" %
(0, dump(client_uuid), CellStates.UP_TO_DATE)) (0, dump(client_uuid), CellStates.UP_TO_DATE))
self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" % self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" %
(1, dump(client_uuid), CellStates.UP_TO_DATE)) (1, dump(client_uuid), CellStates.UP_TO_DATE))
self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" % self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" %
(1, dump(storage_uuid), CellStates.UP_TO_DATE)) (1, dump(storage_uuid), CellStates.UP_TO_DATE))
self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" % self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" %
(2, dump(storage_uuid), CellStates.UP_TO_DATE)) (2, dump(storage_uuid), CellStates.UP_TO_DATE))
self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" % self.app.dm.query("insert into pt (rid, uuid, state) values ('%s', '%s', %d)" %
(2, dump(master_uuid), CellStates.UP_TO_DATE)) (2, dump(master_uuid), CellStates.UP_TO_DATE))
self.assertEqual(len(self.app.dm.getPartitionTable()), 5) self.assertEqual(len(self.app.dm.getPartitionTable()), 5)
self.app.pt.clear() self.app.pt.clear()
self.app.loadPartitionTable() self.app.loadPartitionTable()
self.assertTrue(self.app.pt.filled()) self.assertTrue(self.app.pt.filled())
for x in xrange(num_partitions): for x in xrange(num_partitions):
self.assertTrue(self.app.pt.hasOffset(x)) self.assertTrue(self.app.pt.hasOffset(x))
# check each row # check each row
cell_list = self.app.pt.getCellList(0) cell_list = self.app.pt.getCellList(0)
self.assertEqual(len(cell_list), 1) self.assertEqual(len(cell_list), 1)
self.assertEqual(cell_list[0].getUUID(), client_uuid) self.assertEqual(cell_list[0].getUUID(), client_uuid)
cell_list = self.app.pt.getCellList(1) cell_list = self.app.pt.getCellList(1)
self.assertEqual(len(cell_list), 2) self.assertEqual(len(cell_list), 2)
self.failUnless(cell_list[0].getUUID() in (client_uuid, storage_uuid)) self.failUnless(cell_list[0].getUUID() in (client_uuid, storage_uuid))
self.failUnless(cell_list[1].getUUID() in (client_uuid, storage_uuid)) self.failUnless(cell_list[1].getUUID() in (client_uuid, storage_uuid))
cell_list = self.app.pt.getCellList(2) cell_list = self.app.pt.getCellList(2)
self.assertEqual(len(cell_list), 2) self.assertEqual(len(cell_list), 2)
self.failUnless(cell_list[0].getUUID() in (master_uuid, storage_uuid)) self.failUnless(cell_list[0].getUUID() in (master_uuid, storage_uuid))
self.failUnless(cell_list[1].getUUID() in (master_uuid, storage_uuid)) self.failUnless(cell_list[1].getUUID() in (master_uuid, storage_uuid))
def test_02_queueEvent(self): def test_02_queueEvent(self):
self.assertEqual(len(self.app.event_queue), 0) self.assertEqual(len(self.app.event_queue), 0)
event = Mock({"getId": 1325136}) event = Mock({"getId": 1325136})
self.app.queueEvent(event, "test", key="value") self.app.queueEvent(event, "test", key="value")
self.assertEqual(len(self.app.event_queue), 1) self.assertEqual(len(self.app.event_queue), 1)
event, args, kw = self.app.event_queue[0] event, args, kw = self.app.event_queue[0]
self.assertEqual(event.getId(), 1325136) self.assertEqual(event.getId(), 1325136)
self.assertEqual(len(args), 1) self.assertEqual(len(args), 1)
self.assertEqual(args[0], "test") self.assertEqual(args[0], "test")
self.assertEqual(kw, {"key" : "value"}) self.assertEqual(kw, {"key" : "value"})
def test_03_executeQueuedEvents(self): def test_03_executeQueuedEvents(self):
self.assertEqual(len(self.app.event_queue), 0) self.assertEqual(len(self.app.event_queue), 0)
event = Mock({"getId": 1325136}) event = Mock({"getId": 1325136})
self.app.queueEvent(event, "test", key="value") self.app.queueEvent(event, "test", key="value")
self.app.executeQueuedEvents() self.app.executeQueuedEvents()
self.assertEquals(len(event.mockGetNamedCalls("__call__")), 1) self.assertEquals(len(event.mockGetNamedCalls("__call__")), 1)
call = event.mockGetNamedCalls("__call__")[0] call = event.mockGetNamedCalls("__call__")[0]
params = call.getParam(0) params = call.getParam(0)
self.assertEqual(params, "test") self.assertEqual(params, "test")
params = call.kwparams params = call.kwparams
self.assertEqual(params, {'key': 'value'}) self.assertEqual(params, {'key': 'value'})
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
...@@ -30,210 +30,210 @@ class EventTests(NeoTestBase): ...@@ -30,210 +30,210 @@ class EventTests(NeoTestBase):
pass pass
def test_01_EpollEventManager(self): def test_01_EpollEventManager(self):
# init one # init one
em = EpollEventManager() em = EpollEventManager()
self.assertEqual(len(em.connection_dict), 0) self.assertEqual(len(em.connection_dict), 0)
self.assertEqual(len(em.reader_set), 0) self.assertEqual(len(em.reader_set), 0)
self.assertEqual(len(em.writer_set), 0) self.assertEqual(len(em.writer_set), 0)
self.assertEqual(len(em.event_list), 0) self.assertEqual(len(em.event_list), 0)
self.failUnless(em.prev_time <time) self.failUnless(em.prev_time <time)
self.assertTrue(isinstance(em.epoll, Epoll)) self.assertTrue(isinstance(em.epoll, Epoll))
# use a mock object instead of epoll # use a mock object instead of epoll
em.epoll = Mock() em.epoll = Mock()
connector = Mock({"getDescriptor" : 1014}) connector = Mock({"getDescriptor" : 1014})
conn = Mock({'getConnector': connector}) conn = Mock({'getConnector': connector})
self.assertEqual(len(em.getConnectionList()), 0) self.assertEqual(len(em.getConnectionList()), 0)
# test register/unregister # test register/unregister
em.register(conn) em.register(conn)
self.assertEquals(len(connector.mockGetNamedCalls("getDescriptor")), 1) self.assertEquals(len(connector.mockGetNamedCalls("getDescriptor")), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("register")), 1) self.assertEquals(len(em.epoll.mockGetNamedCalls("register")), 1)
call = em.epoll.mockGetNamedCalls("register")[0] call = em.epoll.mockGetNamedCalls("register")[0]
data = call.getParam(0) data = call.getParam(0)
self.assertEqual(data, 1014) self.assertEqual(data, 1014)
self.assertEqual(len(em.getConnectionList()), 1) self.assertEqual(len(em.getConnectionList()), 1)
self.assertEqual(em.getConnectionList()[0].getDescriptor(), conn.getDescriptor()) self.assertEqual(em.getConnectionList()[0].getDescriptor(), conn.getDescriptor())
connector = Mock({"getDescriptor" : 1014}) connector = Mock({"getDescriptor" : 1014})
conn = Mock({'getConnector': connector}) conn = Mock({'getConnector': connector})
em.unregister(conn) em.unregister(conn)
self.assertEquals(len(connector.mockGetNamedCalls("getDescriptor")), 1) self.assertEquals(len(connector.mockGetNamedCalls("getDescriptor")), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("unregister")), 1) self.assertEquals(len(em.epoll.mockGetNamedCalls("unregister")), 1)
call = em.epoll.mockGetNamedCalls("unregister")[0] call = em.epoll.mockGetNamedCalls("unregister")[0]
data = call.getParam(0) data = call.getParam(0)
self.assertEqual(data, 1014) self.assertEqual(data, 1014)
self.assertEqual(len(em.getConnectionList()), 0) self.assertEqual(len(em.getConnectionList()), 0)
# add/removeIdleEvent # add/removeIdleEvent
event = Mock() event = Mock()
self.assertEqual(len(em.event_list), 0) self.assertEqual(len(em.event_list), 0)
em.addIdleEvent(event) em.addIdleEvent(event)
self.assertEqual(len(em.event_list), 1) self.assertEqual(len(em.event_list), 1)
em.removeIdleEvent(event) em.removeIdleEvent(event)
self.assertEqual(len(em.event_list), 0) self.assertEqual(len(em.event_list), 0)
em.removeIdleEvent(event) # must not fail em.removeIdleEvent(event) # must not fail
self.assertEqual(len(em.event_list), 0) self.assertEqual(len(em.event_list), 0)
# add/removeReader # add/removeReader
connector = Mock({"getDescriptor" : 1515}) connector = Mock({"getDescriptor" : 1515})
conn = Mock({'getConnector': connector}) conn = Mock({'getConnector': connector})
self.assertEqual(len(em.reader_set), 0) self.assertEqual(len(em.reader_set), 0)
em.addReader(conn) em.addReader(conn)
self.assertEqual(len(em.reader_set), 1) self.assertEqual(len(em.reader_set), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 1) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 1)
em.addReader(conn) # do not add if already present em.addReader(conn) # do not add if already present
self.assertEqual(len(em.reader_set), 1) self.assertEqual(len(em.reader_set), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 1) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 1)
em.removeReader(conn) em.removeReader(conn)
self.assertEqual(len(em.reader_set), 0) self.assertEqual(len(em.reader_set), 0)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 2) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 2)
em.removeReader(conn) em.removeReader(conn)
self.assertEqual(len(em.reader_set), 0) self.assertEqual(len(em.reader_set), 0)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 2) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 2)
# add/removeWriter # add/removeWriter
connector = Mock({"getDescriptor" : 1515}) connector = Mock({"getDescriptor" : 1515})
conn = Mock({'getConnector': connector}) conn = Mock({'getConnector': connector})
self.assertEqual(len(em.writer_set), 0) self.assertEqual(len(em.writer_set), 0)
em.addWriter(conn) em.addWriter(conn)
self.assertEqual(len(em.writer_set), 1) self.assertEqual(len(em.writer_set), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 3) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 3)
em.addWriter(conn) # do not add if already present em.addWriter(conn) # do not add if already present
self.assertEqual(len(em.writer_set), 1) self.assertEqual(len(em.writer_set), 1)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 3) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 3)
em.removeWriter(conn) em.removeWriter(conn)
self.assertEqual(len(em.writer_set), 0) self.assertEqual(len(em.writer_set), 0)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 4) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 4)
em.removeWriter(conn) em.removeWriter(conn)
self.assertEqual(len(em.writer_set), 0) self.assertEqual(len(em.writer_set), 0)
self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 4) self.assertEquals(len(em.epoll.mockGetNamedCalls("modify")), 4)
# poll # poll
r_connector = Mock({"getDescriptor" : 14515}) r_connector = Mock({"getDescriptor" : 14515})
r_conn = Mock({'getConnector': r_connector}) r_conn = Mock({'getConnector': r_connector})
em.register(r_conn) em.register(r_conn)
w_connector = Mock({"getDescriptor" : 351621}) w_connector = Mock({"getDescriptor" : 351621})
w_conn = Mock({'getConnector': w_connector}) w_conn = Mock({'getConnector': w_connector})
em.register(w_conn) em.register(w_conn)
em.epoll = Mock({"poll":((r_conn.getDescriptor(),),(w_conn.getDescriptor(),))}) em.epoll = Mock({"poll":((r_conn.getDescriptor(),),(w_conn.getDescriptor(),))})
em.poll(timeout=10) em.poll(timeout=10)
# check it called poll on epoll # check it called poll on epoll
self.assertEquals(len(em.epoll.mockGetNamedCalls("poll")), 1) self.assertEquals(len(em.epoll.mockGetNamedCalls("poll")), 1)
call = em.epoll.mockGetNamedCalls("poll")[0] call = em.epoll.mockGetNamedCalls("poll")[0]
data = call.getParam(0) data = call.getParam(0)
self.assertEqual(data, 10) self.assertEqual(data, 10)
# need to rebuild completely this test and the the packet queue # need to rebuild completely this test and the the packet queue
# check readable conn # check readable conn
#self.assertEquals(len(r_conn.mockGetNamedCalls("lock")), 1) #self.assertEquals(len(r_conn.mockGetNamedCalls("lock")), 1)
#self.assertEquals(len(r_conn.mockGetNamedCalls("unlock")), 1) #self.assertEquals(len(r_conn.mockGetNamedCalls("unlock")), 1)
#self.assertEquals(len(r_conn.mockGetNamedCalls("readable")), 1) #self.assertEquals(len(r_conn.mockGetNamedCalls("readable")), 1)
#self.assertEquals(len(r_conn.mockGetNamedCalls("writable")), 0) #self.assertEquals(len(r_conn.mockGetNamedCalls("writable")), 0)
# check writable conn # check writable conn
#self.assertEquals(len(w_conn.mockGetNamedCalls("lock")), 1) #self.assertEquals(len(w_conn.mockGetNamedCalls("lock")), 1)
#self.assertEquals(len(w_conn.mockGetNamedCalls("unlock")), 1) #self.assertEquals(len(w_conn.mockGetNamedCalls("unlock")), 1)
#self.assertEquals(len(w_conn.mockGetNamedCalls("readable")), 0) #self.assertEquals(len(w_conn.mockGetNamedCalls("readable")), 0)
#self.assertEquals(len(w_conn.mockGetNamedCalls("writable")), 1) #self.assertEquals(len(w_conn.mockGetNamedCalls("writable")), 1)
def test_02_IdleEvent(self): def test_02_IdleEvent(self):
# test init # test init
handler = Mock() handler = Mock()
conn = Mock({"getAddress" : ("127.9.9.9", 135), conn = Mock({"getAddress" : ("127.9.9.9", 135),
"getHandler" : handler}) "getHandler" : handler})
event = IdleEvent(conn, 1, 10, 20) event = IdleEvent(conn, 1, 10, 20)
self.assertEqual(event.getId(), 1) self.assertEqual(event.getId(), 1)
self.assertNotEqual(event.getTime(), None) self.assertNotEqual(event.getTime(), None)
time = event.getTime() time = event.getTime()
self.assertNotEqual(event.getCriticalTime(), None) self.assertNotEqual(event.getCriticalTime(), None)
critical_time = event.getCriticalTime() critical_time = event.getCriticalTime()
self.assertEqual(critical_time, time+20) self.assertEqual(critical_time, time+20)
# call with t < time < critical_time # call with t < time < critical_time
t = time - 10 t = time - 10
r = event(t) r = event(t)
self.assertFalse(r) self.assertFalse(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0) self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < t < critical_time # call with time < t < critical_time
t = time + 5 t = time + 5
self.failUnless(t < critical_time) self.failUnless(t < critical_time)
r = event(t) r = event(t)
self.assertTrue(r) self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0) self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("ping")), 1) self.assertEquals(len(conn.mockGetNamedCalls("ping")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < critical_time < t # call with time < critical_time < t
t = critical_time + 5 t = critical_time + 5
self.failUnless(t > critical_time) self.failUnless(t > critical_time)
r = event(t) r = event(t)
self.assertTrue(r) self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 1) self.assertEquals(len(conn.mockGetNamedCalls("close")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("ping")), 1) self.assertEquals(len(conn.mockGetNamedCalls("ping")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1)
# same test with additional time < 5 # same test with additional time < 5
# test init # test init
handler = Mock() handler = Mock()
conn = Mock({"getAddress" : ("127.9.9.9", 135), conn = Mock({"getAddress" : ("127.9.9.9", 135),
"getHandler" : handler}) "getHandler" : handler})
event = IdleEvent(conn, 1, 10, 3) event = IdleEvent(conn, 1, 10, 3)
self.assertEqual(event.getId(), 1) self.assertEqual(event.getId(), 1)
self.assertNotEqual(event.getTime(), None) self.assertNotEqual(event.getTime(), None)
time = event.getTime() time = event.getTime()
self.assertNotEqual(event.getCriticalTime(), None) self.assertNotEqual(event.getCriticalTime(), None)
critical_time = event.getCriticalTime() critical_time = event.getCriticalTime()
self.assertEqual(critical_time, time+3) self.assertEqual(critical_time, time+3)
# call with t < time < critical_time # call with t < time < critical_time
t = time - 10 t = time - 10
r = event(t) r = event(t)
self.assertFalse(r) self.assertFalse(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0) self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 0)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < t < critical_time # call with time < t < critical_time
t = time + 1 t = time + 1
self.failUnless(t < critical_time) self.failUnless(t < critical_time)
r = event(t) r = event(t)
self.assertTrue(r) self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0) self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 0)
# call with time < critical_time < t # call with time < critical_time < t
t = critical_time + 5 t = critical_time + 5
self.failUnless(t > critical_time) self.failUnless(t > critical_time)
r = event(t) r = event(t)
self.assertTrue(r) self.assertTrue(r)
self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2) self.assertEquals(len(conn.mockGetNamedCalls("lock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1) self.assertEquals(len(conn.mockGetNamedCalls("getHandler")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 1) self.assertEquals(len(conn.mockGetNamedCalls("close")), 1)
self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2) self.assertEquals(len(conn.mockGetNamedCalls("unlock")), 2)
self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1) self.assertEquals(len(conn.mockGetNamedCalls("expectMessage")), 1)
self.checkNoPacketSent(conn) self.checkNoPacketSent(conn)
self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1) self.assertEquals(len(handler.mockGetNamedCalls("timeoutExpired")), 1)
if __name__ == '__main__': if __name__ == '__main__':
......
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