Changeset 7:39e2245da71c in mailjam


Ignore:
Timestamp:
May 18, 2012, 5:54:56 PM (10 years ago)
Author:
Francisco de Borja Lopez Rio <borja@…>
Branch:
default
Phase:
public
Message:

Added some fixes to the XMLRPC server code

Added more tests for the XMLRPC server

Location:
postman
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • postman/daemon.py

    r6 r7  
    152152
    153153    def _listMethods(self):
    154         return list_public_methods(self)
     154        public_methods = []
     155        public_methods += ['lists.'+i for i in dir(MailingListXMLRPC) if '_' not in i]
     156        public_methods += ['members.'+i for i in dir(MemberXMLRPC) if '_' not in i]
     157        return public_methods
    155158
    156159    def _methodHelp(self, method):
     
    165168    def add(self, info={}):
    166169        self.postman.add_mailing_list(info)
     170    def addresses(self):
     171        return self.postman.mailings_addresses
    167172
    168173
     
    185190        self.configfile = configfile
    186191
     192        # FIXME: These should be loaded from a config file
     193        self.address='localhost'
     194        self.port = 9000
     195       
    187196        self.logfile = os.path.join(os.path.dirname(__file__), 'server.log')
    188197        logging.basicConfig(filename=self.logfile, level=logging.DEBUG)
     
    196205        of SimpleXMLRPCServer in that attribute. If there is already a server
    197206        initialized there, simply return True
    198         """
    199         address='localhost'
    200         port = 9000       
     207        """           
    201208        if not self.server:
    202             msg = 'Creating XMLRPC server object on {}:{}'.format(address,port)
     209            msg = 'Creating XMLRPC server object on {}:{}'.format(self.address,
     210                                                                  self.port)
    203211            logging.info(msg)
    204             self.server = SimpleXMLRPCServer((address, port), allow_none=True,
    205                                              logRequests=True)
     212            self.server = SimpleXMLRPCServer((self.address, self.port),
     213                                             allow_none=True,
     214                                             logRequests=False)
    206215            self.server.register_introspection_functions()
    207216        return True
     
    225234        self.server.register_instance(root, allow_dotted_names=True)
    226235        self.ready_to_serve = True
     236        return self.ready_to_serve
    227237           
    228238    def run(self):
     
    234244        if not self.ready_to_serve:
    235245            self.add_methods()
    236         msg = 'Starting XMLRPC server on {}:{}'.format(address,port)
     246        msg = 'Starting XMLRPC server on {}:{}'.format(self.address,
     247                                                       self.port)
    237248        logging.info(msg)
    238         self.server.serve_forever()
     249        try:
     250            self.server.serve_forever()
     251        except KeyboardInterrupt:
     252            msg = 'Stopping server'
     253            logging.info(msg)
  • postman/tests/daemon.py

    r6 r7  
    11# -*- coding: utf-8 -*-
    22
    3 import os, xmlrpclib
     3import os, multiprocessing, xmlrpclib
     4from SimpleXMLRPCServer import SimpleXMLRPCServer
    45from unittest import TestCase
    5 from postman.daemon import Postman
     6
     7from postman.daemon import Postman, PostmanXMLRPC, PostmanDaemon
    68from postman.models import Member, MailingList
    79from postman.storage import JsonStorage as Storage
     
    126128    """
    127129    postman.daemon.PostmanDaemon tests.
    128    
     130
     131    Remember to call the .clear() method of postman after each test, so
     132    the temp storage files are deleted
    129133    """
    130134    def setUp(self):
     
    134138                                        members={}, config=config)
    135139        self.member =  Member('test@example.com')
    136         # FIXME: Hardcoded url here, should be picked from a config file
     140
     141    def test___init__(self):
     142        daemon = PostmanDaemon()
     143        self.assertIsInstance(daemon, PostmanDaemon)
     144        self.assertFalse(daemon.ready_to_serve)
     145        # FIXME: More tests should be added here once the configuration
     146        # file feature is added       
     147
     148    def test_create_server(self):
     149        daemon = PostmanDaemon()
     150        daemon.port = 9001
     151        self.assertTrue(daemon.create_server())
     152        self.assertIsInstance(daemon.server, SimpleXMLRPCServer)
     153        self.assertFalse(daemon.ready_to_serve)
    137154       
    138         self.link = xmlrpclib.ServerProxy('http://localhost:9000')
     155    def test_add_methods(self):
     156        daemon = PostmanDaemon()
     157        daemon.port = 9002
     158        self.assertTrue(daemon.add_methods())
     159        self.assertTrue(daemon.ready_to_serve)
     160
     161        daemon = PostmanDaemon()
     162        daemon.port = 9003       
     163        daemon.create_server()
     164        self.assertTrue(daemon.add_methods())
     165        self.assertTrue(daemon.ready_to_serve)
     166       
     167    def test_run(self):
     168        daemon = PostmanDaemon()
     169        daemon.port = 9004
     170        # start the daemon in another process, so we can start communicating
     171        # with itjobs = []
     172        p = multiprocessing.Process(target=daemon.run)
     173        p.start()
     174       
     175        # FIXME: Hardcoded url here, should be picked from a config file       
     176        client = xmlrpclib.ServerProxy('http://localhost:9004')
     177
     178        # Check that we can perform an XMLRPC call and that the list of
     179        # available public methods contains the list of methods we have
     180        # defined in our base XMLRPC class
     181        set_class_methods = set(PostmanXMLRPC()._listMethods())
     182        set_xmlrpc_methods = set(client.system.listMethods())
     183        self.assertTrue(set_class_methods.issubset(set_xmlrpc_methods))
     184       
     185        # Stop the server
     186        p.terminate()
     187       
Note: See TracChangeset for help on using the changeset viewer.