コード例 #1
0
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
コード例 #2
0
ファイル: __init__.py プロジェクト: aisk/ngru
 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
コード例 #3
0
ファイル: server.py プロジェクト: anniyanvr/demo-scene
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)
コード例 #4
0
ファイル: test_app.py プロジェクト: Extintor/kafkaesk
    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)
コード例 #5
0
ファイル: async_.py プロジェクト: MoshonkaKita/Golovastik
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)
コード例 #6
0
ファイル: async_.py プロジェクト: ManHammer/home-assistant
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)
コード例 #7
0
ファイル: __init__.py プロジェクト: lindycoder/walletplanner
def awaitable(result):
    future = Future()
    future.set_result(result)
    return future
コード例 #8
0
 def on_done(status: Future, task: Task) -> None:
     status.set_result("cancelled" if task.cancelled() else "done")