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

Commit

Permalink
Do not yield on awaitables in tests. (#8193)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Aug 27, 2020
1 parent b49a5b9 commit e00816a
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 131 deletions.
1 change: 1 addition & 0 deletions changelog.d/8193.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
36 changes: 24 additions & 12 deletions tests/api/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ def test_filter_not_labels(self):
@defer.inlineCallbacks
def test_filter_presence_match(self):
user_filter_json = {"presence": {"types": ["m.*"]}}
filter_id = yield self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
)
)
event = MockEvent(sender="@foo:bar", type="m.profile")
events = [event]
Expand All @@ -388,8 +390,10 @@ def test_filter_presence_match(self):
def test_filter_presence_no_match(self):
user_filter_json = {"presence": {"types": ["m.*"]}}

filter_id = yield self.datastore.add_user_filter(
user_localpart=user_localpart + "2", user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.datastore.add_user_filter(
user_localpart=user_localpart + "2", user_filter=user_filter_json
)
)
event = MockEvent(
event_id="$asdasd:localhost",
Expand All @@ -410,8 +414,10 @@ def test_filter_presence_no_match(self):
@defer.inlineCallbacks
def test_filter_room_state_match(self):
user_filter_json = {"room": {"state": {"types": ["m.*"]}}}
filter_id = yield self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
)
)
event = MockEvent(sender="@foo:bar", type="m.room.topic", room_id="!foo:bar")
events = [event]
Expand All @@ -428,8 +434,10 @@ def test_filter_room_state_match(self):
@defer.inlineCallbacks
def test_filter_room_state_no_match(self):
user_filter_json = {"room": {"state": {"types": ["m.*"]}}}
filter_id = yield self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
)
)
event = MockEvent(
sender="@foo:bar", type="org.matrix.custom.event", room_id="!foo:bar"
Expand Down Expand Up @@ -465,8 +473,10 @@ def test_filter_rooms(self):
def test_add_filter(self):
user_filter_json = {"room": {"state": {"types": ["m.*"]}}}

filter_id = yield self.filtering.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.filtering.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
)
)

self.assertEquals(filter_id, 0)
Expand All @@ -485,8 +495,10 @@ def test_add_filter(self):
def test_get_filter(self):
user_filter_json = {"room": {"state": {"types": ["m.*"]}}}

filter_id = yield self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
filter_id = yield defer.ensureDeferred(
self.datastore.add_user_filter(
user_localpart=user_localpart, user_filter=user_filter_json
)
)

filter = yield defer.ensureDeferred(
Expand Down
4 changes: 2 additions & 2 deletions tests/crypto/test_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_verify_json_for_server(self):

# should fail immediately on an unsigned object
d = _verify_json_for_server(kr, "server9", {}, 0, "test unsigned")
self.failureResultOf(d, SynapseError)
self.get_failure(d, SynapseError)

# should succeed on a signed object
d = _verify_json_for_server(kr, "server9", json1, 500, "test signed")
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_verify_json_for_server_with_null_valid_until_ms(self):

# should fail immediately on an unsigned object
d = _verify_json_for_server(kr, "server9", {}, 0, "test unsigned")
self.failureResultOf(d, SynapseError)
self.get_failure(d, SynapseError)

# should fail on a signed object with a non-zero minimum_valid_until_ms,
# as it tries to refetch the keys and fails.
Expand Down
6 changes: 2 additions & 4 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

from mock import Mock

from twisted.internet import defer

from synapse.api.errors import Codes, SynapseError
from synapse.rest import admin
from synapse.rest.client.v1 import login, room
Expand Down Expand Up @@ -60,7 +58,7 @@ def test_complexity_simple(self):

# Artificially raise the complexity
store = self.hs.get_datastore()
store.get_current_state_event_counts = lambda x: defer.succeed(500 * 1.23)
store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)

# Get the room complexity again -- make sure it's our artificial value
request, channel = self.make_request(
Expand Down Expand Up @@ -160,7 +158,7 @@ def test_join_too_large_once_joined(self):
)

# Artificially raise the complexity
self.hs.get_datastore().get_current_state_event_counts = lambda x: defer.succeed(
self.hs.get_datastore().get_current_state_event_counts = lambda x: make_awaitable(
600
)

Expand Down
4 changes: 3 additions & 1 deletion tests/handlers/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def get_users_in_room(room_id):
self.datastore.get_new_device_msgs_for_remote = lambda *args, **kargs: make_awaitable(
([], 0)
)
self.datastore.delete_device_msgs_for_remote = lambda *args, **kargs: None
self.datastore.delete_device_msgs_for_remote = lambda *args, **kargs: make_awaitable(
None
)
self.datastore.set_received_txn_response = lambda *args, **kwargs: make_awaitable(
None
)
Expand Down
8 changes: 6 additions & 2 deletions tests/rest/client/v2_alpha/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

from synapse.api.errors import Codes
from synapse.rest.client.v2_alpha import filter

Expand Down Expand Up @@ -73,8 +75,10 @@ def test_add_filter_non_local_user(self):
self.assertEquals(channel.json_body["errcode"], Codes.FORBIDDEN)

def test_get_filter(self):
filter_id = self.filtering.add_user_filter(
user_localpart="apple", user_filter=self.EXAMPLE_FILTER
filter_id = defer.ensureDeferred(
self.filtering.add_user_filter(
user_localpart="apple", user_filter=self.EXAMPLE_FILTER
)
)
self.reactor.advance(1)
filter_id = filter_id.result
Expand Down
24 changes: 18 additions & 6 deletions tests/storage/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ def test_set_appservices_state_multiple_up(self):
def test_create_appservice_txn_first(self):
service = Mock(id=self.as_list[0]["id"])
events = [Mock(event_id="e1"), Mock(event_id="e2")]
txn = yield self.store.create_appservice_txn(service, events)
txn = yield defer.ensureDeferred(
self.store.create_appservice_txn(service, events)
)
self.assertEquals(txn.id, 1)
self.assertEquals(txn.events, events)
self.assertEquals(txn.service, service)
Expand All @@ -255,7 +257,9 @@ def test_create_appservice_txn_older_last_txn(self):
yield self._set_last_txn(service.id, 9643) # AS is falling behind
yield self._insert_txn(service.id, 9644, events)
yield self._insert_txn(service.id, 9645, events)
txn = yield self.store.create_appservice_txn(service, events)
txn = yield defer.ensureDeferred(
self.store.create_appservice_txn(service, events)
)
self.assertEquals(txn.id, 9646)
self.assertEquals(txn.events, events)
self.assertEquals(txn.service, service)
Expand All @@ -265,7 +269,9 @@ def test_create_appservice_txn_up_to_date_last_txn(self):
service = Mock(id=self.as_list[0]["id"])
events = [Mock(event_id="e1"), Mock(event_id="e2")]
yield self._set_last_txn(service.id, 9643)
txn = yield self.store.create_appservice_txn(service, events)
txn = yield defer.ensureDeferred(
self.store.create_appservice_txn(service, events)
)
self.assertEquals(txn.id, 9644)
self.assertEquals(txn.events, events)
self.assertEquals(txn.service, service)
Expand All @@ -286,7 +292,9 @@ def test_create_appservice_txn_up_fuzzing(self):
yield self._insert_txn(self.as_list[2]["id"], 10, events)
yield self._insert_txn(self.as_list[3]["id"], 9643, events)

txn = yield self.store.create_appservice_txn(service, events)
txn = yield defer.ensureDeferred(
self.store.create_appservice_txn(service, events)
)
self.assertEquals(txn.id, 9644)
self.assertEquals(txn.events, events)
self.assertEquals(txn.service, service)
Expand All @@ -298,7 +306,9 @@ def test_complete_appservice_txn_first_txn(self):
txn_id = 1

yield self._insert_txn(service.id, txn_id, events)
yield self.store.complete_appservice_txn(txn_id=txn_id, service=service)
yield defer.ensureDeferred(
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
)

res = yield self.db_pool.runQuery(
self.engine.convert_param_style(
Expand All @@ -324,7 +334,9 @@ def test_complete_appservice_txn_existing_in_state_table(self):
txn_id = 5
yield self._set_last_txn(service.id, 4)
yield self._insert_txn(service.id, txn_id, events)
yield self.store.complete_appservice_txn(txn_id=txn_id, service=service)
yield defer.ensureDeferred(
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
)

res = yield self.db_pool.runQuery(
self.engine.convert_param_style(
Expand Down
9 changes: 3 additions & 6 deletions tests/storage/test_background_update.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from mock import Mock

from twisted.internet import defer

from synapse.storage.background_updates import BackgroundUpdater

from tests import unittest
Expand Down Expand Up @@ -38,11 +36,10 @@ def test_do_background_update(self):
)

# first step: make a bit of progress
@defer.inlineCallbacks
def update(progress, count):
yield self.clock.sleep((count * duration_ms) / 1000)
async def update(progress, count):
await self.clock.sleep((count * duration_ms) / 1000)
progress = {"my_key": progress["my_key"] + 1}
yield store.db_pool.runInteraction(
await store.db_pool.runInteraction(
"update_progress",
self.updates._background_update_progress_txn,
"test_update",
Expand Down
32 changes: 24 additions & 8 deletions tests/storage/test_end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def test_key_without_device_name(self):

yield defer.ensureDeferred(self.store.store_device("user", "device", None))

yield self.store.set_e2e_device_keys("user", "device", now, json)
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)

res = yield defer.ensureDeferred(
self.store.get_e2e_device_keys((("user", "device"),))
Expand All @@ -49,20 +51,26 @@ def test_reupload_key(self):

yield defer.ensureDeferred(self.store.store_device("user", "device", None))

changed = yield self.store.set_e2e_device_keys("user", "device", now, json)
changed = yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)
self.assertTrue(changed)

# If we try to upload the same key then we should be told nothing
# changed
changed = yield self.store.set_e2e_device_keys("user", "device", now, json)
changed = yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)
self.assertFalse(changed)

@defer.inlineCallbacks
def test_get_key_with_device_name(self):
now = 1470174257070
json = {"key": "value"}

yield self.store.set_e2e_device_keys("user", "device", now, json)
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user", "device", now, json)
)
yield defer.ensureDeferred(
self.store.store_device("user", "device", "display_name")
)
Expand All @@ -86,10 +94,18 @@ def test_multiple_devices(self):
yield defer.ensureDeferred(self.store.store_device("user2", "device1", None))
yield defer.ensureDeferred(self.store.store_device("user2", "device2", None))

yield self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
yield self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
yield self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
yield self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user1", "device1", now, {"key": "json11"})
)
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user1", "device2", now, {"key": "json12"})
)
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user2", "device1", now, {"key": "json21"})
)
yield defer.ensureDeferred(
self.store.set_e2e_device_keys("user2", "device2", now, {"key": "json22"})
)

res = yield defer.ensureDeferred(
self.store.get_e2e_device_keys((("user1", "device1"), ("user2", "device2")))
Expand Down
Loading

0 comments on commit e00816a

Please sign in to comment.