Пример #1
0
def test_init_app(app: Sanic, client: PublisherClient, mocker: MockFixture,
                  q: SQLiteAckQueue):
    # don't hit actual pubsub
    publish = mocker.patch.object(client.api, "publish")
    publish.return_value = PublishResponse(message_ids=["1"])

    # listener to create test conditions while sanic is running
    @app.listener("after_server_start")
    async def after_server_start(app, _):
        # stop Sanic
        app.stop()
        # queue message to be delivered on shutdown
        q.put(("topic", b"data", {}))
        q.put(("topic", b"data", {}))

    # set required configuration
    app.config.update(FLUSH_CONCURRENT_BYTES=1,
                      FLUSH_CONCURRENT_MESSAGES=1,
                      FLUSH_SLEEP_SECONDS=0)
    # configure sanic listeners to handle q in the background
    flush.init_app(app, client, q)
    # use a socket to bind to a random port and allow parallel testing
    sock = socket()
    sock.bind(("", 0))
    # start the app
    app.run(sock=sock)
    # make sure everything flushed cleanly
    assert q.size == 0
    assert q.unack_count() == 0
    # make sure publish was called the expected number of times
    assert publish.call_count == 2
Пример #2
0
 def api_publish(topic, messages):
     published.append(
         (topic, [(message.data, dict(message.attributes)) for message in messages])
     )
     return PublishResponse()
Пример #3
0
 def api_publish(topic, messages):
     published.append(
         (topic, [(message.data, dict(message.attributes)) for message in messages])
     )
     return PublishResponse(message_ids=[str(i) for i in range(len(messages))])