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
|
2018-08-09 09:34:29 +00:00
|
|
|
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 (
|
2018-08-09 09:34:29 +00:00
|
|
|
NamedPipeClient, NamedPipeServer, SearpcClient, SearpcError,
|
|
|
|
SearpcTransport, searpc_func, searpc_server
|
2018-08-04 06:12:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
SVCNAME = 'test-service'
|
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
|
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):
|
2018-08-09 09:34:29 +00:00
|
|
|
def connect(self):
|
|
|
|
pass
|
2018-08-04 06:12:50 +00:00
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
def send(self, service, fcall_str):
|
|
|
|
return searpc_server.call_function(service, fcall_str)
|
2011-04-08 12:58:15 +00:00
|
|
|
|
2018-08-09 09:34:29 +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
|
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
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):
|
2018-08-09 09:34:29 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2018-08-04 06:12:50 +00:00
|
|
|
init_server()
|
2018-08-09 09:34:29 +00:00
|
|
|
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):
|
2018-08-09 09:34:29 +00:00
|
|
|
self.run_common(self.client)
|
2018-08-04 06:37:07 +00:00
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
# @unittest.skip('not implemented yet')
|
2018-08-04 06:37:07 +00:00
|
|
|
def test_pipe_transport(self):
|
2018-08-09 09:34:29 +00:00
|
|
|
self.run_common(self.named_pipe_client)
|
2018-08-04 06:37:07 +00:00
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
def run_common(self, client):
|
|
|
|
v = client.add(1, 2)
|
2018-08-04 06:12:50 +00:00
|
|
|
self.assertEqual(v, 3)
|
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
v = client.multi(1, 2)
|
2018-08-04 06:37:07 +00:00
|
|
|
self.assertEqual(v, 2)
|
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
v = client.multi('abc', 2)
|
2018-08-04 06:37:07 +00:00
|
|
|
self.assertEqual(v, 'abcabc')
|
|
|
|
|
2018-08-09 09:34:29 +00:00
|
|
|
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__':
|
2018-08-09 09:34:29 +00:00
|
|
|
setup_logging()
|
2018-08-04 06:12:50 +00:00
|
|
|
unittest.main()
|