1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-06-20 02:41:54 +00:00
libsearpc/pysearpc/test_pysearpc.py

53 lines
1.1 KiB
Python
Raw Normal View History

2018-08-04 06:12:50 +00:00
#!/usr/bin/env python
#coding: UTF-8
2011-04-08 12:58:15 +00:00
2018-08-04 06:12:50 +00:00
import json
2011-04-08 12:58:15 +00:00
import sys
2018-08-04 06:12:50 +00:00
import unittest
from operator import add
sys.path.insert(0, '..')
from pysearpc import (
SearpcClient, SearpcError, SearpcTransport, searpc_func, searpc_server
)
SVCNAME = 'test-service'
def multi(a, b):
return a * b
def init_server():
searpc_server.create_service(SVCNAME)
searpc_server.register_function(SVCNAME, add, 'add')
searpc_server.register_function(SVCNAME, multi, 'multi')
class DummyTransport(SearpcTransport):
def send(self, fcall_str):
return searpc_server.call_function(SVCNAME, fcall_str)
2011-04-08 12:58:15 +00:00
class SampleRpcClient(SearpcClient):
2018-08-04 06:12:50 +00:00
def __init__(self):
self.transport = DummyTransport()
2011-04-08 12:58:15 +00:00
def call_remote_func_sync(self, fcall_str):
2018-08-04 06:12:50 +00:00
return self.transport.send(fcall_str)
2011-04-08 12:58:15 +00:00
2018-08-04 06:12:50 +00:00
@searpc_func("int", ["int", "int"])
def add(self, x, y):
2011-04-08 12:58:15 +00:00
pass
2018-08-04 06:12:50 +00:00
class SearpcTest(unittest.TestCase):
def setUp(self):
init_server()
self.client = SampleRpcClient()
def testNormalTransport(self):
v = self.client.add(1, 2)
self.assertEqual(v, 3)
if __name__ == '__main__':
unittest.main()