def __init__(self, context: Context, service: str) -> None: self.service = service self.url = context.url self.context = context self.registry = context.get_registry() self.endpoints = context.get_endpoints(self.registry, service) self.ha = context.get_ha() self.lb = context.get_lb(list(self.endpoints.values())) self.filter = context.get_filter(self) self.available = True self.closed = False self.watch()
def __init__(self, context: Context) -> None: self.name = context.url.get_param("name") self.url = context.url self.context = context self.registry = context.get_registry() self.handler: Optional[DogeRPCServer] = None self.limit = context.url.get_param("limitConn", "default")
def __init__(self, config_file: str) -> None: u"""Cluster 抽象""" self.config_file = config_file self.config = Config(config_file) self.context = Context(self.config.parse_refer(), self.config.parse_registry()) self.clients: Dict[str, Client] = {} self.sem = BoundedSemaphore(1)
def server(): server = StreamServer( ("127.0.0.1", 4399), DogeRPCServer( Context(URL(None, None, None, {"name": ""}), URL(None, None, None, {})), SumServer, ), ) g = gevent.spawn(server.serve_forever) gevent.sleep(0.1) yield server g.kill()
def test_context(self, server): url = URL( "127.0.0.1", 4399, params={ "name": "test", "node": "n1", "haStrategy": "backupRequestHA", "loadBalance": "RandomLB", }, ) rurl = URL("127.0.0.1", 4399, params={"protocol": "direct"}) context = Context(url, rurl) c = Client(context, "test") self.c = c assert c.call("sum", 1, 2) == 3 assert c.call("sum", 1, 2) == 3 assert c.call("sum", 1, 2) == 3
# coding: utf-8 import gevent import pytest from gevent import sleep from mprpc import RPCClient from doge.common.context import Context from doge.common.url import URL from doge.rpc.server import Server url = URL("127.0.0.1", 4399, params={"name": "test", "node": "n1"}) rurl = URL("127.0.0.1", 2379, params={"ttl": 10}) context = Context(url, rurl) @pytest.fixture(scope="function") def server(): s = Server(context) yield s s.registry.deregister(s.name, url) s.registry.destroy() class TestServer: def teardown_method(self, method): self.g.kill() def test_server(self, server): class Sum: def sum(self, x, y):
def new_server(config_file: str) -> Server: """从配置文件生成server""" config = Config(config_file) context = Context(config.parse_service(), config.parse_registry()) return Server(context)
def __init__(self, context: Context, cls: Type) -> None: super().__init__() self._name = context.url.get_param("name") self._filter = context.get_filter(self) self._methods = cls()