@@ -8,13 +8,15 @@ package tests
8
8
9
9
import (
10
10
"bytes"
11
+ "context"
11
12
"fmt"
12
13
"io/ioutil"
13
14
"net/http"
14
15
"net/url"
15
16
"testing"
16
17
"time"
17
18
19
+ "github.com/gorilla/mux"
18
20
"github.com/tidwall/gjson"
19
21
20
22
"github.com/matrix-org/complement/internal/b"
@@ -307,6 +309,102 @@ func TestImportHistoricalMessages(t *testing.T) {
307
309
})
308
310
})
309
311
312
+ t .Run ("Historical events from batch_send do not get pushed out as application service transactions" , func (t * testing.T ) {
313
+ t .Parallel ()
314
+
315
+ // Create a listener and handler to stub an application service listening
316
+ // for transactions from a homeserver.
317
+ handler := mux .NewRouter ()
318
+ // Application Service API: /_matrix/app/v1/transactions/{txnId}
319
+ waiter := NewWaiter ()
320
+ var eventIDsWeSawOverTransactions []string
321
+ var eventIDAfterHistoricalImport string
322
+ handler .HandleFunc ("/transactions/{txnId}" , func (w http.ResponseWriter , req * http.Request ) {
323
+ must .MatchRequest (t , req , match.HTTPRequest {
324
+ JSON : []match.JSON {
325
+ match .JSONArrayEach ("events" , func (r gjson.Result ) error {
326
+ // Add to our running list of events
327
+ eventIDsWeSawOverTransactions = append (eventIDsWeSawOverTransactions , r .Get ("event_id" ).Str )
328
+
329
+ // If we found the event that occurs after our batch send. we can
330
+ // probably safely assume the historical events won't come later.
331
+ if r .Get ("event_id" ).Str != "" && r .Get ("event_id" ).Str == eventIDAfterHistoricalImport {
332
+ defer waiter .Finish ()
333
+ }
334
+
335
+ return nil
336
+ }),
337
+ },
338
+ })
339
+
340
+ // Acknowledge that we've seen the transaction
341
+ w .WriteHeader (200 )
342
+ w .Write ([]byte ("{}" ))
343
+ }).Methods ("PUT" )
344
+
345
+ srv := & http.Server {
346
+ Addr : ":9111" ,
347
+ Handler : handler ,
348
+ }
349
+ go func () {
350
+ srv .ListenAndServe ()
351
+ }()
352
+ defer srv .Shutdown (context .Background ())
353
+ // ----------------------------------------------------------
354
+
355
+ // Create the room all of the action is going to happen in
356
+ roomID := as .CreateRoom (t , createPublicRoomOpts )
357
+ alice .JoinRoom (t , roomID , nil )
358
+
359
+ // Create the "live" event we are going to insert our historical events next to
360
+ eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
361
+ eventIdBefore := eventIDsBefore [0 ]
362
+ timeAfterEventBefore := time .Now ()
363
+
364
+ // Import a historical event
365
+ batchSendRes := batchSendHistoricalMessages (
366
+ t ,
367
+ as ,
368
+ roomID ,
369
+ eventIdBefore ,
370
+ "" ,
371
+ createJoinStateEventsForBatchSendRequest ([]string {virtualUserID }, timeAfterEventBefore ),
372
+ createMessageEventsForBatchSendRequest ([]string {virtualUserID }, timeAfterEventBefore , 1 ),
373
+ // Status
374
+ 200 ,
375
+ )
376
+ batchSendResBody := client .ParseJSON (t , batchSendRes )
377
+ historicalEventIDs := client .GetJSONFieldStringArray (t , batchSendResBody , "event_ids" )
378
+ historicalStateEventIDs := client .GetJSONFieldStringArray (t , batchSendResBody , "state_event_ids" )
379
+
380
+ // This is just a dummy event we search for after the historicalEventIDs/historicalStateEventIDs
381
+ eventIDsAfterHistoricalImport := createMessagesInRoom (t , alice , roomID , 1 )
382
+ eventIDAfterHistoricalImport = eventIDsAfterHistoricalImport [0 ]
383
+
384
+ // Check if eventIDAfterHistoricalImport already came over `/transactions`.
385
+ if ! includes (eventIDAfterHistoricalImport , eventIDsWeSawOverTransactions ) {
386
+ // If not, wait 5 seconds for to see if it happens. The waiter will only
387
+ // resolve if we see eventIDAfterHistoricalImport, otherwise timeout
388
+ waiter .Wait (t , 5 * time .Second )
389
+ }
390
+
391
+ // Now, that we know eventIDAfterHistoricalImport came over /transactions,
392
+ // we can probably safely assume the historical events won't come later.
393
+
394
+ // Check to make sure the historical events didn't come over /transactions
395
+ for _ , historicalEventID := range historicalEventIDs {
396
+ if includes (historicalEventID , eventIDsWeSawOverTransactions ) {
397
+ t .Fatalf ("We should not see the %s historical event come over /transactions but it did" , historicalEventID )
398
+ }
399
+ }
400
+ // Check to make sure the historical state events didn't come over /transactions
401
+ for _ , historicalStateEventID := range historicalStateEventIDs {
402
+ if includes (historicalStateEventID , eventIDsWeSawOverTransactions ) {
403
+ t .Fatalf ("We should not see the %s historical state event come over /transactions but it did" , historicalStateEventID )
404
+ }
405
+ }
406
+ })
407
+
310
408
t .Run ("Batch send endpoint only returns state events that we passed in via state_events_at_start" , func (t * testing.T ) {
311
409
t .Parallel ()
312
410
0 commit comments