1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-05-02 04:23:19 +00:00
libsearpc/pysearpc/test_pysearpc.py

104 lines
2.4 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
import logging
2018-08-04 06:37:07 +00:00
import os
2011-04-08 12:58:15 +00:00
import sys
2018-08-04 06:12:50 +00:00
import unittest
2018-08-04 06:37:07 +00:00
from operator import add, mul
2018-08-04 06:12:50 +00:00
2018-08-04 06:37:07 +00:00
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2018-08-04 06:12:50 +00:00
sys.path.insert(0, '..')
from pysearpc import (
NamedPipeClient, NamedPipeServer, SearpcClient, SearpcError,
SearpcTransport, searpc_func, searpc_server
2018-08-04 06:12:50 +00:00
)
SVCNAME = 'test-service'
2018-08-04 06:12:50 +00:00
def init_server():
searpc_server.create_service(SVCNAME)
searpc_server.register_function(SVCNAME, add, 'add')
2018-08-04 06:37:07 +00:00
searpc_server.register_function(SVCNAME, mul, 'multi')
2018-08-04 06:12:50 +00:00
class DummyTransport(SearpcTransport):
def connect(self):
pass
2018-08-04 06:12:50 +00:00
def send(self, service, fcall_str):
return searpc_server.call_function(service, fcall_str)
2011-04-08 12:58:15 +00:00
class RpcMixin(object):
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:37:07 +00:00
@searpc_func("string", ["string", "int"])
def multi(self, x, y):
pass
class DummyRpcClient(SearpcClient, RpcMixin):
def __init__(self):
self.transport = DummyTransport()
def call_remote_func_sync(self, fcall_str):
return self.transport.send(SVCNAME, fcall_str)
class NamedPipeClientForTest(NamedPipeClient, RpcMixin):
pass
SOCKET_PATH = '/tmp/libsearpc-test.sock'
2018-08-04 06:12:50 +00:00
class SearpcTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
2018-08-04 06:12:50 +00:00
init_server()
cls.client = DummyRpcClient()
cls.named_pipe_server = NamedPipeServer(SOCKET_PATH)
cls.named_pipe_server.start()
cls.named_pipe_client = NamedPipeClientForTest(SOCKET_PATH, SVCNAME)
@classmethod
def tearDownClass(cls):
cls.named_pipe_client.stop()
cls.named_pipe_server.stop()
2018-08-04 06:12:50 +00:00
2018-08-04 06:37:07 +00:00
def test_normal_transport(self):
self.run_common(self.client)
2018-08-04 06:37:07 +00:00
# @unittest.skip('not implemented yet')
2018-08-04 06:37:07 +00:00
def test_pipe_transport(self):
self.run_common(self.named_pipe_client)
2018-08-04 06:37:07 +00:00
def run_common(self, client):
v = client.add(1, 2)
2018-08-04 06:12:50 +00:00
self.assertEqual(v, 3)
v = client.multi(1, 2)
2018-08-04 06:37:07 +00:00
self.assertEqual(v, 2)
v = client.multi('abc', 2)
2018-08-04 06:37:07 +00:00
self.assertEqual(v, 'abcabc')
def setup_logging(level=logging.INFO):
kw = {
# 'format': '[%(asctime)s][%(pathname)s]: %(message)s',
'format': '[%(asctime)s][%(module)s]: %(message)s',
'datefmt': '%m/%d/%Y %H:%M:%S',
'level': level,
'stream': sys.stdout
}
logging.basicConfig(**kw)
2018-08-04 06:12:50 +00:00
if __name__ == '__main__':
setup_logging()
2018-08-04 06:12:50 +00:00
unittest.main()