Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example harness for testing an XMLRPC client/server relationship
from threading import Thread
from unittest import TestCase
from xmlrpclib import ServerProxy
from SimpleXMLRPCServer import SimpleXMLRPCServer
class Dummy(object):
def __init__(self):
self.data = list(xrange(0,10))
def get(self, index):
return self.data[index]
def steal(self, index):
value = self.data[index]
self.data[index] = None
return value
class XMLRPCHarness(object):
"""
A setUp/tearDown harness that will provide an XMLRPC Server for us
to test against.
"""
HOST = "localhost"
PORT = 8999
def __init__(self, *args, **kwds):
super(XMLRPCHarness, self).__init__(*args, **kwds)
self.server = None
self.thread = None
self.dummy = None
def setUp(self):
assert(self.server is None)
assert(self.thread is None)
self.dummy = Dummy()
self.server = SimpleXMLRPCServer((self.HOST, self.PORT),
logRequests=False)
self.server.register_function(self.dummy.get, "get")
self.server.register_function(self.dummy.steal, "steal")
self.server.register_multicall_functions()
self.thread = Thread(target=self.server.serve_forever,
kwargs={"poll_interval":0.2})
self.thread.start()
def tearDown(self):
assert(self.server is not None)
assert(self.thread is not None)
self.server.shutdown()
self.server.socket.close()
self.server = None
self.dummy = None
self.thread.join()
self.thread = None
def get_client(self):
assert(self.server is not None)
assert(self.thread is not None)
return ServerProxy("http://%s:%i" % (self.HOST, self.PORT))
class TestSomething(XMLRPCHarness, TestCase):
def test_my_thing(self):
connect = self.get_client()
self.assertEqual(connect.get(1), 1)
# steal should be destructive, check that our dummy changed
self.assertEqual(connect.steal(1), 1)
self.assertEqual(self.dummy.get(1), None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.