tests: Clear in-memory Client caches before testing query counts.

This makes counts more apples-to-apples comparable when run
back-to-back.

(cherry picked from commit a84de411a9)
This commit is contained in:
Alex Vandiver 2023-09-27 00:10:49 +00:00 committed by Tim Abbott
parent c440958ecf
commit aee36b2b49
9 changed files with 30 additions and 31 deletions

View File

@ -102,7 +102,7 @@ class ActivityTest(ZulipTestCase):
user_profile.is_staff = True
user_profile.save(update_fields=["is_staff"])
with self.assert_database_query_count(11):
with self.assert_database_query_count(12):
result = self.client_get("/activity")
self.assertEqual(result.status_code, 200)
@ -131,20 +131,20 @@ class ActivityTest(ZulipTestCase):
hostname="demo.example.com",
contact_email="email@example.com",
)
with self.assert_database_query_count(10):
with self.assert_database_query_count(11):
result = self.client_get("/activity/remote")
self.assertEqual(result.status_code, 200)
with self.assert_database_query_count(4):
with self.assert_database_query_count(5):
result = self.client_get("/activity/integrations")
self.assertEqual(result.status_code, 200)
with self.assert_database_query_count(8):
with self.assert_database_query_count(9):
result = self.client_get("/realm_activity/zulip/")
self.assertEqual(result.status_code, 200)
iago = self.example_user("iago")
with self.assert_database_query_count(5):
with self.assert_database_query_count(6):
result = self.client_get(f"/user_activity/{iago.id}/")
self.assertEqual(result.status_code, 200)

View File

@ -53,7 +53,7 @@ from zerver.lib.rate_limiter import RateLimitedIPAddr, rules
from zerver.lib.request import RequestNotes
from zerver.lib.upload.s3 import S3UploadBackend
from zerver.models import Client, Message, RealmUserDefault, Subscription, UserMessage, UserProfile
from zerver.models.clients import get_client
from zerver.models.clients import clear_client_cache, get_client
from zerver.models.realms import get_realm
from zerver.models.streams import get_stream
from zerver.tornado.handlers import AsyncDjangoHandler, allocate_handler_id
@ -181,6 +181,7 @@ def queries_captured(
cache = get_cache_backend(None)
cache.clear()
flush_per_request_caches()
clear_client_cache()
with mock.patch.multiple(
TimeTrackingCursor, execute=cursor_execute, executemany=cursor_executemany
):

View File

@ -148,7 +148,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assert_num_bots_equal(num_bots)
with self.assert_database_query_count(3):
with self.assert_database_query_count(4):
users_result = self.client_get("/json/users")
self.assert_json_success(users_result)

View File

@ -906,7 +906,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase):
test_bot = self.create_test_bot("foo-bot", iago)
self.login_user(iago)
with self.assert_database_query_count(4):
with self.assert_database_query_count(5):
response = self.client_get(
"/json/users", {"client_gravatar": "false", "include_custom_profile_fields": "true"}
)

View File

@ -53,7 +53,6 @@ from zerver.lib.users import get_api_key
from zerver.lib.utils import generate_api_key, has_api_key_format
from zerver.middleware import LogRequests, parse_client
from zerver.models import Client, Realm, UserProfile
from zerver.models.clients import clear_client_cache
from zerver.models.realms import get_realm
from zerver.models.users import get_user
@ -1664,7 +1663,7 @@ class ClientTestCase(ZulipTestCase):
return notes.client, notes.client_name
self.assertEqual(Client.objects.filter(name="ZulipThingy").count(), 0)
with queries_captured() as queries:
with queries_captured(keep_cache_warm=True) as queries:
client, client_name = request_user_agent("ZulipThingy/1.0.0")
self.assertEqual(client.name, "ZulipThingy")
self.assertEqual(client_name, "ZulipThingy")
@ -1672,7 +1671,7 @@ class ClientTestCase(ZulipTestCase):
self.assert_length(queries, 2)
# Ensure our in-memory cache prevents another database hit
with queries_captured() as queries:
with queries_captured(keep_cache_warm=True) as queries:
client, client_name = request_user_agent(
"ZulipThingy/1.0.0",
)
@ -1681,7 +1680,7 @@ class ClientTestCase(ZulipTestCase):
self.assert_length(queries, 0)
# This operates on the extracted value, so different ZulipThingy versions don't cause another DB query
with queries_captured() as queries:
with queries_captured(keep_cache_warm=True) as queries:
client, client_name = request_user_agent(
"ZulipThingy/2.0.0",
)
@ -1691,8 +1690,7 @@ class ClientTestCase(ZulipTestCase):
# If we clear the memory cache we see a database query but get
# the same client-id back.
clear_client_cache()
with queries_captured() as queries:
with queries_captured(keep_cache_warm=False) as queries:
fresh_client, client_name = request_user_agent(
"ZulipThingy/2.0.0",
)
@ -1701,7 +1699,7 @@ class ClientTestCase(ZulipTestCase):
self.assert_length(queries, 1)
# Ensure that long parsed user-agents (longer than 30 characters) work
with queries_captured() as queries:
with queries_captured(keep_cache_warm=True) as queries:
client, client_name = request_user_agent(
"very-long-name-goes-here-and-somewhere-else (client@example.com)"
)
@ -1711,7 +1709,7 @@ class ClientTestCase(ZulipTestCase):
self.assert_length(queries, 2)
# Longer than that uses the same in-memory cache key, so no database queries
with queries_captured() as queries:
with queries_captured(keep_cache_warm=True) as queries:
client, client_name = request_user_agent(
"very-long-name-goes-here-and-still-works (client@example.com)"
)

View File

@ -251,7 +251,7 @@ class HomeTest(ZulipTestCase):
self.client_post("/json/bots", bot_info)
# Verify succeeds once logged-in
with self.assert_database_query_count(50):
with self.assert_database_query_count(51):
with patch("zerver.lib.cache.cache_set") as cache_mock:
result = self._get_home_page(stream="Denmark")
self.check_rendered_logged_in_app(result)
@ -259,7 +259,7 @@ class HomeTest(ZulipTestCase):
set(result["Cache-Control"].split(", ")), {"must-revalidate", "no-store", "no-cache"}
)
self.assert_length(cache_mock.call_args_list, 5)
self.assert_length(cache_mock.call_args_list, 6)
html = result.content.decode()
@ -469,7 +469,7 @@ class HomeTest(ZulipTestCase):
self._get_home_page()
# Then for the second page load, measure the number of queries.
with self.assert_database_query_count(45):
with self.assert_database_query_count(46):
result = self._get_home_page()
# Do a sanity check that our new streams were in the payload.

View File

@ -4621,7 +4621,7 @@ class SubscriptionAPITest(ZulipTestCase):
# Now add ourselves
with self.capture_send_event_calls(expected_num_events=2) as events:
with self.assert_database_query_count(13):
with self.assert_database_query_count(14):
self.common_subscribe_to_streams(
self.test_user,
streams_to_sub,
@ -5530,7 +5530,7 @@ class SubscriptionAPITest(ZulipTestCase):
)
# Test creating private stream.
with self.assert_database_query_count(36):
with self.assert_database_query_count(38):
self.common_subscribe_to_streams(
self.test_user,
[new_streams[1]],
@ -5542,7 +5542,7 @@ class SubscriptionAPITest(ZulipTestCase):
notifications_stream = get_stream(self.streams[0], self.test_realm)
self.test_realm.notifications_stream_id = notifications_stream.id
self.test_realm.save()
with self.assert_database_query_count(45):
with self.assert_database_query_count(47):
self.common_subscribe_to_streams(
self.test_user,
[new_streams[2]],

View File

@ -713,7 +713,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Owner user should be able to view file
self.login_user(hamlet)
with self.assert_database_query_count(5):
with self.assert_database_query_count(6):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")
@ -721,7 +721,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Subscribed user who received the message should be able to view file
self.login_user(cordelia)
with self.assert_database_query_count(6):
with self.assert_database_query_count(7):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")
@ -774,7 +774,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Owner user should be able to view file
self.login_user(user)
with self.assert_database_query_count(5):
with self.assert_database_query_count(6):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")
@ -782,7 +782,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Originally subscribed user should be able to view file
self.login_user(polonius)
with self.assert_database_query_count(6):
with self.assert_database_query_count(7):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")
@ -790,7 +790,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
# Subscribed user who did not receive the message should also be able to view file
self.login_user(late_subscribed_user)
with self.assert_database_query_count(9):
with self.assert_database_query_count(10):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")
@ -800,7 +800,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
def assert_cannot_access_file(user: UserProfile) -> None:
self.login_user(user)
# It takes a few extra queries to verify lack of access with shared history.
with self.assert_database_query_count(8):
with self.assert_database_query_count(9):
response = self.client_get(url)
self.assertEqual(response.status_code, 403)
self.assert_in_response("You are not authorized to view this file.", response)
@ -841,7 +841,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
user = self.example_user("aaron")
self.login_user(user)
with self.assert_database_query_count(8):
with self.assert_database_query_count(9):
response = self.client_get(url)
self.assertEqual(response.status_code, 403)
self.assert_in_response("You are not authorized to view this file.", response)
@ -850,7 +850,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
self.subscribe(user, "test-subscribe 2")
# If we were accidentally one query per message, this would be 20+
with self.assert_database_query_count(9):
with self.assert_database_query_count(10):
response = self.client_get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.getvalue(), b"zulip!")

View File

@ -1462,7 +1462,7 @@ class UserProfileTest(ZulipTestCase):
# Subscribe to the stream.
self.subscribe(iago, stream.name)
with self.assert_database_query_count(6):
with self.assert_database_query_count(7):
result = orjson.loads(
self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}").content
)