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

Commit

Permalink
Update backwards-extremities testcase
Browse files Browse the repository at this point in the history
We can no longer poke in events with missing prevs via the C-S API, so we
rewrite this to do them via the Federation API instead.
  • Loading branch information
richvdh committed Mar 9, 2022
1 parent ec43ad5 commit 63fcd4e
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions tests/handlers/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
from synapse.api.constants import EventTypes
from synapse.api.errors import AuthError, Codes, LimitExceededError, SynapseError
from synapse.api.room_versions import RoomVersions
from synapse.events import EventBase
from synapse.events import EventBase, make_event_from_dict
from synapse.federation.federation_base import event_from_pdu_json
from synapse.logging.context import LoggingContext, run_in_background
from synapse.rest import admin
from synapse.rest.client import login, room
from synapse.types import create_requester
from synapse.util.stringutils import random_string

from tests import unittest
from tests.test_utils import event_injection

logger = logging.getLogger(__name__)

Expand All @@ -35,7 +35,7 @@ def generate_fake_event_id() -> str:
return "$fake_" + random_string(43)


class FederationTestCase(unittest.HomeserverTestCase):
class FederationTestCase(unittest.FederatingHomeserverTestCase):
servlets = [
admin.register_servlets,
login.register_servlets,
Expand Down Expand Up @@ -215,41 +215,77 @@ def test_backfill_with_many_backward_extremities(self):
# create the room
user_id = self.register_user("kermit", "test")
tok = self.login("kermit", "test")
requester = create_requester(user_id)

room_id = self.helper.create_room_as(room_creator=user_id, tok=tok)
room_version = self.get_success(self.store.get_room_version(room_id))

# we need a user on the remote server to be a member, so that we can send
# extremity-causing events.
self.get_success(
event_injection.inject_member_event(
self.hs, room_id, f"@user:{self.OTHER_SERVER_NAME}", "join"
)
)

ev1 = self.helper.send(room_id, "first message", tok=tok)
send_result = self.helper.send(room_id, "first message", tok=tok)
ev1 = self.get_success(
self.store.get_event(send_result["event_id"], allow_none=False)
)
current_state = self.get_success(
self.store.get_events_as_list(
(self.get_success(self.store.get_current_state_ids(room_id))).values()
)
)

# Create "many" backward extremities. The magic number we're trying to
# create more than is 5 which corresponds to the number of backward
# extremities we slice off in `_maybe_backfill_inner`
federation_event_handler = self.hs.get_federation_event_handler()
for _ in range(0, 8):
event_handler = self.hs.get_event_creation_handler()
event, context = self.get_success(
event_handler.create_event(
requester,
event = make_event_from_dict(
self.add_hashes_and_signatures(
{
"origin_server_ts": 1,
"type": "m.room.message",
"content": {
"msgtype": "m.text",
"body": "message connected to fake event",
},
"room_id": room_id,
"sender": user_id,
"sender": f"@user:{self.OTHER_SERVER_NAME}",
"prev_events": [
ev1.event_id,
# We're creating an backward extremity each time thanks
# to this fake event
generate_fake_event_id(),
],
# lazy: *everything* is an auth event
"auth_events": [ev.event_id for ev in current_state],
"depth": ev1.depth + 1,
},
prev_event_ids=[
ev1["event_id"],
# We're creating an backward extremity each time thanks
# to this fake event
generate_fake_event_id(),
],
)
room_version,
),
room_version,
)

# we poke this directly into _process_received_pdu, to avoid the
# federation handler wanting to backfill the fake event.
self.get_success(
event_handler.handle_new_client_event(requester, event, context)
federation_event_handler._process_received_pdu(
self.OTHER_SERVER_NAME, event, state=current_state
)
)

# we should now have 8 backwards extremities.
backwards_extremities = self.get_success(
self.store.db_pool.simple_select_list(
"event_backward_extremities",
keyvalues={"room_id": room_id},
retcols=["event_id"],
)
)
self.assertEqual(len(backwards_extremities), 8)

current_depth = 1
limit = 100
with LoggingContext("receive_pdu"):
Expand Down

0 comments on commit 63fcd4e

Please sign in to comment.