def make_awaitable(result: Any) -> Awaitable[Any]: """ Makes an awaitable, suitable for mocking an `async` function. This uses Futures as they can be awaited multiple times so can be returned to multiple callers. """ future = Future() future.set_result(result) return future
async def send(data): if data['type'] == 'http.response.start': status_code = data['status'] lib.evhttp_send_reply_start(req, status_code, http_messages.get(status_code, b'Unknown')) if data['type'] == 'http.response.body': body = data['body'] buf = lib.evbuffer_new() lib.evbuffer_add_printf(buf, body) lib.evhttp_send_reply_chunk(req, buf) lib.evhttp_send_reply_end(req) lib.evbuffer_free(buf) future = Future() future.set_result(None) return future
async def main(): shutdown_flag = Future() clients = set() lock = threading.Lock() get_event_loop().run_in_executor(None, run_consumer, shutdown_flag, clients, lock) print("Starting Websocket Server.") try: async with websockets.serve(partial(handle_connection, clients, lock), "localhost", 8080): await Future() finally: shutdown_flag.set_result(True)
def test_publish_callback(self, metrics): fut = Future() fut.set_result(record_factory()) published_callback("topic", time.time() - 1, fut) metrics["PUBLISHED_MESSAGES"].labels.assert_called_with( stream_id="topic", partition=0, error="none" ) metrics["PUBLISHED_MESSAGES"].labels().inc() metrics["PRODUCER_TOPIC_OFFSET"].labels.assert_called_with(stream_id="topic", partition=0) metrics["PRODUCER_TOPIC_OFFSET"].labels().set.assert_called_with(0) metrics["PUBLISHED_MESSAGES_TIME"].labels.assert_called_with(stream_id="topic") assert metrics["PUBLISHED_MESSAGES_TIME"].labels().observe.mock_calls[0].args[ 0 ] == pytest.approx(1, 0.1)
def _set_result_unless_cancelled(fut: Future, result: Any) -> None: """Set the result only if the Future was not cancelled.""" if fut.cancelled(): return fut.set_result(result)
def _set_result_unless_cancelled(fut: Future, result: Any) -> None: """Set the result only if the Future was not cancelled.""" if fut.cancelled(): return fut.set_result(result)
def awaitable(result): future = Future() future.set_result(result) return future
def on_done(status: Future, task: Task) -> None: status.set_result("cancelled" if task.cancelled() else "done")