Skip to content

Commit 7b57794

Browse files
committed
Add a test for eventstreams
1 parent 5d911bd commit 7b57794

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

tests/resources/eventstream.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import asyncio
2+
3+
import tornado.escape
4+
import tornado.ioloop
5+
import tornado.options
6+
import tornado.web
7+
import tornado.websocket
8+
from tornado.options import define, options
9+
10+
11+
class Application(tornado.web.Application):
12+
def __init__(self):
13+
handlers = [
14+
(r"/stream/(\d+)", StreamHandler),
15+
]
16+
super().__init__(handlers)
17+
18+
19+
class StreamHandler(tornado.web.RequestHandler):
20+
async def get(self, seconds):
21+
for i in range(int(seconds)):
22+
await asyncio.sleep(0.5)
23+
self.write(f"data: {i}\n\n")
24+
await self.flush()
25+
26+
27+
def main():
28+
define("port", default=8888, help="run on the given port", type=int)
29+
options.parse_command_line()
30+
app = Application()
31+
app.listen(options.port)
32+
tornado.ioloop.IOLoop.current().start()
33+
34+
35+
if __name__ == "__main__":
36+
main()

tests/resources/jupyter_server_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def my_env():
7979
"X-Custom-Header": "pytest-23456",
8080
},
8181
},
82+
"python-eventstream": {
83+
"command": [sys.executable, "./tests/resources/eventstream.py", "--port={port}"]
84+
},
8285
"python-unix-socket-true": {
8386
"command": [
8487
sys.executable,

tests/test_proxies.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import gzip
22
import json
33
import sys
4+
import time
45
from http.client import HTTPConnection
56
from io import BytesIO
67
from typing import Tuple
78
from urllib.parse import quote
89

910
import pytest
10-
from tornado.httpclient import HTTPClientError
11+
from tornado.httpclient import AsyncHTTPClient, HTTPClientError
1112
from tornado.websocket import websocket_connect
1213

1314
# use ipv4 for CI, etc.
@@ -343,6 +344,39 @@ def test_server_content_encoding_header(
343344
assert f.read() == b"this is a test"
344345

345346

347+
async def test_eventstream(a_server_port_and_token: Tuple[int, str]) -> None:
348+
PORT, TOKEN = a_server_port_and_token
349+
# The test server under eventstream.py will send back monotonically increasing numbers
350+
# starting at 0 until the specified limit, with a 500ms gap between them. We test that:
351+
# 1. We get back as many callbacks from our streaming read as the total number,
352+
# as the server does a flush after writing each entry.
353+
# 2. The streaming entries are read (with some error margin) around the 500ms mark, to
354+
# ensure this is *actually* being streamed
355+
limit = 3
356+
last_cb_time = time.perf_counter()
357+
times_called = 0
358+
stream_read_intervals = []
359+
360+
def streaming_cb(data):
361+
nonlocal times_called, last_cb_time, stream_read_intervals
362+
time_taken = time.perf_counter() - last_cb_time
363+
last_cb_time = time.perf_counter()
364+
stream_read_intervals.append(time_taken)
365+
times_called += 1
366+
367+
url = f"http://{LOCALHOST}:{PORT}/python-eventstream/stream/{limit}?token={TOKEN}"
368+
client = AsyncHTTPClient()
369+
await client.fetch(
370+
url,
371+
headers={"Accept": "text/event-stream"},
372+
request_timeout=22,
373+
streaming_callback=streaming_cb,
374+
)
375+
assert times_called == limit
376+
print(stream_read_intervals)
377+
assert all([0.45 < t < 0.7 for t in stream_read_intervals])
378+
379+
346380
async def test_server_proxy_websocket_messages(
347381
a_server_port_and_token: Tuple[int, str]
348382
) -> None:

0 commit comments

Comments
 (0)