def testSendJob(): print '=' * 60 print '-' * 20, 'testSendJob', '-'* 20 manager = ConnectionManager() processor = Processor(4) protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 newSock.status.addStatus(CONST.STATUS_WF) connection.genJobs() assert processor.list[-1].__doc__ == "Protocol.handleConnected" connection.send("test", 5) connection.genJobs() assert processor.list[-1].__doc__ == "AsynClientSocket.sendImpl" assert connection.status.has(CONST.STATUS_C) print newSock.dump() print processor.dump() print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testConnectFailed(): print '=' * 60 print '-' * 20, 'testConnectFailed', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 print "connect failed immediately." connection.connect("localhost", 12345) sleepTime = 5 while not connection.status.has(CONST.STATUS_E) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 connection.status.dump() print "\n\nconnect failed later." connection.connect("202.38.193.244", 12345) sleepTime = 5 while not connection.status.has(CONST.STATUS_E) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 connection.status.dump() newSock.status.addStatus(CONST.STATUS_UD) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testSend(testId): print '=' * 60 print '-' * 20, 'testSend(', testId, ')', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) connection.connect(host, port) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 timeout = 0 while not connection.status.has(CONST.STATUS_UD) and timeout < testTimeout: time.sleep(1) timeout += 1 if timeout >= testTimeout: pdb.set_trace() manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testAddSocket(): print '=' * 60 print '-' * 20, 'testAddSocket', '-'* 20 manager = ConnectionManager() processor = Processor(4) protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 newSock.status.addStatus(CONST.STATUS_UD) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testSend(): print '=' * 60 print '-' * 20, 'testSend', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 connection.connect(host, port) while not connection.status.has(CONST.STATUS_C): time.sleep(1) while not connection.status.has(CONST.STATUS_UD): time.sleep(1) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60
def testConnectSuccess(): print '=' * 60 print '-' * 20, 'testConnectSuccess', '-'* 20 protocol = Protocol() newSock = AsynClientSocket() connection = SocketConnection(newSock, protocol, processor) manager.addConnection(newSock.getFileNo(), connection) assert len(manager) == 1 connection.connect(host, port) sleepTime = 5 while not connection.status.has(CONST.STATUS_C) and sleepTime > 0: time.sleep(1) sleepTime = sleepTime - 1 assert sleepTime > 0 newSock.status.addStatus(CONST.STATUS_UD) manager.clean() assert len(manager) == 0 print '-' * 20, 'test done', '-' * 20 print '=' * 60