def test_semaphore(self): edge = APIEdge(MockApp(), self.get_settings()) api = edge.app.api edge.max_concurrent_calls = 1 in_first_method = Event() finish_first_method = Event() def first_method(): in_first_method.set() finish_first_method.wait() api.first_method = first_method in_second_method = Event() def second_method(): in_second_method.set() api.second_method = second_method gevent.spawn(edge.execute, Call("first_method")) in_first_method.wait() gevent.spawn(edge.execute, Call("second_method")) gevent.sleep(0) assert_logged("too many concurrent callers") assert not in_second_method.is_set() finish_first_method.set() in_second_method.wait() self.assert_edge_clean(edge)
def test_raise(self): self.set_messages([("raise", "42")]) try: self.client.call(Call("foo")) raise AssertionError("RemoteException not raised") except RemoteException, e: pass
def test_no_timeout_decorator(self): app = MockApp() edge = APIEdge(app, self.get_settings()) app.api.foo = edge.no_timeout(Mock()) edge.execute(Call("foo")) assert_equal(app.api.foo.call_count, 1) self.assert_edge_clean(edge)
def test_debug_call(self): edge = APIEdge(MockApp(), self.get_settings()) debug_api = edge.app.debug_api call = Call("debug.foo") edge.execute(call) assert_equal(debug_api.foo.call_count, 1) assert_equal(edge._call_semaphore, None)
def test_simple_call(self): self.set_messages([("return", 42)]) result = self.client.call(Call("foo", (1, ), {"bar": 2})) assert_equal(result, 42) assert_equal(self.cxn.send_message.call_args_list, [((("call", ("foo", (1, ), { "bar": 2 })), ), {})]) assert not self.cxn.disconnect.called assert self.release_called
def test_error_call(self): app = DirtApp("test_normal_call", self.get_settings(), []) edge = APIEdge(app, app.settings) call = Call("debug.ping", (), {"raise_error": True}, {}) try: edge.execute(call) raise AssertionError("exception not raised") except Exception as e: if not str(e).startswith("pong:"): raise
def list_methods(self, prefix=""): """ Returns a list of the methods available on the API. """ handlers = [ handler_prefix + "." for handler_prefix in self.app.api_handlers if (handler_prefix and handler_prefix.startswith(prefix) and handler_prefix != prefix) ] dummy_call = Call(prefix + "._list_methods_dummy_method") handler, _ = self.edge.get_call_handler(dummy_call) methods = self._list_methods(handler) return handlers + methods
def test_timeout(self): edge = APIEdge(MockApp(), self.get_settings()) edge.call_timeout = 0.0 edge.app.api.foo = lambda: gevent.sleep(0.1) try: edge.execute(Call("foo")) raise AssertionError("timeout not raised!") except gevent.Timeout: pass self.assert_edge_clean(edge)
def test_normal_call(self): edge = APIEdge(MockApp(), self.get_settings()) api = edge.app.api call = Call("foo") assert_equal(edge.get_call_handler(call), (api, "foo")) assert_equal(edge.get_call_handler_method(call, api, "foo"), api.foo) edge.execute(call) assert_equal(api.foo.call_count, 1) self.assert_edge_clean(edge)
def _handle_one_message(self): """ Handles one stateless message from a client. Stateful messages should be handled by other sub-functions (eg, ``_handle_call``). """ type, data = self.cxn.recv_message() if type.startswith("call"): if len(data) != 3: message = (type, data) raise MessageError.invalid(message, "incorrect number of args") flags = { "want_response": type == "call", } call = Call(data[0], data[1], data[2], flags, self.client) self._handle_call(call) return False raise MessageError.bad_type(type)
def _async_task(self, initial_event): ### TODO: Use ZeroRPC middleware functionality protocol_v1 = initial_event.header.get('v', 1) < 2 channel = self._multiplexer.channel(initial_event) hbchan = HeartBeatOnChannel(channel, freq=self._heartbeat_freq, passive=protocol_v1) bufchan = BufferedChannel(hbchan) event = bufchan.recv() try: self._context.middleware_load_task_context(event.header) # TODO: support non Req/Rep patterns, such as pubsub, pushpull call = Call(event.name, event.args) # TODO: Adam: add ``peer=...`` here result = self.execute_call(call) bufchan.emit('OK', (result,), self._context.middleware_get_task_context()) except LostRemote: self._print_traceback(protocol_v1) except Exception: exception_info = self._print_traceback(protocol_v1) bufchan.emit('ERR', exception_info, self._context.middleware_get_task_context()) finally: bufchan.close()
def test_returns_stop(self): self.set_messages([("stop", )]) result = self.client.call(Call("foo")) assert_equal(list(result), []) assert not self.cxn.disconnect.called assert self.release_called
def test_no_want_response(self): self.client.call(Call("foo", flags={"want_response": False})) assert_equal(self.cxn.recv_message.call_count, 0) assert not self.cxn.disconnect.called assert self.release_called
def test_normal_call(self): app = DirtApp("test_normal_call", self.get_settings(), []) edge = APIEdge(app, app.settings) call = Call("debug.status", (), {}, {}) result = edge.execute(call) assert_contains(result, "uptime")