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

Fix and simplify connection tests suite.

Remove MT* tests as then are just copy/paste of non-MT tests.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1885 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 215250bb
...@@ -16,11 +16,10 @@ ...@@ -16,11 +16,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import unittest import unittest
from mock import Mock from mock import Mock
from neo.connection import BaseConnection, ListeningConnection, Connection, \ from neo.connection import ListeningConnection, Connection, \
ClientConnection, ServerConnection, MTClientConnection, \ ClientConnection, ServerConnection, MTClientConnection, \
MTServerConnection, HandlerSwitcher MTServerConnection, HandlerSwitcher
from neo.connector import getConnectorHandler, registerConnectorHandler from neo.connector import getConnectorHandler, registerConnectorHandler
from neo.handler import EventHandler
from neo.tests import DoNothingConnector from neo.tests import DoNothingConnector
from neo.connector import ConnectorException, ConnectorTryAgainException, \ from neo.connector import ConnectorException, ConnectorTryAgainException, \
ConnectorInProgressException, ConnectorConnectionRefusedException ConnectorInProgressException, ConnectorConnectionRefusedException
...@@ -30,127 +29,168 @@ from neo.tests import NeoTestBase ...@@ -30,127 +29,168 @@ from neo.tests import NeoTestBase
class ConnectionTests(NeoTestBase): class ConnectionTests(NeoTestBase):
def setUp(self): def setUp(self):
pass self.app = Mock({'__repr__': 'Fake App'})
self.em = Mock({'__repr__': 'Fake Em'})
self.handler = Mock({'__repr__': 'Fake Handler'})
self.address = ("127.0.0.7", 93413)
def tearDown(self): def tearDown(self):
pass pass
def test_01_BaseConnection(self): def _makeListeningConnection(self, addr):
app = Mock() # create instance after monkey patches
em = Mock() #EpollEventManager() self.connector = DoNothingConnector()
handler = EventHandler(app) return ListeningConnection(event_manager=self.em, handler=self.handler,
# no connector connector=self.connector, addr=addr)
bc = BaseConnection(em, handler)
self.assertNotEqual(bc.em, None) def _makeConnection(self):
self.assertEqual(bc.getHandler(), handler) self.connector = DoNothingConnector()
self.assertNotEqual(bc.getEventManager(), None) return Connection(event_manager=self.em, handler=self.handler,
self.assertEqual(bc.getHandler(), handler) connector=self.connector, addr=self.address)
self.assertEqual(bc.getUUID(), None)
self.assertEqual(bc.lock(), 1) def _makeClientConnection(self):
self.assertEqual(bc.unlock(), None) self.connector = DoNothingConnector()
self.assertEqual(bc.getAddress(), None) return ClientConnection(event_manager=self.em, handler=self.handler,
self.assertEqual(bc.getConnector(), None) connector=self.connector, addr=self.address)
self.assertRaises(NotImplementedError, bc.readable)
self.assertRaises(NotImplementedError, bc.writable) def _makeServerConnection(self):
self.assertEqual(bc.connector, None) self.connector = DoNothingConnector()
self.assertEqual(bc.addr, None) return ServerConnection(event_manager=self.em, handler=self.handler,
self.assertEqual(bc.connector_handler, None) connector=self.connector, addr=self.address)
# init with connector but no handler def _checkRegistered(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("register")), n)
def _checkUnregistered(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("unregister")), n)
def _checkReaderAdded(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("addReader")), n)
def _checkReaderRemoved(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("removeReader")), n)
def _checkWriterAdded(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("addWriter")), n)
def _checkWriterRemoved(self, n=1):
self.assertEqual(len(self.em.mockGetNamedCalls("removeWriter")), n)
def _checkShutdown(self, n=1):
self.assertEquals(len(self.connector.mockGetNamedCalls("shutdown")), n)
def _checkClose(self, n=1):
self.assertEquals(len(self.connector.mockGetNamedCalls("close")), n)
def _checkGetNewConnection(self, n=1):
calls = self.connector.mockGetNamedCalls('getNewConnection')
self.assertEqual(len(calls), n)
def _checkSend(self, n=1, data=None):
calls = self.connector.mockGetNamedCalls('send')
self.assertEqual(len(calls), n)
if n > 1 and data is not None:
data = calls[n-1].getParam(0)
self.assertEquals(data, "testdata")
def _checkConnectionAccepted(self, n=1):
calls = self.handler.mockGetNamedCalls('connectionAccepted')
self.assertEqual(len(calls), n)
def _checkConnectionFailed(self, n=1):
calls = self.handler.mockGetNamedCalls('connectionFailed')
self.assertEqual(len(calls), n)
def _checkConnectionClosed(self, n=1):
calls = self.handler.mockGetNamedCalls('connectionClosed')
self.assertEqual(len(calls), n)
def _checkConnectionStarted(self, n=1):
calls = self.handler.mockGetNamedCalls('connectionStarted')
self.assertEqual(len(calls), n)
def _checkConnectionCompleted(self, n=1):
calls = self.handler.mockGetNamedCalls('connectionCompleted')
self.assertEqual(len(calls), n)
def _checkMakeListeningConnection(self, n=1):
calls = self.connector.mockGetNamedCalls('makeListeningConnection')
self.assertEqual(len(calls), n)
def _checkMakeClientConnection(self, n=1):
calls = self.connector.mockGetNamedCalls("makeClientConnection")
self.assertEqual(len(calls), n)
self.assertEqual(calls[n-1].getParam(0), self.address)
def _checkAddIdleEvent(self, n=1):
self.assertEquals(len(self.em.mockGetNamedCalls("addIdleEvent")), n)
def _checkRemoveIdleEvent(self, n=1):
self.assertEquals(len(self.em.mockGetNamedCalls("removeIdleEvent")), n)
def _checkPacketReceived(self, n=1):
calls = self.handler.mockGetNamedCalls('packetReceived')
self.assertEquals(len(calls), n)
def _checkReadBuf(self, bc, data):
self.assertEqual(''.join(bc.read_buf), data)
def _checkWriteBuf(self, bc, data):
self.assertEqual(''.join(bc.write_buf), data)
def test_01_BaseConnection1(self):
# init with connector
registerConnectorHandler(DoNothingConnector) registerConnectorHandler(DoNothingConnector)
connector = getConnectorHandler("DoNothingConnector")() connector = getConnectorHandler("DoNothingConnector")()
self.assertNotEqual(connector, None) self.assertNotEqual(connector, None)
em = Mock() bc = self._makeConnection()
bc = BaseConnection(em, handler, connector=connector)
self.assertNotEqual(bc.connector, None) self.assertNotEqual(bc.connector, None)
self.assertNotEqual(bc.getConnector(), None) self._checkRegistered(1)
self.assertEqual(bc.connector_handler, DoNothingConnector)
# check it registered the connection in epoll
self.assertEquals(len(em.mockGetNamedCalls("register")), 1)
call = em.mockGetNamedCalls("register")[0]
conn = call.getParam(0)
self.assertEquals(conn, bc)
# init just with handler
em = Mock()
bc = BaseConnection(em, handler, connector_handler=DoNothingConnector)
self.assertEqual(bc.getConnector(), None)
self.assertEqual(bc.connector_handler, DoNothingConnector)
self.assertEquals(len(em.mockGetNamedCalls("register")), 0)
# add connector
connector = bc.connector_handler()
bc.setConnector(connector)
self.assertTrue(isinstance(bc.getConnector(), DoNothingConnector))
self.assertNotEqual(bc.getConnector(), None)
# check it registered the connection in epoll
self.assertEquals(len(em.mockGetNamedCalls("register")), 1)
call = em.mockGetNamedCalls("register")[0]
conn = call.getParam(0)
self.assertEquals(conn, bc)
def test_01_BaseConnection2(self):
# init with address # init with address
connector = DoNothingConnector() bc = self._makeConnection()
em = Mock() self.assertEqual(bc.getAddress(), self.address)
handler = EventHandler(app) self._checkRegistered(1)
bc = BaseConnection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413))
# check it registered the connection in epoll
self.assertEquals(len(em.mockGetNamedCalls("register")), 1)
call = em.mockGetNamedCalls("register")[0]
conn = call.getParam(0)
self.assertEquals(conn, bc)
def test_02_ListeningConnection1(self): def test_02_ListeningConnection1(self):
# test init part # test init part
em = Mock()
handler = Mock()
def getNewConnection(self): def getNewConnection(self):
return self, "127.0.0.1" return self, "127.0.0.1"
DoNothingConnector.getNewConnection = getNewConnection DoNothingConnector.getNewConnection = getNewConnection
bc = ListeningConnection(em, handler, connector_handler=DoNothingConnector, addr = ("127.0.0.7", 93413)
connector=None, addr=("127.0.0.7", 93413)) bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413)) self.assertEqual(bc.getAddress(), addr)
self.assertEqual(len(em.mockGetNamedCalls("register")), 1) self._checkRegistered()
self.assertEqual(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded()
self.assertNotEqual(bc.getConnector(), None) self._checkMakeListeningConnection()
connector = bc.getConnector()
self.assertEqual(len(connector.mockGetNamedCalls("makeListeningConnection")), 1)
# test readable # test readable
bc.readable() bc.readable()
self.assertEqual(len(connector.mockGetNamedCalls("getNewConnection")), 1) self._checkGetNewConnection()
self.assertEqual(len(handler.mockGetNamedCalls("connectionAccepted")), 1) self._checkConnectionAccepted()
def test_02_ListeningConnection2(self): def test_02_ListeningConnection2(self):
# test with exception raise when getting new connection # test with exception raise when getting new connection
em = Mock()
handler = Mock()
def getNewConnection(self): def getNewConnection(self):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.getNewConnection = getNewConnection DoNothingConnector.getNewConnection = getNewConnection
bc = ListeningConnection(em, handler, connector_handler=DoNothingConnector, addr = ("127.0.0.7", 93413)
connector=None, addr=("127.0.0.7", 93413)) bc = self._makeListeningConnection(addr=addr)
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413)) self.assertEqual(bc.getAddress(), addr)
self.assertEqual(len(em.mockGetNamedCalls("register")), 1) self._checkRegistered()
self.assertEqual(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded()
self.assertNotEqual(bc.getConnector(), None) self._checkMakeListeningConnection()
connector = bc.getConnector()
self.assertEqual(len(connector.mockGetNamedCalls("makeListeningConnection")), 1)
# test readable # test readable
bc.readable() bc.readable()
self.assertEqual(len(connector.mockGetNamedCalls("getNewConnection")), 1) self._checkGetNewConnection(1)
self.assertEqual(len(handler.mockGetNamedCalls("connectionAccepted")), 0) self._checkConnectionAccepted(0)
def test_03_Connection(self): def test_03_Connection(self):
em = Mock() bc = self._makeConnection()
handler = Mock() self.assertEqual(bc.getAddress(), self.address)
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkReaderAdded(1)
connector=None, addr=("127.0.0.7", 93413)) self._checkReadBuf(bc, '')
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413)) self._checkWriteBuf(bc, '')
self.assertEqual(len(em.mockGetNamedCalls("addReader")), 0)
self.assertEqual(bc.getConnector(), None)
self.assertEqual(''.join(bc.read_buf), '')
self.assertEqual(''.join(bc.write_buf), '')
self.assertEqual(bc.cur_id, 0) self.assertEqual(bc.cur_id, 0)
self.assertEqual(bc.event_dict, {}) self.assertEqual(bc.event_dict, {})
self.assertEqual(bc.aborted, False) self.assertEqual(bc.aborted, False)
...@@ -178,309 +218,183 @@ class ConnectionTests(NeoTestBase): ...@@ -178,309 +218,183 @@ class ConnectionTests(NeoTestBase):
self.assertFalse(bc.isServer()) self.assertFalse(bc.isServer())
def test_Connection_pending(self): def test_Connection_pending(self):
em = Mock() bc = self._makeConnection()
handler = Mock()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=None, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '') self.assertEqual(''.join(bc.write_buf), '')
self.assertEqual(bc.connector, None)
# no connector and no buffer
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
# no connector but buffer
bc.write_buf += '1'
self.assertFalse(bc.pending())
# connector with no buffer
conn = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=conn, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
self.assertNotEqual(bc.connector, None)
self.assertFalse(bc.pending())
# connector and buffer
bc.write_buf += '1' bc.write_buf += '1'
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
def test_Connection_recv1(self): def test_Connection_recv1(self):
# patch receive method to return data # patch receive method to return data
em = Mock()
handler = Mock()
def receive(self): def receive(self):
return "testdata" return "testdata"
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkReadBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.read_buf), '')
self.assertNotEqual(bc.getConnector(), None)
bc._recv() bc._recv()
self.assertEqual(''.join(bc.read_buf), "testdata") self._checkReadBuf(bc, 'testdata')
def test_Connection_recv2(self): def test_Connection_recv2(self):
# patch receive method to raise try again # patch receive method to raise try again
em = Mock()
handler = Mock()
def receive(self): def receive(self):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkReadBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.read_buf), '')
self.assertNotEqual(bc.getConnector(), None)
bc._recv() bc._recv()
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0) self._checkConnectionClosed(0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0) self._checkUnregistered(0)
def test_Connection_recv3(self): def test_Connection_recv3(self):
# patch receive method to raise ConnectorConnectionRefusedException # patch receive method to raise ConnectorConnectionRefusedException
em = Mock()
handler = Mock()
def receive(self): def receive(self):
raise ConnectorConnectionRefusedException raise ConnectorConnectionRefusedException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkReadBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.read_buf), '')
self.assertNotEqual(bc.getConnector(), None)
# fake client connection instance with connecting attribute # fake client connection instance with connecting attribute
bc.connecting = True bc.connecting = True
bc._recv() bc._recv()
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 1) self._checkConnectionFailed(1)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 1) self._checkUnregistered(1)
def test_Connection_recv4(self): def test_Connection_recv4(self):
# patch receive method to raise any other connector error # patch receive method to raise any other connector error
em = Mock()
handler = Mock()
def receive(self): def receive(self):
raise ConnectorException raise ConnectorException
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkReadBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.read_buf), '')
self.assertNotEqual(bc.getConnector(), None)
self.assertRaises(ConnectorException, bc._recv) self.assertRaises(ConnectorException, bc._recv)
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 1) self._checkConnectionClosed(1)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 1) self._checkUnregistered(1)
def test_Connection_send1(self): def test_Connection_send1(self):
# no data, nothing done # no data, nothing done
em = Mock()
handler = Mock()
# patch receive method to return data # patch receive method to return data
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 0) self._checkSend(0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0) self._checkConnectionClosed(0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0) self._checkUnregistered(0)
def test_Connection_send2(self): def test_Connection_send2(self):
# send all data # send all data
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, '')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdata") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
def test_Connection_send3(self): def test_Connection_send3(self):
# send part of the data # send part of the data
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, 'data')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdata") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), "data")
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
def test_Connection_send4(self): def test_Connection_send4(self):
# send multiple packet # send multiple packet
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdatasecondthird")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, '')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdatasecondthird") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
def test_Connection_send5(self): def test_Connection_send5(self):
# send part of multiple packet # send part of multiple packet
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdatasecondthird")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, 'econdthird')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdatasecondthird") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), "econdthird")
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
def test_Connection_send6(self): def test_Connection_send6(self):
# raise try again # raise try again
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
raise ConnectorTryAgainException raise ConnectorTryAgainException
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
self.assertNotEqual(bc.getConnector(), None)
bc._send() bc._send()
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdatasecondthird")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, 'testdatasecondthird')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdatasecondthird") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), "testdata" + "second" + "third")
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
def test_Connection_send7(self): def test_Connection_send7(self):
# raise other error # raise other error
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
raise ConnectorException raise ConnectorException
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata", "second", "third"] bc.write_buf = ["testdata", "second", "third"]
self.assertNotEqual(bc.getConnector(), None)
self.assertRaises(ConnectorException, bc._send) self.assertRaises(ConnectorException, bc._send)
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdatasecondthird")
call = connector.mockGetNamedCalls("send")[0]
data = call.getParam(0)
self.assertEquals(data, "testdatasecondthird")
# connection closed -> buffers flushed # connection closed -> buffers flushed
self.assertEqual(''.join(bc.write_buf), "") self._checkWriteBuf(bc, '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 1) self._checkReaderRemoved(1)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 1) self._checkConnectionClosed(1)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 1) self._checkUnregistered(1)
def test_07_Connection_addPacket(self): def test_07_Connection_addPacket(self):
# no connector
p = Mock({"encode" : "testdata"})
em = Mock()
handler = Mock()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=None, addr=("127.0.0.7", 93413))
self.assertEqual(bc.getConnector(), None)
self.assertEqual(''.join(bc.write_buf), '')
bc._addPacket(p)
self.assertEqual(''.join(bc.write_buf), '')
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 0)
# new packet # new packet
connector = DoNothingConnector() p = Mock({"encode" : "testdata"})
bc = Connection(em, handler, connector_handler=DoNothingConnector, bc = self._makeConnection()
connector=connector, addr=("127.0.0.7", 93413)) self._checkWriteBuf(bc, '')
self.assertEqual(''.join(bc.write_buf), '')
self.assertNotEqual(bc.getConnector(), None)
bc._addPacket(p) bc._addPacket(p)
self.assertEqual(''.join(bc.write_buf), "testdata") self._checkWriteBuf(bc, 'testdata')
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 1) self._checkWriterAdded(1)
def test_08_Connection_expectMessage(self): def test_08_Connection_expectMessage(self):
# no connector -> nothing is done
em = Mock()
handler = Mock()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=None, addr=("127.0.0.7", 93413))
self.assertEqual(bc.getConnector(), None)
self.assertEqual(len(bc.event_dict), 0)
bc.expectMessage('1')
self.assertEqual(len(bc.event_dict), 0)
self.assertEquals(len(em.mockGetNamedCalls("addIdleEvent")), 0)
# with a right connector -> event created # with a right connector -> event created
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
self.assertNotEqual(bc.getConnector(), None)
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.expectMessage('1') bc.expectMessage('1')
self.assertEqual(len(bc.event_dict), 1) self.assertEqual(len(bc.event_dict), 1)
self.assertEquals(len(em.mockGetNamedCalls("addIdleEvent")), 1) self._checkAddIdleEvent(1)
def test_Connection_analyse1(self): def test_Connection_analyse1(self):
# nothing to read, nothing is done # nothing to read, nothing is done
em = Mock() bc = self._makeConnection()
handler = Mock()
connector = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
bc._queue = Mock() bc._queue = Mock()
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.analyse() bc.analyse()
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 0) self._checkRemoveIdleEvent(0)
self.assertEquals(len(handler.mockGetNamedCalls("packetReceived")), 0) self._checkPacketReceived(0)
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
# give some data to analyse # give some data to analyse
...@@ -499,7 +413,7 @@ class ConnectionTests(NeoTestBase): ...@@ -499,7 +413,7 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.analyse() bc.analyse()
# check packet decoded # check packet decoded
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 0) self._checkRemoveIdleEvent(0)
self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1) self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1)
call = bc._queue.mockGetNamedCalls("append")[0] call = bc._queue.mockGetNamedCalls("append")[0]
data = call.getParam(0) data = call.getParam(0)
...@@ -507,15 +421,11 @@ class ConnectionTests(NeoTestBase): ...@@ -507,15 +421,11 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(data.getId(), p.getId()) self.assertEqual(data.getId(), p.getId())
self.assertEqual(data.decode(), p.decode()) self.assertEqual(data.decode(), p.decode())
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
def test_Connection_analyse2(self): def test_Connection_analyse2(self):
# give multiple packet # give multiple packet
em = Mock() bc = self._makeConnection()
handler = Mock()
connector = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
bc._queue = Mock() bc._queue = Mock()
# packet 1 # packet 1
master_list = ( master_list = (
...@@ -547,7 +457,7 @@ class ConnectionTests(NeoTestBase): ...@@ -547,7 +457,7 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
bc.analyse() bc.analyse()
# check two packets decoded # check two packets decoded
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 0) self._checkRemoveIdleEvent(0)
self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 2) self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 2)
# packet 1 # packet 1
call = bc._queue.mockGetNamedCalls("append")[0] call = bc._queue.mockGetNamedCalls("append")[0]
...@@ -562,15 +472,11 @@ class ConnectionTests(NeoTestBase): ...@@ -562,15 +472,11 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(data.getId(), p2.getId()) self.assertEqual(data.getId(), p2.getId())
self.assertEqual(data.decode(), p2.decode()) self.assertEqual(data.decode(), p2.decode())
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
def test_Connection_analyse3(self): def test_Connection_analyse3(self):
# give a bad packet, won't be decoded # give a bad packet, won't be decoded
em = Mock() bc = self._makeConnection()
handler = Mock()
connector = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
bc._queue = Mock() bc._queue = Mock()
bc.read_buf += "datadatadatadata" bc.read_buf += "datadatadatadata"
self.assertEqual(len(bc.read_buf), 16) self.assertEqual(len(bc.read_buf), 16)
...@@ -578,17 +484,12 @@ class ConnectionTests(NeoTestBase): ...@@ -578,17 +484,12 @@ class ConnectionTests(NeoTestBase):
bc.analyse() bc.analyse()
self.assertEqual(len(bc.read_buf), 16) self.assertEqual(len(bc.read_buf), 16)
self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 0) self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 0)
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 0) self._checkRemoveIdleEvent(0)
def test_Connection_analyse4(self): def test_Connection_analyse4(self):
# give an expected packet # give an expected packet
em = Mock() bc = self._makeConnection()
handler = Mock()
connector = DoNothingConnector()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
bc._queue = Mock() bc._queue = Mock()
master_list = ( master_list = (
(("127.0.0.1", 2135), self.getNewUUID()), (("127.0.0.1", 2135), self.getNewUUID()),
(("127.0.0.1", 2135), self.getNewUUID()), (("127.0.0.1", 2135), self.getNewUUID()),
...@@ -606,7 +507,7 @@ class ConnectionTests(NeoTestBase): ...@@ -606,7 +507,7 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(len(bc.event_dict), 1) self.assertEqual(len(bc.event_dict), 1)
bc.analyse() bc.analyse()
# check packet decoded # check packet decoded
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 1) self._checkRemoveIdleEvent(1)
self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1) self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1)
call = bc._queue.mockGetNamedCalls("append")[0] call = bc._queue.mockGetNamedCalls("append")[0]
data = call.getParam(0) data = call.getParam(0)
...@@ -618,105 +519,79 @@ class ConnectionTests(NeoTestBase): ...@@ -618,105 +519,79 @@ class ConnectionTests(NeoTestBase):
def test_Connection_writable1(self): def test_Connection_writable1(self):
# with pending operation after send # with pending operation after send
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data)/2 return len(data)/2
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertNotEqual(bc.getConnector(), None)
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
bc.writable() bc.writable()
# test send was called # test send was called
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = connector.mockGetNamedCalls("send")[0]
data = call.getParam(0)
self.assertEquals(data, "testdata")
self.assertEqual(''.join(bc.write_buf), "data") self.assertEqual(''.join(bc.write_buf), "data")
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0) self._checkConnectionClosed(0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0) self._checkUnregistered(0)
# pending, so nothing called # pending, so nothing called
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 0) self._checkWriterRemoved(0)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 0) self._checkReaderRemoved(0)
self.assertEquals(len(connector.mockGetNamedCalls("shutdown")), 0) self._checkShutdown(0)
self.assertEquals(len(connector.mockGetNamedCalls("close")), 0) self._checkClose(0)
def test_Connection_writable2(self): def test_Connection_writable2(self):
# with no longer pending operation after send # with no longer pending operation after send
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertNotEqual(bc.getConnector(), None)
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
bc.writable() bc.writable()
# test send was called # test send was called
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, '')
data = call.getParam(0) self._checkClose(0)
self.assertEquals(data, "testdata") self._checkUnregistered(0)
self.assertEqual(''.join(bc.write_buf), '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
# nothing else pending, and aborted is false, so writer has been removed # nothing else pending, and aborted is false, so writer has been removed
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 1) self._checkWriterRemoved(1)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 0) self._checkReaderRemoved(0)
self.assertEquals(len(connector.mockGetNamedCalls("shutdown")), 0) self._checkShutdown(0)
self.assertEquals(len(connector.mockGetNamedCalls("close")), 0) self._checkClose(0)
def test_Connection_writable3(self): def test_Connection_writable3(self):
# with no longer pending operation after send and aborted set to true # with no longer pending operation after send and aborted set to true
em = Mock()
handler = Mock()
def send(self, data): def send(self, data):
return len(data) return len(data)
DoNothingConnector.send = send DoNothingConnector.send = send
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector, self._checkWriteBuf(bc, '')
connector=connector, addr=("127.0.0.7", 93413))
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertNotEqual(bc.getConnector(), None)
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
bc.abort() bc.abort()
self.assertTrue(bc.aborted) self.assertTrue(bc.aborted)
bc.writable() bc.writable()
# test send was called # test send was called
self.assertEquals(len(connector.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = connector.mockGetNamedCalls("send")[0] self._checkWriteBuf(bc, '')
data = call.getParam(0) self._checkConnectionClosed(0)
self.assertEquals(data, "testdata") self._checkUnregistered(1)
self.assertEqual(''.join(bc.write_buf), '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 1)
# nothing else pending, and aborted is false, so writer has been removed # nothing else pending, and aborted is false, so writer has been removed
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
self.assertTrue(bc.aborted) self.assertTrue(bc.aborted)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 1) self._checkWriterRemoved(1)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 1) self._checkReaderRemoved(1)
self.assertEquals(len(connector.mockGetNamedCalls("shutdown")), 1) self._checkShutdown(1)
self.assertEquals(len(connector.mockGetNamedCalls("close")), 1) self._checkClose(1)
def test_Connection_readable(self): def test_Connection_readable(self):
# With aborted set to false # With aborted set to false
em = Mock()
handler = Mock()
# patch receive method to return data # patch receive method to return data
def receive(self): def receive(self):
master_list = ((("127.0.0.1", 2135), self.getNewUUID()), master_list = ((("127.0.0.1", 2135), self.getNewUUID()),
...@@ -732,115 +607,91 @@ class ConnectionTests(NeoTestBase): ...@@ -732,115 +607,91 @@ class ConnectionTests(NeoTestBase):
p.setId(1) p.setId(1)
return ''.join(p.encode()) return ''.join(p.encode())
DoNothingConnector.receive = receive DoNothingConnector.receive = receive
connector = DoNothingConnector() bc = self._makeConnection()
bc = Connection(em, handler, connector_handler=DoNothingConnector,
connector=connector, addr=("127.0.0.7", 93413))
bc._queue = Mock() bc._queue = Mock()
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertNotEqual(bc.getConnector(), None)
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
bc.readable() bc.readable()
# check packet decoded # check packet decoded
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
self.assertEquals(len(em.mockGetNamedCalls("removeIdleEvent")), 0) self._checkRemoveIdleEvent(0)
self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1) self.assertEquals(len(bc._queue.mockGetNamedCalls("append")), 1)
call = bc._queue.mockGetNamedCalls("append")[0] call = bc._queue.mockGetNamedCalls("append")[0]
data = call.getParam(0) data = call.getParam(0)
self.assertEqual(data.getType(), Packets.AnswerPrimary) self.assertEqual(data.getType(), Packets.AnswerPrimary)
self.assertEqual(data.getId(), 1) self.assertEqual(data.getId(), 1)
self.assertEqual(len(bc.event_dict), 0) self.assertEqual(len(bc.event_dict), 0)
self.assertEqual(''.join(bc.read_buf), '') self._checkReadBuf(bc, '')
# check not aborted # check not aborted
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0) self._checkUnregistered(0)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 0) self._checkWriterRemoved(0)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 0) self._checkReaderRemoved(0)
self.assertEquals(len(connector.mockGetNamedCalls("shutdown")), 0) self._checkShutdown(0)
self.assertEquals(len(connector.mockGetNamedCalls("close")), 0) self._checkClose(0)
def test_ClientConnection_init1(self): def test_ClientConnection_init1(self):
# create a good client connection # create a good client connection
em = Mock() bc = self._makeClientConnection()
handler = Mock()
bc = ClientConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413))
# check connector created and connection initialize # check connector created and connection initialize
self.assertFalse(bc.connecting) self.assertFalse(bc.connecting)
self.assertFalse(bc.isServer()) self.assertFalse(bc.isServer())
self.assertNotEqual(bc.getConnector(), None) self._checkMakeClientConnection(1)
conn = bc.getConnector()
self.assertEquals(len(conn.mockGetNamedCalls("makeClientConnection")), 1)
call = conn.mockGetNamedCalls("makeClientConnection")[0]
data = call.getParam(0)
self.assertEqual(data, ("127.0.0.7", 93413))
# check call to handler # check call to handler
self.assertNotEqual(bc.getHandler(), None) self.assertNotEqual(bc.getHandler(), None)
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1) self._checkConnectionStarted(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1) self._checkConnectionCompleted(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0) self._checkConnectionFailed(0)
# check call to event manager # check call to event manager
self.assertNotEqual(bc.getEventManager(), None) self.assertNotEqual(bc.getEventManager(), None)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded(1)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 0) self._checkWriterAdded(0)
def test_ClientConnection_init2(self): def test_ClientConnection_init2(self):
# raise connection in progress # raise connection in progress
makeClientConnection_org = DoNothingConnector.makeClientConnection makeClientConnection_org = DoNothingConnector.makeClientConnection
def makeClientConnection(self, *args, **kw): def makeClientConnection(self, *args, **kw):
raise ConnectorInProgressException raise ConnectorInProgressException
em = Mock()
handler = Mock()
DoNothingConnector.makeClientConnection = makeClientConnection DoNothingConnector.makeClientConnection = makeClientConnection
try: try:
bc = ClientConnection(em, handler, connector_handler=DoNothingConnector, bc = self._makeClientConnection()
addr=("127.0.0.7", 93413))
finally: finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org DoNothingConnector.makeClientConnection = makeClientConnection_org
# check connector created and connection initialize # check connector created and connection initialize
self.assertTrue(bc.connecting) self.assertTrue(bc.connecting)
self.assertFalse(bc.isServer()) self.assertFalse(bc.isServer())
self.assertNotEqual(bc.getConnector(), None) self._checkMakeClientConnection(1)
conn = bc.getConnector()
self.assertEquals(len(conn.mockGetNamedCalls("makeClientConnection")), 1)
call = conn.mockGetNamedCalls("makeClientConnection")[0]
data = call.getParam(0)
self.assertEqual(data, ("127.0.0.7", 93413))
# check call to handler # check call to handler
self.assertNotEqual(bc.getHandler(), None) self.assertNotEqual(bc.getHandler(), None)
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1) self._checkConnectionStarted(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 0) self._checkConnectionCompleted(0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0) self._checkConnectionFailed(0)
# check call to event manager # check call to event manager
self.assertNotEqual(bc.getEventManager(), None) self.assertNotEqual(bc.getEventManager(), None)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 0) self._checkReaderAdded(1)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 1) self._checkWriterAdded(1)
def test_ClientConnection_init3(self): def test_ClientConnection_init3(self):
# raise another error, connection must fail # raise another error, connection must fail
makeClientConnection_org = DoNothingConnector.makeClientConnection makeClientConnection_org = DoNothingConnector.makeClientConnection
def makeClientConnection(self, *args, **kw): def makeClientConnection(self, *args, **kw):
raise ConnectorException raise ConnectorException
em = Mock()
handler = Mock()
DoNothingConnector.makeClientConnection = makeClientConnection DoNothingConnector.makeClientConnection = makeClientConnection
try: try:
self.assertRaises(ConnectorException, ClientConnection, em, handler, self.assertRaises(ConnectorException, self._makeClientConnection)
connector_handler=DoNothingConnector, addr=("127.0.0.7", 93413))
finally: finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org DoNothingConnector.makeClientConnection = makeClientConnection_org
# since the exception was raised, the connection is not created # since the exception was raised, the connection is not created
# check call to handler # check call to handler
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1) self._checkConnectionStarted(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 0) self._checkConnectionCompleted(0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 1) self._checkConnectionFailed(1)
# check call to event manager # check call to event manager
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 0) self._checkReaderAdded(1)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 0) self._checkWriterAdded(0)
def test_ClientConnection_writable1(self): def test_ClientConnection_writable1(self):
# with a non connecting connection, will call parent's method # with a non connecting connection, will call parent's method
em = Mock()
handler = Mock()
def makeClientConnection(self, *args, **kw): def makeClientConnection(self, *args, **kw):
return "OK" return "OK"
def send(self, data): def send(self, data):
...@@ -849,249 +700,68 @@ class ConnectionTests(NeoTestBase): ...@@ -849,249 +700,68 @@ class ConnectionTests(NeoTestBase):
DoNothingConnector.send = send DoNothingConnector.send = send
DoNothingConnector.makeClientConnection = makeClientConnection DoNothingConnector.makeClientConnection = makeClientConnection
try: try:
bc = ClientConnection(em, handler, connector_handler=DoNothingConnector, bc = self._makeClientConnection()
addr=("127.0.0.7", 93413))
finally: finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org DoNothingConnector.makeClientConnection = makeClientConnection_org
# check connector created and connection initialize # check connector created and connection initialize
self.assertFalse(bc.connecting) self.assertFalse(bc.connecting)
self.assertNotEqual(bc.getConnector(), None) self._checkWriteBuf(bc, '')
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
# call # call
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1) self._checkConnectionCompleted(1)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded(1)
bc.writable() bc.writable()
conn = bc.getConnector()
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self.assertFalse(bc.connecting) self.assertFalse(bc.connecting)
self.assertEquals(len(conn.mockGetNamedCalls("send")), 1) self._checkSend(1, "testdata")
call = conn.mockGetNamedCalls("send")[0] self._checkConnectionClosed(0)
data = call.getParam(0) self._checkConnectionCompleted(1)
self.assertEquals(data, "testdata") self._checkConnectionFailed(0)
self.assertEqual(''.join(bc.write_buf), '') self._checkUnregistered(0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0) self._checkReaderAdded(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1) self._checkWriterRemoved(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0) self._checkReaderRemoved(0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0) self._checkShutdown(0)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1) self._checkClose(0)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 1)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("shutdown")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("send")), 1)
call = conn.mockGetNamedCalls("send")[0]
data = call.getParam(0)
self.assertEquals(data, "testdata")
def test_ClientConnection_writable2(self): def test_ClientConnection_writable2(self):
# with a connecting connection, must not call parent's method
# with no error, just complete connection
em = Mock()
handler = Mock()
def getError(self):
return None
DoNothingConnector.getError = getError
bc = ClientConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413))
# check connector created and connection initialize
bc.connecting = True
self.assertNotEqual(bc.getConnector(), None)
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"]
self.assertTrue(bc.pending())
self.assertFalse(bc.aborted)
# call
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1)
bc.writable()
conn = bc.getConnector()
self.assertFalse(bc.connecting)
self.assertTrue(bc.pending())
self.assertFalse(bc.aborted)
self.assertEqual(''.join(bc.write_buf), "testdata")
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 2)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 0)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 2)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 0)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("send")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("shutdown")), 0)
self.assertEquals(len(conn.mockGetNamedCalls("close")), 0)
def test_ClientConnection_writable3(self):
# with a connecting connection, must not call parent's method # with a connecting connection, must not call parent's method
# with errors, close connection # with errors, close connection
em = Mock()
handler = Mock()
def getError(self): def getError(self):
return True return True
DoNothingConnector.getError = getError DoNothingConnector.getError = getError
bc = ClientConnection(em, handler, connector_handler=DoNothingConnector, bc = self._makeClientConnection()
addr=("127.0.0.7", 93413))
# check connector created and connection initialize # check connector created and connection initialize
bc.connecting = True bc.connecting = True
self.assertNotEqual(bc.getConnector(), None) self._checkWriteBuf(bc, '')
self.assertEqual(''.join(bc.write_buf), '')
bc.write_buf = ["testdata"] bc.write_buf = ["testdata"]
self.assertTrue(bc.pending()) self.assertTrue(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
# call # call
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1) self._checkConnectionCompleted(1)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded(1)
bc.writable() bc.writable()
self.assertEqual(bc.getConnector(), None)
self.assertTrue(bc.connecting) self.assertTrue(bc.connecting)
self.assertFalse(bc.pending()) self.assertFalse(bc.pending())
self.assertFalse(bc.aborted) self.assertFalse(bc.aborted)
self.assertEqual(''.join(bc.write_buf), "") # buffer flushed at closure self._checkWriteBuf(bc, '')
self.assertEquals(len(handler.mockGetNamedCalls("connectionClosed")), 0) self._checkConnectionClosed(0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1) self._checkConnectionCompleted(1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 1) self._checkConnectionFailed(1)
self.assertEquals(len(em.mockGetNamedCalls("unregister")), 1) self._checkUnregistered(1)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1) self._checkReaderAdded(1)
self.assertEquals(len(em.mockGetNamedCalls("removeWriter")), 1) self._checkWriterRemoved(1)
self.assertEquals(len(em.mockGetNamedCalls("removeReader")), 1) self._checkReaderRemoved(1)
def test_14_ServerConnection(self): def test_14_ServerConnection(self):
em = Mock() bc = self._makeServerConnection()
handler = Mock()
bc = ServerConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413))
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413))
self.assertEqual(len(em.mockGetNamedCalls("addReader")), 0)
self.assertEqual(bc.getConnector(), None)
self.assertEqual(''.join(bc.read_buf), '')
self.assertEqual(''.join(bc.write_buf), '')
self.assertEqual(bc.cur_id, 0)
self.assertEqual(bc.event_dict, {})
self.assertEqual(bc.aborted, False)
# test uuid
self.assertEqual(bc.uuid, None)
self.assertEqual(bc.getUUID(), None)
uuid = self.getNewUUID()
bc.setUUID(uuid)
self.assertEqual(bc.getUUID(), uuid)
# test next id
cur_id = bc.cur_id
next_id = bc._getNextId()
self.assertEqual(next_id, cur_id)
next_id = bc._getNextId()
self.assertTrue(next_id > cur_id)
# test overflow of next id
bc.cur_id = 0xffffffff
next_id = bc._getNextId()
self.assertEqual(next_id, 0xffffffff)
next_id = bc._getNextId()
self.assertEqual(next_id, 0)
# test abort
bc.abort()
self.assertEqual(bc.aborted, True)
self.assertTrue(bc.isServer())
def test_MTClientConnection1(self):
# same as ClientConnection, except definition of some lock
# create a good client connection
em = Mock()
handler = Mock()
dispatcher = Mock()
bc = MTClientConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413), dispatcher=dispatcher)
# check connector created and connection initialize
self.assertFalse(bc.connecting)
self.assertFalse(bc.isServer())
self.assertNotEqual(bc.getConnector(), None)
conn = bc.getConnector()
self.assertEquals(len(conn.mockGetNamedCalls("makeClientConnection")), 1)
call = conn.mockGetNamedCalls("makeClientConnection")[0]
data = call.getParam(0)
self.assertEqual(data, ("127.0.0.7", 93413))
# check call to handler
self.assertNotEqual(bc.getHandler(), None)
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0)
# check call to event manager
self.assertNotEqual(bc.getEventManager(), None)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 1)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 0)
def test_MTClientConnection2(self):
# raise connection in progress
makeClientConnection_org = DoNothingConnector.makeClientConnection
def makeClientConnection(self, *args, **kw):
raise ConnectorInProgressException
em = Mock()
handler = Mock()
dispatcher = Mock()
DoNothingConnector.makeClientConnection = makeClientConnection
try:
bc = MTClientConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413), dispatcher=dispatcher)
finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org
# check connector created and connection initialize
self.assertTrue(bc.connecting)
self.assertFalse(bc.isServer())
self.assertNotEqual(bc.getConnector(), None)
conn = bc.getConnector()
self.assertEquals(len(conn.mockGetNamedCalls("makeClientConnection")), 1)
call = conn.mockGetNamedCalls("makeClientConnection")[0]
data = call.getParam(0)
self.assertEqual(data, ("127.0.0.7", 93413))
# check call to handler
self.assertNotEqual(bc.getHandler(), None)
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 0)
# check call to event manager
self.assertNotEqual(bc.getEventManager(), None)
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 0)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 1)
def test_MTClientConnection3(self):
# raise another error, connection must fail
makeClientConnection_org = DoNothingConnector.makeClientConnection
def makeClientConnection(self, *args, **kw):
raise ConnectorException
em = Mock()
handler = Mock()
dispatcher = Mock()
DoNothingConnector.makeClientConnection = makeClientConnection
try:
self.assertRaises(ConnectorException, MTClientConnection, em, handler,
connector_handler=DoNothingConnector, addr=("127.0.0.7", 93413),
dispatcher=dispatcher)
finally:
DoNothingConnector.makeClientConnection = makeClientConnection_org
# the connection is not created
# check call to handler
self.assertEquals(len(handler.mockGetNamedCalls("connectionStarted")), 1)
self.assertEquals(len(handler.mockGetNamedCalls("connectionCompleted")), 0)
self.assertEquals(len(handler.mockGetNamedCalls("connectionFailed")), 1)
# check call to event manager
self.assertEquals(len(em.mockGetNamedCalls("addReader")), 0)
self.assertEquals(len(em.mockGetNamedCalls("addWriter")), 0)
# XXX check locking ?
def test_16_MTServerConnection(self):
em = Mock()
handler = Mock()
bc = MTServerConnection(em, handler, connector_handler=DoNothingConnector,
addr=("127.0.0.7", 93413))
self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413)) self.assertEqual(bc.getAddress(), ("127.0.0.7", 93413))
self.assertEqual(len(em.mockGetNamedCalls("addReader")), 0) self._checkReaderAdded(1)
self.assertEqual(bc.getConnector(), None) self._checkReadBuf(bc, '')
self.assertEqual(''.join(bc.read_buf), '') self._checkWriteBuf(bc, '')
self.assertEqual(''.join(bc.write_buf), '')
self.assertEqual(bc.cur_id, 0) self.assertEqual(bc.cur_id, 0)
self.assertEqual(bc.event_dict, {}) self.assertEqual(bc.event_dict, {})
self.assertEqual(bc.aborted, False) self.assertEqual(bc.aborted, False)
...@@ -1102,7 +772,6 @@ class ConnectionTests(NeoTestBase): ...@@ -1102,7 +772,6 @@ class ConnectionTests(NeoTestBase):
bc.setUUID(uuid) bc.setUUID(uuid)
self.assertEqual(bc.getUUID(), uuid) self.assertEqual(bc.getUUID(), uuid)
# test next id # test next id
bc._lock = Mock({'_is_owned': True})
cur_id = bc.cur_id cur_id = bc.cur_id
next_id = bc._getNextId() next_id = bc._getNextId()
self.assertEqual(next_id, cur_id) self.assertEqual(next_id, cur_id)
...@@ -1119,7 +788,6 @@ class ConnectionTests(NeoTestBase): ...@@ -1119,7 +788,6 @@ class ConnectionTests(NeoTestBase):
self.assertEqual(bc.aborted, True) self.assertEqual(bc.aborted, True)
self.assertTrue(bc.isServer()) self.assertTrue(bc.isServer())
# XXX check locking ???
class HandlerSwitcherTests(NeoTestBase): class HandlerSwitcherTests(NeoTestBase):
......
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