File: test_background_tasks.py

package info (click to toggle)
quart 0.20.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,892 kB
  • sloc: python: 8,644; makefile: 42; sh: 17; sql: 6
file content (74 lines) | stat: -rw-r--r-- 1,544 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import annotations

import asyncio
import time

from quart import current_app
from quart import Quart


async def test_background_task() -> None:
    app = Quart(__name__)
    app.config["DATA"] = "data"

    data = None

    async def background() -> None:
        nonlocal data
        await asyncio.sleep(0.5)
        data = current_app.config["DATA"]

    @app.route("/")
    async def index() -> str:
        app.add_background_task(background)
        return ""

    async with app.test_app():
        test_client = app.test_client()
        await test_client.get("/")

    assert data == "data"


async def test_lifespan_background_task() -> None:
    app = Quart(__name__)
    app.config["DATA"] = "data"

    data = None

    async def background() -> None:
        nonlocal data
        await asyncio.sleep(0.5)
        data = current_app.config["DATA"]

    @app.before_serving
    async def startup() -> None:
        app.add_background_task(background)

    async with app.test_app():
        pass

    assert data == "data"


async def test_sync_background_task() -> None:
    app = Quart(__name__)
    app.config["DATA"] = "data"

    data = None

    def background() -> None:
        nonlocal data
        time.sleep(0.5)
        data = current_app.config["DATA"]

    @app.route("/")
    async def index() -> str:
        app.add_background_task(background)
        return ""

    async with app.test_app():
        test_client = app.test_client()
        await test_client.get("/")

    assert data == "data"