tests: Add assert_memcached_count.

We use a specific name to distinguish from other caches
like per-request caches.
This commit is contained in:
Steve Howell 2023-08-11 13:28:22 +00:00 committed by Tim Abbott
parent 0eea42b48c
commit 549891266d
6 changed files with 24 additions and 25 deletions

View File

@ -70,7 +70,12 @@ from zerver.lib.test_console_output import (
tee_stderr_and_find_extra_console_output, tee_stderr_and_find_extra_console_output,
tee_stdout_and_find_extra_console_output, tee_stdout_and_find_extra_console_output,
) )
from zerver.lib.test_helpers import find_key_by_email, instrument_url, queries_captured from zerver.lib.test_helpers import (
cache_tries_captured,
find_key_by_email,
instrument_url,
queries_captured,
)
from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, filter_by_topic_name_via_message from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, filter_by_topic_name_via_message
from zerver.lib.user_groups import get_system_user_group_for_user from zerver.lib.user_groups import get_system_user_group_for_user
from zerver.lib.users import get_api_key from zerver.lib.users import get_api_key
@ -1223,6 +1228,12 @@ Output:
print(f"\nexpected length: {count}\nactual length: {actual_count}") print(f"\nexpected length: {count}\nactual length: {actual_count}")
raise AssertionError(f"{type(items)} is of unexpected size!") raise AssertionError(f"{type(items)} is of unexpected size!")
@contextmanager
def assert_memcached_count(self, count: int) -> Iterator[None]:
with cache_tries_captured() as cache_tries:
yield
self.assert_length(cache_tries, count)
@contextmanager @contextmanager
def assert_database_query_count( def assert_database_query_count(
self, count: int, include_savepoints: bool = False, keep_cache_warm: bool = False self, count: int, include_savepoints: bool = False, keep_cache_warm: bool = False

View File

@ -24,7 +24,6 @@ from zerver.lib.digest import (
from zerver.lib.message import get_last_message_id from zerver.lib.message import get_last_message_id
from zerver.lib.streams import create_stream_if_needed from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import cache_tries_captured
from zerver.models import ( from zerver.models import (
Client, Client,
Message, Message,
@ -209,11 +208,9 @@ class TestDigestEmailMessages(ZulipTestCase):
digest_user_ids = [user.id for user in digest_users] digest_user_ids = [user.id for user in digest_users]
with self.assert_database_query_count(12): with self.assert_database_query_count(12):
with cache_tries_captured() as cache_tries: with self.assert_memcached_count(0):
bulk_handle_digest_email(digest_user_ids, cutoff) bulk_handle_digest_email(digest_user_ids, cutoff)
self.assert_length(cache_tries, 0)
self.assert_length(digest_users, mock_send_future_email.call_count) self.assert_length(digest_users, mock_send_future_email.call_count)
for i, digest_user in enumerate(digest_users): for i, digest_user in enumerate(digest_users):

View File

@ -21,7 +21,7 @@ from zerver.actions.user_topics import do_set_user_topic_visibility_policy
from zerver.actions.users import do_change_user_role from zerver.actions.users import do_change_user_role
from zerver.lib.message import MessageDict, has_message_access, messages_for_ids, truncate_topic from zerver.lib.message import MessageDict, has_message_access, messages_for_ids, truncate_topic
from zerver.lib.test_classes import ZulipTestCase, get_topic_messages from zerver.lib.test_classes import ZulipTestCase, get_topic_messages
from zerver.lib.test_helpers import cache_tries_captured, queries_captured from zerver.lib.test_helpers import queries_captured
from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, TOPIC_NAME from zerver.lib.topic import RESOLVED_TOPIC_PREFIX, TOPIC_NAME
from zerver.lib.url_encoding import near_stream_message_url from zerver.lib.url_encoding import near_stream_message_url
from zerver.lib.user_topics import ( from zerver.lib.user_topics import (
@ -3691,7 +3691,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), cache_tries_captured() as cache_tries: with self.assert_database_query_count(56), self.assert_memcached_count(13):
result = self.client_patch( result = self.client_patch(
f"/json/messages/{msg_id}", f"/json/messages/{msg_id}",
{ {
@ -3701,7 +3701,6 @@ class EditMessageTest(EditMessageTestCase):
"topic": "new topic", "topic": "new topic",
}, },
) )
self.assert_length(cache_tries, 13)
messages = get_topic_messages(user_profile, old_stream, "test") messages = get_topic_messages(user_profile, old_stream, "test")
self.assert_length(messages, 1) self.assert_length(messages, 1)

View File

@ -52,7 +52,6 @@ from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import ( from zerver.lib.test_helpers import (
HostRequestMock, HostRequestMock,
avatar_disk_path, avatar_disk_path,
cache_tries_captured,
find_key_by_email, find_key_by_email,
get_test_image_file, get_test_image_file,
load_subdomain_token, load_subdomain_token,
@ -929,15 +928,13 @@ class LoginTest(ZulipTestCase):
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)
with self.assert_database_query_count(104), cache_tries_captured() as cache_tries:
with self.captureOnCommitCallbacks(execute=True):
self.register(self.nonreg_email("test"), "test")
# We can probably avoid a couple cache hits here, but there doesn't # We can probably avoid a couple cache hits here, but there doesn't
# seem to be any O(N) behavior. Some of the cache hits are related # seem to be any O(N) behavior. Some of the cache hits are related
# to sending messages, such as getting the welcome bot, looking up # to sending messages, such as getting the welcome bot, looking up
# the alert words for a realm, etc. # the alert words for a realm, etc.
self.assert_length(cache_tries, 19) with self.assert_database_query_count(104), self.assert_memcached_count(19):
with self.captureOnCommitCallbacks(execute=True):
self.register(self.nonreg_email("test"), "test")
user_profile = self.nonreg_user("test") user_profile = self.nonreg_user("test")
self.assert_logged_in_user_id(user_profile.id) self.assert_logged_in_user_id(user_profile.id)

View File

@ -4944,7 +4944,7 @@ class SubscriptionAPITest(ZulipTestCase):
# Sends 3 peer-remove events and 2 unsubscribe events. # Sends 3 peer-remove events and 2 unsubscribe events.
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(16): with self.assert_database_query_count(16):
with cache_tries_captured() as cache_count: with self.assert_memcached_count(3):
bulk_remove_subscriptions( bulk_remove_subscriptions(
realm, realm,
[user1, user2], [user1, user2],
@ -4952,8 +4952,6 @@ class SubscriptionAPITest(ZulipTestCase):
acting_user=None, acting_user=None,
) )
self.assert_length(cache_count, 3)
peer_events = [e for e in events if e["event"].get("op") == "peer_remove"] peer_events = [e for e in events if e["event"].get("op") == "peer_remove"]
# We only care about a subset of users when we inspect # We only care about a subset of users when we inspect
@ -5081,8 +5079,11 @@ class SubscriptionAPITest(ZulipTestCase):
test_user_ids = [user.id for user in test_users] test_user_ids = [user.id for user in test_users]
# The only known O(N) behavior here is that we call
# principal_to_user_profile for each of our users, but it
# should be cached.
with self.assert_database_query_count(21): with self.assert_database_query_count(21):
with cache_tries_captured() as cache_tries: with self.assert_memcached_count(3):
with mock.patch("zerver.views.streams.send_messages_for_new_subscribers"): with mock.patch("zerver.views.streams.send_messages_for_new_subscribers"):
self.common_subscribe_to_streams( self.common_subscribe_to_streams(
desdemona, desdemona,
@ -5090,10 +5091,6 @@ class SubscriptionAPITest(ZulipTestCase):
dict(principals=orjson.dumps(test_user_ids).decode()), dict(principals=orjson.dumps(test_user_ids).decode()),
) )
# The only known O(N) behavior here is that we call
# principal_to_user_profile for each of our users.
self.assert_length(cache_tries, 3)
def test_subscriptions_add_for_principal(self) -> None: def test_subscriptions_add_for_principal(self) -> None:
""" """
You can subscribe other people to streams. You can subscribe other people to streams.

View File

@ -40,7 +40,6 @@ from zerver.lib.send_email import (
from zerver.lib.stream_topic import StreamTopicTarget from zerver.lib.stream_topic import StreamTopicTarget
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.test_helpers import ( from zerver.lib.test_helpers import (
cache_tries_captured,
get_subscription, get_subscription,
get_test_image_file, get_test_image_file,
reset_email_visibility_to_everyone_in_zulip_realm, reset_email_visibility_to_everyone_in_zulip_realm,
@ -795,7 +794,7 @@ class QueryCountTest(ZulipTestCase):
prereg_user = PreregistrationUser.objects.get(email="fred@zulip.com") prereg_user = PreregistrationUser.objects.get(email="fred@zulip.com")
with self.assert_database_query_count(93): with self.assert_database_query_count(93):
with cache_tries_captured() as cache_tries: with self.assert_memcached_count(24):
with self.capture_send_event_calls(expected_num_events=11) as events: with self.capture_send_event_calls(expected_num_events=11) as events:
fred = do_create_user( fred = do_create_user(
email="fred@zulip.com", email="fred@zulip.com",
@ -806,7 +805,6 @@ class QueryCountTest(ZulipTestCase):
acting_user=None, acting_user=None,
) )
self.assert_length(cache_tries, 24)
peer_add_events = [event for event in events if event["event"].get("op") == "peer_add"] peer_add_events = [event for event in events if event["event"].get("op") == "peer_add"]
notifications = set() notifications = set()