Source

JRPC / jrpc / tests / test_client_server.py

Full commit
from multiprocessing import Process
import time

from jrpc.client import JRPCClient, ServerError
from jrpc.server import serve

# the class whose methods we're going to expose
class Handler(object):
	def sum(self, a, b):
		return a + b

def test_client_server():
	# start server in separate process
	serverproc = Process(target=serve, args=(9000, Handler()))
	serverproc.start()

	# Give the server time to start up
	time.sleep(1)

	# create cleint and call method
	c = JRPCClient('localhost', 9000)
	sum = c.sum(1, 2)

	assert sum == 3, 'Expected 1 + 2 = 3, but got %s' % sum

	try:
		c.sum(1, 2, 3)
		raise Assertion("Expected ServerError")
	except ServerError:
		pass

	try:
		c.somefunctionthatdoesnotexist()
		raise Assertion("Expected ServerError")
	except ServerError:
		pass

	# terminate server process
	serverproc.terminate()