tests: Flush per-request caches automatically for query counts.

This commit is contained in:
Steve Howell 2023-08-11 14:40:06 +00:00 committed by Tim Abbott
parent 730ae61ce5
commit 751b8b5bb5
9 changed files with 15 additions and 33 deletions

View File

@ -3,7 +3,7 @@ from unittest import mock
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Client, UserActivity, UserProfile, flush_per_request_caches from zerver.models import Client, UserActivity, UserProfile
class ActivityTest(ZulipTestCase): class ActivityTest(ZulipTestCase):
@ -31,18 +31,15 @@ class ActivityTest(ZulipTestCase):
user_profile.is_staff = True user_profile.is_staff = True
user_profile.save(update_fields=["is_staff"]) user_profile.save(update_fields=["is_staff"])
flush_per_request_caches()
with self.assert_database_query_count(18): with self.assert_database_query_count(18):
result = self.client_get("/activity") result = self.client_get("/activity")
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
flush_per_request_caches()
with self.assert_database_query_count(8): with self.assert_database_query_count(8):
result = self.client_get("/realm_activity/zulip/") result = self.client_get("/realm_activity/zulip/")
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
iago = self.example_user("iago") iago = self.example_user("iago")
flush_per_request_caches()
with self.assert_database_query_count(5): with self.assert_database_query_count(5):
result = self.client_get(f"/user_activity/{iago.id}/") result = self.client_get(f"/user_activity/{iago.id}/")
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View File

@ -56,6 +56,7 @@ from zerver.models import (
Subscription, Subscription,
UserMessage, UserMessage,
UserProfile, UserProfile,
flush_per_request_caches,
get_client, get_client,
get_realm, get_realm,
get_stream, get_stream,
@ -181,6 +182,7 @@ def queries_captured(
if not keep_cache_warm: if not keep_cache_warm:
cache = get_cache_backend(None) cache = get_cache_backend(None)
cache.clear() cache.clear()
flush_per_request_caches()
with mock.patch.multiple( with mock.patch.multiple(
TimeTrackingCursor, execute=cursor_execute, executemany=cursor_executemany TimeTrackingCursor, execute=cursor_execute, executemany=cursor_executemany
): ):

View File

@ -32,7 +32,6 @@ from zerver.models import (
Stream, Stream,
UserActivityInterval, UserActivityInterval,
UserProfile, UserProfile,
flush_per_request_caches,
get_client, get_client,
get_realm, get_realm,
get_stream, get_stream,
@ -60,7 +59,6 @@ class TestDigestEmailMessages(ZulipTestCase):
# Remove RealmAuditLog rows, so we don't exclude polonius. # Remove RealmAuditLog rows, so we don't exclude polonius.
RealmAuditLog.objects.all().delete() RealmAuditLog.objects.all().delete()
flush_per_request_caches()
# When this test is run in isolation, one additional query is run which # When this test is run in isolation, one additional query is run which
# is equivalent to # is equivalent to
# ContentType.objects.get(app_label='zerver', model='userprofile') # ContentType.objects.get(app_label='zerver', model='userprofile')
@ -137,7 +135,6 @@ class TestDigestEmailMessages(ZulipTestCase):
# Remove RealmAuditoLog rows, so we don't exclude polonius. # Remove RealmAuditoLog rows, so we don't exclude polonius.
RealmAuditLog.objects.all().delete() RealmAuditLog.objects.all().delete()
flush_per_request_caches()
# When this test is run in isolation, one additional query is run which # When this test is run in isolation, one additional query is run which
# is equivalent to # is equivalent to
# ContentType.objects.get(app_label='zerver', model='userprofile') # ContentType.objects.get(app_label='zerver', model='userprofile')
@ -195,8 +192,6 @@ class TestDigestEmailMessages(ZulipTestCase):
one_hour_ago = timezone_now() - datetime.timedelta(seconds=3600) one_hour_ago = timezone_now() - datetime.timedelta(seconds=3600)
cutoff = time.mktime(one_hour_ago.timetuple()) cutoff = time.mktime(one_hour_ago.timetuple())
flush_per_request_caches()
# When this test is run in isolation, one additional query is run which # When this test is run in isolation, one additional query is run which
# is equivalent to # is equivalent to
# ContentType.objects.get(app_label='zerver', model='userprofile') # ContentType.objects.get(app_label='zerver', model='userprofile')

View File

@ -32,7 +32,6 @@ from zerver.models import (
UserMessage, UserMessage,
UserPresence, UserPresence,
UserProfile, UserProfile,
flush_per_request_caches,
get_client, get_client,
get_realm, get_realm,
get_stream, get_stream,
@ -1248,7 +1247,6 @@ class FetchQueriesTest(ZulipTestCase):
self.login_user(user) self.login_user(user)
flush_per_request_caches()
with self.assert_database_query_count(39): with self.assert_database_query_count(39):
with mock.patch("zerver.lib.events.always_want") as want_mock: with mock.patch("zerver.lib.events.always_want") as want_mock:
fetch_initial_state_data(user) fetch_initial_state_data(user)
@ -1298,7 +1296,6 @@ class FetchQueriesTest(ZulipTestCase):
for event_type in sorted(wanted_event_types): for event_type in sorted(wanted_event_types):
count = expected_counts[event_type] count = expected_counts[event_type]
flush_per_request_caches()
with self.assert_database_query_count(count): with self.assert_database_query_count(count):
if event_type == "update_message_flags": if event_type == "update_message_flags":
event_types = ["update_message_flags", "message"] event_types = ["update_message_flags", "message"]

View File

@ -29,7 +29,6 @@ from zerver.models import (
Realm, Realm,
UserActivity, UserActivity,
UserProfile, UserProfile,
flush_per_request_caches,
get_realm, get_realm,
get_stream, get_stream,
get_system_bot, get_system_bot,
@ -246,7 +245,6 @@ class HomeTest(ZulipTestCase):
self.client_post("/json/bots", bot_info) self.client_post("/json/bots", bot_info)
# Verify succeeds once logged-in # Verify succeeds once logged-in
flush_per_request_caches()
with self.assert_database_query_count(49): with self.assert_database_query_count(49):
with patch("zerver.lib.cache.cache_set") as cache_mock: with patch("zerver.lib.cache.cache_set") as cache_mock:
result = self._get_home_page(stream="Denmark") result = self._get_home_page(stream="Denmark")
@ -302,7 +300,6 @@ class HomeTest(ZulipTestCase):
self.login("hamlet") self.login("hamlet")
# Verify succeeds once logged-in # Verify succeeds once logged-in
flush_per_request_caches()
with queries_captured(): with queries_captured():
with patch("zerver.lib.cache.cache_set"): with patch("zerver.lib.cache.cache_set"):
result = self._get_home_page(stream="Denmark") result = self._get_home_page(stream="Denmark")
@ -436,7 +433,6 @@ class HomeTest(ZulipTestCase):
def test_num_queries_for_realm_admin(self) -> None: def test_num_queries_for_realm_admin(self) -> None:
# Verify number of queries for Realm admin isn't much higher than for normal users. # Verify number of queries for Realm admin isn't much higher than for normal users.
self.login("iago") self.login("iago")
flush_per_request_caches()
with self.assert_database_query_count(50): with self.assert_database_query_count(50):
with patch("zerver.lib.cache.cache_set") as cache_mock: with patch("zerver.lib.cache.cache_set") as cache_mock:
result = self._get_home_page() result = self._get_home_page()
@ -468,7 +464,6 @@ class HomeTest(ZulipTestCase):
self._get_home_page() self._get_home_page()
# Then for the second page load, measure the number of queries. # Then for the second page load, measure the number of queries.
flush_per_request_caches()
with self.assert_database_query_count(44): with self.assert_database_query_count(44):
result = self._get_home_page() result = self._get_home_page()

View File

@ -176,7 +176,6 @@ class MessageDictTest(ZulipTestCase):
num_ids = len(ids) num_ids = len(ids)
self.assertTrue(num_ids >= 600) self.assertTrue(num_ids >= 600)
flush_per_request_caches()
with self.assert_database_query_count(7): with self.assert_database_query_count(7):
rows = list(MessageDict.get_raw_db_rows(ids)) rows = list(MessageDict.get_raw_db_rows(ids))

View File

@ -289,13 +289,14 @@ class EditMessageTest(EditMessageTestCase):
# Check number of queries performed # Check number of queries performed
# 1 query for realm_id per message = 3 # 1 query for realm_id per message = 3
# 1 query each for reactions & submessage for all messages = 2 # 1 query each for reactions & submessage for all messages = 2
with self.assert_database_query_count(5): # 1 query for linkifiers
# 1 query for display recipients
with self.assert_database_query_count(7):
MessageDict.to_dict_uncached(messages) MessageDict.to_dict_uncached(messages)
realm_id = 2 # Fetched from stream object realm_id = 2 # Fetched from stream object
# Check number of queries performed with realm_id # Check number of queries performed with realm_id
# 1 query each for reactions & submessage for all messages = 2 with self.assert_database_query_count(3):
with self.assert_database_query_count(2):
MessageDict.to_dict_uncached(messages, realm_id) MessageDict.to_dict_uncached(messages, realm_id)
def test_save_message(self) -> None: def test_save_message(self) -> None:
@ -1374,7 +1375,7 @@ class EditMessageTest(EditMessageTestCase):
# state + 1/user with a UserTopic row for the events data) # state + 1/user with a UserTopic row for the events data)
# beyond what is typical were there not UserTopic records to # beyond what is typical were there not UserTopic records to
# update. Ideally, we'd eliminate the per-user component. # update. Ideally, we'd eliminate the per-user component.
with self.assert_database_query_count(21): with self.assert_database_query_count(22):
check_update_message( check_update_message(
user_profile=hamlet, user_profile=hamlet,
message_id=message_id, message_id=message_id,
@ -1471,7 +1472,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(30): with self.assert_database_query_count(31):
check_update_message( check_update_message(
user_profile=desdemona, user_profile=desdemona,
message_id=message_id, message_id=message_id,
@ -1502,7 +1503,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(35): with self.assert_database_query_count(36):
check_update_message( check_update_message(
user_profile=desdemona, user_profile=desdemona,
message_id=message_id, message_id=message_id,
@ -1535,7 +1536,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED) set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
with self.assert_database_query_count(30): with self.assert_database_query_count(31):
check_update_message( check_update_message(
user_profile=desdemona, user_profile=desdemona,
message_id=message_id, message_id=message_id,
@ -1558,7 +1559,7 @@ class EditMessageTest(EditMessageTestCase):
second_message_id = self.send_stream_message( second_message_id = self.send_stream_message(
hamlet, stream_name, topic_name="changed topic name", content="Second message" hamlet, stream_name, topic_name="changed topic name", content="Second message"
) )
with self.assert_database_query_count(26): with self.assert_database_query_count(27):
check_update_message( check_update_message(
user_profile=desdemona, user_profile=desdemona,
message_id=second_message_id, message_id=second_message_id,
@ -1657,7 +1658,7 @@ class EditMessageTest(EditMessageTestCase):
users_to_be_notified_via_muted_topics_event.append(user_topic.user_profile_id) users_to_be_notified_via_muted_topics_event.append(user_topic.user_profile_id)
change_all_topic_name = "Topic 1 edited" change_all_topic_name = "Topic 1 edited"
with self.assert_database_query_count(26): with self.assert_database_query_count(27):
check_update_message( check_update_message(
user_profile=hamlet, user_profile=hamlet,
message_id=message_id, message_id=message_id,
@ -3691,7 +3692,7 @@ class EditMessageTest(EditMessageTestCase):
"iago", "test move stream", "new stream", "test" "iago", "test move stream", "new stream", "test"
) )
with self.assert_database_query_count(56), self.assert_memcached_count(13): with self.assert_database_query_count(57), self.assert_memcached_count(14):
result = self.client_patch( result = self.client_patch(
f"/json/messages/{msg_id}", f"/json/messages/{msg_id}",
{ {

View File

@ -77,7 +77,6 @@ from zerver.models import (
Subscription, Subscription,
UserMessage, UserMessage,
UserProfile, UserProfile,
flush_per_request_caches,
get_realm, get_realm,
get_stream, get_stream,
get_system_bot, get_system_bot,
@ -923,8 +922,7 @@ class LoginTest(ZulipTestCase):
content="test message", content="test message",
) )
# Clear all the caches. # Clear the ContentType cache.
flush_per_request_caches()
ContentType.objects.clear_cache() ContentType.objects.clear_cache()
# Ensure the number of queries we make is not O(streams) # Ensure the number of queries we make is not O(streams)

View File

@ -102,7 +102,6 @@ from zerver.models import (
UserMessage, UserMessage,
UserProfile, UserProfile,
active_non_guest_user_ids, active_non_guest_user_ids,
flush_per_request_caches,
get_default_stream_groups, get_default_stream_groups,
get_realm, get_realm,
get_stream, get_stream,
@ -4625,7 +4624,6 @@ class SubscriptionAPITest(ZulipTestCase):
user2 = self.example_user("iago") user2 = self.example_user("iago")
realm = get_realm("zulip") realm = get_realm("zulip")
streams_to_sub = ["multi_user_stream"] streams_to_sub = ["multi_user_stream"]
flush_per_request_caches()
with self.capture_send_event_calls(expected_num_events=5) as events: with self.capture_send_event_calls(expected_num_events=5) as events:
with self.assert_database_query_count(37): with self.assert_database_query_count(37):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(