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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
import asyncio
from collections import Counter
import time
import pytest
from falcon import testing
from falcon.asgi import App
@pytest.mark.slow
def test_multiple():
class SomeResource:
def __init__(self):
self.counter = Counter()
async def on_get(self, req, resp):
async def background_job_async():
self.counter['backround:on_get:async'] += 1
def background_job_sync():
self.counter['backround:on_get:sync'] += 20
with pytest.raises(TypeError):
resp.schedule(background_job_sync)
resp.schedule_sync(background_job_sync)
resp.schedule(background_job_async)
resp.schedule_sync(background_job_sync)
resp.schedule(background_job_async)
async def on_post(self, req, resp):
async def background_job_async():
self.counter['backround:on_get:async'] += 1000
def background_job_sync():
self.counter['backround:on_get:sync'] += 2000
resp.schedule(background_job_async)
resp.schedule(background_job_async)
resp.schedule_sync(background_job_sync)
resp.schedule_sync(background_job_sync)
async def on_put(self, req, resp):
async def background_job_async():
self.counter['backround:on_get:async'] += 1000
c = background_job_async()
try:
resp.schedule(c)
finally:
await c
resource = SomeResource()
app = App()
app.add_route('/', resource)
client = testing.TestClient(app)
client.simulate_get()
client.simulate_post()
time.sleep(0.5)
assert resource.counter['backround:on_get:async'] == 2002
assert resource.counter['backround:on_get:sync'] == 4040
result = client.simulate_put()
assert result.status_code == 500
# NOTE(kgriffs): Remove default handlers so that we can check the raised
# exception is what we expected.
app._error_handlers.clear()
with pytest.raises(TypeError) as exinfo:
client.simulate_put()
assert 'coroutine' in str(exinfo.value)
class SimpleCallback:
def __init__(self):
self.called = 0
self.event = asyncio.Event()
async def _call_me(self):
self.called += 1
self.event.set()
async def on_get(self, req, resp):
resp.content_type = 'text/plain'
resp.data = b'Hello, World!\n'
resp.schedule(self._call_me)
on_head = on_get
async def on_get_sse(self, req, resp):
async def nop_emitter():
yield None
resp.sse = nop_emitter()
resp.schedule(self._call_me)
async def on_get_stream(self, req, resp):
async def stream():
yield b'One\n'
yield b'Two\n'
yield b'Three\n'
resp.content_type = 'text/plain'
resp.stream = stream()
resp.schedule(self._call_me)
@pytest.fixture()
def simple_resource():
return SimpleCallback()
@pytest.fixture()
def callback_app(simple_resource):
app = App()
app.add_route('/', simple_resource)
app.add_route('/sse', simple_resource, suffix='sse')
app.add_route('/stream', simple_resource, suffix='stream')
return app
@pytest.mark.parametrize(
'method,uri,expected',
[
('GET', '/', 'Hello, World!\n'),
('HEAD', '/', ''),
('GET', '/sse', ': ping\n\n'),
('GET', '/stream', 'One\nTwo\nThree\n'),
],
)
async def test_callback(callback_app, simple_resource, method, uri, expected):
async with testing.ASGIConductor(callback_app) as conductor:
resp = await conductor.simulate_request(method, uri)
assert resp.status_code == 200
assert resp.text == expected
await asyncio.wait_for(simple_resource.event.wait(), 3.0)
assert simple_resource.called == 1
|