18
18
from synapse .api .constants import EventTypes
19
19
from synapse .api .errors import AuthError , Codes , LimitExceededError , SynapseError
20
20
from synapse .api .room_versions import RoomVersions
21
- from synapse .events import EventBase
21
+ from synapse .events import EventBase , make_event_from_dict
22
22
from synapse .federation .federation_base import event_from_pdu_json
23
23
from synapse .logging .context import LoggingContext , run_in_background
24
24
from synapse .rest import admin
25
25
from synapse .rest .client import login , room
26
- from synapse .types import create_requester
27
26
from synapse .util .stringutils import random_string
28
27
29
28
from tests import unittest
29
+ from tests .test_utils import event_injection
30
30
31
31
logger = logging .getLogger (__name__ )
32
32
@@ -35,7 +35,7 @@ def generate_fake_event_id() -> str:
35
35
return "$fake_" + random_string (43 )
36
36
37
37
38
- class FederationTestCase (unittest .HomeserverTestCase ):
38
+ class FederationTestCase (unittest .FederatingHomeserverTestCase ):
39
39
servlets = [
40
40
admin .register_servlets ,
41
41
login .register_servlets ,
@@ -215,41 +215,77 @@ def test_backfill_with_many_backward_extremities(self):
215
215
# create the room
216
216
user_id = self .register_user ("kermit" , "test" )
217
217
tok = self .login ("kermit" , "test" )
218
- requester = create_requester (user_id )
219
218
220
219
room_id = self .helper .create_room_as (room_creator = user_id , tok = tok )
220
+ room_version = self .get_success (self .store .get_room_version (room_id ))
221
+
222
+ # we need a user on the remote server to be a member, so that we can send
223
+ # extremity-causing events.
224
+ self .get_success (
225
+ event_injection .inject_member_event (
226
+ self .hs , room_id , f"@user:{ self .OTHER_SERVER_NAME } " , "join"
227
+ )
228
+ )
221
229
222
- ev1 = self .helper .send (room_id , "first message" , tok = tok )
230
+ send_result = self .helper .send (room_id , "first message" , tok = tok )
231
+ ev1 = self .get_success (
232
+ self .store .get_event (send_result ["event_id" ], allow_none = False )
233
+ )
234
+ current_state = self .get_success (
235
+ self .store .get_events_as_list (
236
+ (self .get_success (self .store .get_current_state_ids (room_id ))).values ()
237
+ )
238
+ )
223
239
224
240
# Create "many" backward extremities. The magic number we're trying to
225
241
# create more than is 5 which corresponds to the number of backward
226
242
# extremities we slice off in `_maybe_backfill_inner`
243
+ federation_event_handler = self .hs .get_federation_event_handler ()
227
244
for _ in range (0 , 8 ):
228
- event_handler = self .hs .get_event_creation_handler ()
229
- event , context = self .get_success (
230
- event_handler .create_event (
231
- requester ,
245
+ event = make_event_from_dict (
246
+ self .add_hashes_and_signatures (
232
247
{
248
+ "origin_server_ts" : 1 ,
233
249
"type" : "m.room.message" ,
234
250
"content" : {
235
251
"msgtype" : "m.text" ,
236
252
"body" : "message connected to fake event" ,
237
253
},
238
254
"room_id" : room_id ,
239
- "sender" : user_id ,
255
+ "sender" : f"@user:{ self .OTHER_SERVER_NAME } " ,
256
+ "prev_events" : [
257
+ ev1 .event_id ,
258
+ # We're creating an backward extremity each time thanks
259
+ # to this fake event
260
+ generate_fake_event_id (),
261
+ ],
262
+ # lazy: *everything* is an auth event
263
+ "auth_events" : [ev .event_id for ev in current_state ],
264
+ "depth" : ev1 .depth + 1 ,
240
265
},
241
- prev_event_ids = [
242
- ev1 ["event_id" ],
243
- # We're creating an backward extremity each time thanks
244
- # to this fake event
245
- generate_fake_event_id (),
246
- ],
247
- )
266
+ room_version ,
267
+ ),
268
+ room_version ,
248
269
)
270
+
271
+ # we poke this directly into _process_received_pdu, to avoid the
272
+ # federation handler wanting to backfill the fake event.
249
273
self .get_success (
250
- event_handler .handle_new_client_event (requester , event , context )
274
+ federation_event_handler ._process_received_pdu (
275
+ self .OTHER_SERVER_NAME , event , state = current_state
276
+ )
251
277
)
252
278
279
+ # we should now have 8 backwards extremities.
280
+ backwards_extremities = self .get_success (
281
+ self .store .db_pool .simple_select_list (
282
+ "event_backward_extremities" ,
283
+ keyvalues = {"room_id" : room_id },
284
+ retcols = ["event_id" ],
285
+ )
286
+ )
287
+ self .assertEqual (len (backwards_extremities ), 8 )
288
+
253
289
current_depth = 1
254
290
limit = 100
255
291
with LoggingContext ("receive_pdu" ):
0 commit comments