From 0430705d13927eba1979fbf29c6234a6ae1545b3 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 8 Jun 2022 16:12:19 -0700 Subject: [PATCH] test_tornado: Call process_event on first fetch_events return. The 0.1 second delay was sometimes not long enough to guarantee we hit the async response path, resulting in a nondeterministic coverage failure. Signed-off-by: Anders Kaseorg --- zerver/tests/test_tornado.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/zerver/tests/test_tornado.py b/zerver/tests/test_tornado.py index f7bf223f60..be5f68be20 100644 --- a/zerver/tests/test_tornado.py +++ b/zerver/tests/test_tornado.py @@ -1,8 +1,8 @@ import asyncio import urllib.parse from functools import wraps -from typing import Any, Awaitable, Callable, Dict, Optional, TypeVar -from unittest import TestResult +from typing import Any, Awaitable, Callable, Dict, Mapping, Optional, TypeVar +from unittest import TestResult, mock import orjson from asgiref.sync import async_to_sync, sync_to_async @@ -144,8 +144,14 @@ class EventsTestCase(TornadoWebTestCase): ) process_event(event, users) - self.io_loop.call_later(0.1, process_events) - response = await self.client_get_async(path) + def wrapped_fetch_events(query: Mapping[str, Any]) -> Dict[str, Any]: + ret = event_queue.fetch_events(query) + self.io_loop.add_callback(process_events) + return ret + + with mock.patch("zerver.tornado.views.fetch_events", side_effect=wrapped_fetch_events): + response = await self.client_get_async(path) + self.assertEqual(response.headers["Vary"], "Accept-Language, Cookie") data = orjson.loads(response.body) self.assertEqual(