Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow for make_awaitable's return value to be re-used. #8261

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8261.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify tests that mock asynchronous functions.
30 changes: 10 additions & 20 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ def test_join_too_large(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
)
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -110,11 +108,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
)
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -150,11 +146,9 @@ def test_join_too_large_once_joined(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(None)
)
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

# Artificially raise the complexity
Expand Down Expand Up @@ -208,11 +202,9 @@ def test_join_too_large_no_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
)
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down Expand Up @@ -240,11 +232,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
)
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
Expand Down
2 changes: 1 addition & 1 deletion tests/federation/test_federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FederationSenderReceiptsTestCases(HomeserverTestCase):
def make_homeserver(self, reactor, clock):
mock_state_handler = Mock(spec=["get_current_hosts_in_room"])
# Ensure a new Awaitable is created for each call.
mock_state_handler.get_current_hosts_in_room.side_effect = lambda room_Id: make_awaitable(
mock_state_handler.get_current_hosts_in_room.return_value = make_awaitable(
["test", "host2"]
)
return self.setup_test_homeserver(
Expand Down
20 changes: 10 additions & 10 deletions tests/handlers/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_mau_limits_disabled(self):
def test_mau_limits_exceeded_large(self):
self.auth_blocking._limit_usage_by_mau = True
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.large_number_of_users)
return_value=make_awaitable(self.large_number_of_users)
)

with self.assertRaises(ResourceLimitError):
Expand All @@ -154,7 +154,7 @@ def test_mau_limits_exceeded_large(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.large_number_of_users)
return_value=make_awaitable(self.large_number_of_users)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -169,7 +169,7 @@ def test_mau_limits_parity(self):

# If not in monthly active cohort
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -179,7 +179,7 @@ def test_mau_limits_parity(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -189,21 +189,21 @@ def test_mau_limits_parity(self):
)
# If in monthly active cohort
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
return_value=make_awaitable(self.hs.get_clock().time_msec())
)
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
yield defer.ensureDeferred(
self.auth_handler.get_access_token_for_user_id(
"user_a", device_id=None, valid_until_ms=None
)
)
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
return_value=make_awaitable(self.hs.get_clock().time_msec())
)
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
yield defer.ensureDeferred(
self.auth_handler.validate_short_term_login_token_and_get_user_id(
Expand All @@ -216,7 +216,7 @@ def test_mau_limits_not_exceeded(self):
self.auth_blocking._limit_usage_by_mau = True

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.small_number_of_users)
return_value=make_awaitable(self.small_number_of_users)
)
# Ensure does not raise exception
yield defer.ensureDeferred(
Expand All @@ -226,7 +226,7 @@ def test_mau_limits_not_exceeded(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.small_number_of_users)
return_value=make_awaitable(self.small_number_of_users)
)
yield defer.ensureDeferred(
self.auth_handler.validate_short_term_login_token_and_get_user_id(
Expand Down
10 changes: 5 additions & 5 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ def test_mau_limits_when_disabled(self):
def test_get_or_create_user_mau_not_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.count_monthly_users = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value - 1)
return_value=make_awaitable(self.hs.config.max_mau_value - 1)
)
# Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "c", "User"))

def test_get_or_create_user_mau_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.lots_of_users)
return_value=make_awaitable(self.lots_of_users)
)
self.get_failure(
self.get_or_create_user(self.requester, "b", "display_name"),
ResourceLimitError,
)

self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
self.get_failure(
self.get_or_create_user(self.requester, "b", "display_name"),
Expand All @@ -126,14 +126,14 @@ def test_get_or_create_user_mau_blocked(self):
def test_register_mau_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.lots_of_users)
return_value=make_awaitable(self.lots_of_users)
)
self.get_failure(
self.handler.register_user(localpart="local_part"), ResourceLimitError
)

self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
self.get_failure(
self.handler.register_user(localpart="local_part"), ResourceLimitError
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def prepare(self, reactor, clock, hs):
retry_timings_res
)

self.datastore.get_device_updates_by_remote.side_effect = lambda destination, from_stream_id, limit: make_awaitable(
self.datastore.get_device_updates_by_remote.return_value = make_awaitable(
(0, [])
)

Expand Down
10 changes: 5 additions & 5 deletions tests/replication/test_federation_sender_shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_send_event_single_sender(self):
new event.
"""
mock_client = Mock(spec=["put_json"])
mock_client.put_json.side_effect = lambda *_, **__: make_awaitable({})
mock_client.put_json.return_value = make_awaitable({})

self.make_worker_hs(
"synapse.app.federation_sender",
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_send_event_sharded(self):
new events.
"""
mock_client1 = Mock(spec=["put_json"])
mock_client1.put_json.side_effect = lambda *_, **__: make_awaitable({})
mock_client1.put_json.return_value = make_awaitable({})
self.make_worker_hs(
"synapse.app.federation_sender",
{
Expand All @@ -85,7 +85,7 @@ def test_send_event_sharded(self):
)

mock_client2 = Mock(spec=["put_json"])
mock_client2.put_json.side_effect = lambda *_, **__: make_awaitable({})
mock_client2.put_json.return_value = make_awaitable({})
self.make_worker_hs(
"synapse.app.federation_sender",
{
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_send_typing_sharded(self):
new typing EDUs.
"""
mock_client1 = Mock(spec=["put_json"])
mock_client1.put_json.side_effect = lambda *_, **__: make_awaitable({})
mock_client1.put_json.return_value = make_awaitable({})
self.make_worker_hs(
"synapse.app.federation_sender",
{
Expand All @@ -148,7 +148,7 @@ def test_send_typing_sharded(self):
)

mock_client2 = Mock(spec=["put_json"])
mock_client2.put_json.side_effect = lambda *_, **__: make_awaitable({})
mock_client2.put_json.return_value = make_awaitable({})
self.make_worker_hs(
"synapse.app.federation_sender",
{
Expand Down
6 changes: 3 additions & 3 deletions tests/rest/admin/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def test_register_mau_limit_reached(self):

# Set monthly active users to the limit
store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
# Check that the blocking of monthly active users is working as expected
# The registration of a new user fails due to the limit
Expand Down Expand Up @@ -591,7 +591,7 @@ def test_create_user_mau_limit_reached_active_admin(self):

# Set monthly active users to the limit
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
# Check that the blocking of monthly active users is working as expected
# The registration of a new user fails due to the limit
Expand Down Expand Up @@ -631,7 +631,7 @@ def test_create_user_mau_limit_reached_passive_admin(self):

# Set monthly active users to the limit
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
# Check that the blocking of monthly active users is working as expected
# The registration of a new user fails due to the limit
Expand Down
14 changes: 5 additions & 9 deletions tests/server_notices/test_resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def prepare(self, reactor, clock, hs):
raise Exception("Failed to find reference to ResourceLimitsServerNotices")

self._rlsn._store.user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(1000)
return_value=make_awaitable(1000)
)
self._rlsn._server_notices_manager.send_notice = Mock(
return_value=defer.succeed(Mock())
Expand All @@ -80,9 +80,7 @@ def prepare(self, reactor, clock, hs):
return_value=defer.succeed("!something:localhost")
)
self._rlsn._store.add_tag_to_room = Mock(return_value=defer.succeed(None))
self._rlsn._store.get_tags_for_room = Mock(
side_effect=lambda user_id, room_id: make_awaitable({})
)
self._rlsn._store.get_tags_for_room = Mock(return_value=make_awaitable({}))

@override_config({"hs_disabled": True})
def test_maybe_send_server_notice_disabled_hs(self):
Expand Down Expand Up @@ -158,7 +156,7 @@ def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
"""
self._rlsn._auth.check_auth_blocking = Mock(return_value=defer.succeed(None))
self._rlsn._store.user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(None)
return_value=make_awaitable(None)
)
self.get_success(self._rlsn.maybe_send_server_notice_to_user(self.user_id))

Expand Down Expand Up @@ -261,12 +259,10 @@ def prepare(self, reactor, clock, hs):
self.user_id = "@user_id:test"

def test_server_notice_only_sent_once(self):
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(1000)
)
self.store.get_monthly_active_count = Mock(return_value=make_awaitable(1000))

self.store.user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(1000)
return_value=make_awaitable(1000)
)

# Call the function multiple times to ensure we only send the notice once
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_adding_monthly_active_user_when_full(self):
user_id = "@user:server"

self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(lots_of_users)
return_value=make_awaitable(lots_of_users)
)
self.get_success(
self.store.insert_client_ip(
Expand Down
16 changes: 4 additions & 12 deletions tests/storage/test_monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,15 @@ def test_populate_monthly_users_is_guest(self):
)
self.get_success(d)

self.store.upsert_monthly_active_user = Mock(
side_effect=lambda user_id: make_awaitable(None)
)
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))

d = self.store.populate_monthly_active_users(user_id)
self.get_success(d)

self.store.upsert_monthly_active_user.assert_not_called()

def test_populate_monthly_users_should_update(self):
self.store.upsert_monthly_active_user = Mock(
side_effect=lambda user_id: make_awaitable(None)
)
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))

self.store.is_trial_user = Mock(return_value=defer.succeed(False))

Expand All @@ -256,9 +252,7 @@ def test_populate_monthly_users_should_update(self):
self.store.upsert_monthly_active_user.assert_called_once()

def test_populate_monthly_users_should_not_update(self):
self.store.upsert_monthly_active_user = Mock(
side_effect=lambda user_id: make_awaitable(None)
)
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))

self.store.is_trial_user = Mock(return_value=defer.succeed(False))
self.store.user_last_seen_monthly_active = Mock(
Expand Down Expand Up @@ -344,9 +338,7 @@ def test_track_monthly_users_without_cap(self):

@override_config({"limit_usage_by_mau": False, "mau_stats_only": False})
def test_no_users_when_not_tracking(self):
self.store.upsert_monthly_active_user = Mock(
side_effect=lambda user_id: make_awaitable(None)
)
self.store.upsert_monthly_active_user = Mock(return_value=make_awaitable(None))

self.get_success(self.store.populate_monthly_active_users("@user:sever"))

Expand Down
Loading