def __init__(self, conf: Any): _conf = ParamDict(conf) self._conf = ParamDict({**FUGUE_DEFAULT_CONF, **_conf}) self._rpc_server = make_rpc_server(self.conf) self._engine_start_lock = RLock() self._engine_start = 0 self._sql_engine: Optional[SQLEngine] = None
def test_flask_service(): # fugue.rpc.flask.FlaskRPCServer conf = ParamDict({ "fugue.rpc.server": "fugue.rpc.flask.FlaskRPCServer", "fugue.rpc.flask_server.host": "127.0.0.1", "fugue.rpc.flask_server.port": "1234", "fugue.rpc.flask_server.timeout": "2 sec", }) def k(value: str) -> str: return value + "x" def kk(a: int, b: int) -> int: return a + b def kkk(f: callable, a: int) -> int: return f(a) with make_rpc_server(conf).start() as server: assert "1234" == server.conf["fugue.rpc.flask_server.port"] with server.start(): # recursive start will take no effect client1 = cloudpickle.loads( cloudpickle.dumps(server.make_client(k))) assert "dddx" == client1("ddd") client2 = cloudpickle.loads(cloudpickle.dumps(server.make_client(kk))) assert 3 == client2(1, 2) assert "dddx" == client1("ddd") client3 = cloudpickle.loads(cloudpickle.dumps(server.make_client(kkk))) assert 3 == client3(lambda x: x + 1, 2) assert 3 == client2(1, 2) server.stop() # extra stop in the end will take no effect
def test_server_handlers(): func = lambda x: x + "aa" class _Dict(RPCFunc): def __init__(self, obj): super().__init__(obj) self.start_called = 0 self.stop_called = 0 def start_handler(self): self.start_called += 1 def stop_handler(self): self.stop_called += 1 server = make_rpc_server({}) server.start() d1 = _Dict(func) c1 = server.make_client(d1) assert "xaa" == c1("x") assert 1 == d1.start_called assert 0 == d1.stop_called server.stop() assert 1 == d1.start_called assert 1 == d1.stop_called with server.start(): d2 = _Dict(func) c1 = server.make_client(d2) server.start() assert "xaa" == c1("x") assert 1 == d2.start_called assert 0 == d2.stop_called assert 1 == d1.start_called assert 1 == d1.stop_called server.stop() assert 1 == d2.start_called assert 1 == d2.stop_called assert 1 == d1.start_called assert 1 == d1.stop_called
def test_default_server(): def k(value: str) -> str: return value + "x" def kk(value: str) -> str: return value + "xx" conf = {"x": "y"} with make_rpc_server(conf).start() as server: assert "y" == server.conf["x"] with server.start(): # recursive start will take no effect client = server.make_client(k) assert "dddx" == client("ddd") client = server.make_client(kk) assert "dddxx" == client("ddd") server.stop() # extra stop in the end will take no effect with raises(pickle.PicklingError): pickle.dumps(client) with raises(pickle.PicklingError): pickle.dumps(server)