SF.net SVN: fclient: [18] trunk/fclient/fclient_lib/fcp
Status: Pre-Alpha
Brought to you by:
jurner
From: <jU...@us...> - 2007-10-28 09:44:01
|
Revision: 18 http://fclient.svn.sourceforge.net/fclient/?rev=18&view=rev Author: jUrner Date: 2007-10-28 02:44:05 -0700 (Sun, 28 Oct 2007) Log Message: ----------- added some basic unittests for message objects Added Paths: ----------- trunk/fclient/fclient_lib/fcp/test_fcp2_0/ trunk/fclient/fclient_lib/fcp/test_fcp2_0/__init__.py trunk/fclient/fclient_lib/fcp/test_fcp2_0/oo1.py trunk/fclient/fclient_lib/fcp/test_fcp2_0/test_message_object.py Added: trunk/fclient/fclient_lib/fcp/test_fcp2_0/__init__.py =================================================================== --- trunk/fclient/fclient_lib/fcp/test_fcp2_0/__init__.py (rev 0) +++ trunk/fclient/fclient_lib/fcp/test_fcp2_0/__init__.py 2007-10-28 09:44:05 UTC (rev 18) @@ -0,0 +1 @@ + Added: trunk/fclient/fclient_lib/fcp/test_fcp2_0/oo1.py =================================================================== --- trunk/fclient/fclient_lib/fcp/test_fcp2_0/oo1.py (rev 0) +++ trunk/fclient/fclient_lib/fcp/test_fcp2_0/oo1.py 2007-10-28 09:44:05 UTC (rev 18) @@ -0,0 +1,5 @@ + +import socket + + +raise socket.error('foo') \ No newline at end of file Added: trunk/fclient/fclient_lib/fcp/test_fcp2_0/test_message_object.py =================================================================== --- trunk/fclient/fclient_lib/fcp/test_fcp2_0/test_message_object.py (rev 0) +++ trunk/fclient/fclient_lib/fcp/test_fcp2_0/test_message_object.py 2007-10-28 09:44:05 UTC (rev 18) @@ -0,0 +1,177 @@ +"""Unittests for Message object""" + +import os, sys +import socket +import unittest + +#--> rel import hack +def parentdir(n, fpath): + fpath = os.path.abspath(fpath) + for i in xrange(n): + fpath = os.path.dirname(fpath) + return fpath +sys.path.insert(0, parentdir(2, __file__)) + + +from fcp2_0 import Message + +sys.path.pop(0) +del parentdir +#<-- rel import hack + +#******************************************************************** +# +#******************************************************************** +class DummySocket(object): + """Dummy socket for testing""" + + def __init__(self, bytes, closed=False, error=False): + self.bytes = bytes + self.closed = closed + self.error = error + + def recv(self, n): + if self.closed: + return '' + elif self.error: + raise socket.error(0, 'I am dead!') + self.bytes, bytes = self.bytes[n:], self.bytes[:n] + if bytes: + return bytes + raise socket.timeout(0) + + def sendall(self, bytes): + self.bytes += bytes + +#**************************************************************************************** +# +#**************************************************************************************** +class TestMessageObject(unittest.TestCase): + + + def testFromSocket(self): + """Reads a simple message from socket""" + s = DummySocket('NodeHello\nfoo=bar\nEndMessage\n') + msg = Message.fromSocket(s) + + self.failUnless(msg.name == Message.NodeHello) + self.failUnless(len(msg.params) == 1) + self.failUnless(msg.get('foo', None) == 'bar') + + + def testFromSocketClosed(self): + """Reads a message from a closed socket""" + s = DummySocket('NodeHello\nfoo=bar\nEndMessage\n', closed=True) + msg = Message.fromSocket(s) + + self.failUnless(msg.name == Message.ClientSocketDied) + self.failUnless(msg.get('Exception', None) == socket.error) + + + + def testFromSocketError(self): + """Reads a message from a socket that raises emidiately""" + s = DummySocket('NodeHello\nfoo=bar\nEndMessage\n', error=True) + msg = Message.fromSocket(s) + + self.failUnless(msg.name == Message.ClientSocketDied) + self.failUnless(msg.get('Exception', None) == socket.error) + + + def testFromSocketTimeout(self): + """Reads a message from a socket with no bytes available""" + s = DummySocket('') + msg = Message.fromSocket(s) + + self.failUnless(msg.name == Message.ClientSocketTimeout) + + + def testDataFromSocket(self): + """Reads a message with associated data member from a socket""" + s = DummySocket('AllData\nDataLength=8\nData\n\nABC\nDEF') + msg = Message.fromSocket(s) + + self.failUnless(msg.name == Message.AllData) + self.failUnless(len(msg.params) == 1) + self.failUnless(msg.get('DataLength', None) == '8') + self.failUnless(msg.data == '\nABC\nDEF') + + + def testSend(self): + """Sends a simple message""" + s = DummySocket('') + msg = Message( + Message.ClientHello, + foo='bar' + ) + msg.send(s) + self.failUnless(s.bytes == 'ClientHello\nfoo=bar\nEndMessage\n') + + + def testSendData(self): + """Sends a message containing a data ,member""" + s = DummySocket('') + msg = Message( + Message.AllData, + DataLength='5', + data='ABCDE' + ) + msg.send(s) + self.failUnless(s.bytes == 'AllData\nDataLength=5\nData\nABCDE') + + # test error checking of data member + msg = Message( + Message.AllData, + ##DataLength='1', # no data length specified + data='A' + ) + self.assertRaises(AssertionError, msg.send, s) + msg = Message( + Message.AllData, + DataLength='1', + data='A' * 100 + ) + self.assertRaises(AssertionError, msg.send, s) + msg = Message( + Message.AllData, + DataLength='100', + data='A' + ) + self.assertRaises(AssertionError, msg.send, s) + msg = Message( + Message.AllData, + DataLength='X', + data='A' + ) + self.assertRaises(AssertionError, msg.send, s) + + + + +#********************************************************************************* +# +#********************************************************************************* +def suite(): + return unittest.TestLoader().loadTestsFromTestCase(TestMessageObject) + +def test(): + unittest.main() + +if __name__ == '__main__': + test() + + + + + + + + + + + + + + + + \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |