Skip to content

Commit

Permalink
Make sure historical state events don't come down incremental /sync
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Nov 23, 2021
1 parent 10d9f2e commit 6cf9986
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions tests/msc2716_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestImportHistoricalMessages(t *testing.T) {
validateBatchSendRes(t, as, roomID, batchSendRes, false)
})

t.Run("Historical events from /batch_send do not come down in an incremental sync", func(t *testing.T) {
t.Run("Historical events from batch_send do not come down in an incremental sync", func(t *testing.T) {
t.Parallel()

roomID := as.CreateRoom(t, createPublicRoomOpts)
Expand All @@ -234,6 +234,10 @@ func TestImportHistoricalMessages(t *testing.T) {
// Create some "live" events to saturate and fill up the /sync response
createMessagesInRoom(t, alice, roomID, 5)

// Get a /sync `since` pagination token we can try paginating from later
// on
since := doInitialSync(t, alice)

// Import a historical event
batchSendRes := batchSendHistoricalMessages(
t,
Expand All @@ -248,19 +252,20 @@ func TestImportHistoricalMessages(t *testing.T) {
)
batchSendResBody := client.ParseJSON(t, batchSendRes)
historicalEventIDs := client.GetJSONFieldStringArray(t, batchSendResBody, "event_ids")
historicalEventId := historicalEventIDs[0]
historicalStateEventIDs := client.GetJSONFieldStringArray(t, batchSendResBody, "state_event_ids")

// This is just a dummy event we search for after the historicalEventId
// This is just a dummy event we search for after the historicalEventIDs/historicalStateEventIDs
eventIDsAfterHistoricalImport := createMessagesInRoom(t, alice, roomID, 1)
eventIDAfterHistoricalImport := eventIDsAfterHistoricalImport[0]

// Sync until we find the eventIDAfterHistoricalImport.
// If we're able to see the eventIDAfterHistoricalImport that occurs after
// the historicalEventId without seeing eventIDAfterHistoricalImport in
// between, we're probably safe to assume it won't sync
alice.SyncUntil(t, "", `{ "room": { "timeline": { "limit": 3 } } }`, "rooms.join."+client.GjsonEscape(roomID)+".timeline.events", func(r gjson.Result) bool {
if r.Get("event_id").Str == historicalEventId {
t.Fatalf("We should not see the %s historical event in /sync response but it was present", historicalEventId)
// Sync from before we did any batch sending until we find the
// eventIDAfterHistoricalImport. If we're able to see
// eventIDAfterHistoricalImport without any the
// historicalEventIDs/historicalStateEventIDs in between, we're probably
// safe to assume it won't sync.
alice.SyncUntil(t, since, "", "rooms.join."+client.GjsonEscape(roomID)+".timeline.events", func(r gjson.Result) bool {
if includes(r.Get("event_id").Str, historicalEventIDs) || includes(r.Get("event_id").Str, historicalStateEventIDs) {
t.Fatalf("We should not see the %s historical event in /sync response but it was present", r.Get("event_id").Str)
}

return r.Get("event_id").Str == eventIDAfterHistoricalImport
Expand Down Expand Up @@ -992,6 +997,17 @@ func reversed(in []string) []string {
return out
}

// Find a given "needle" string in a list of strings, the haystack
func includes(needle string, haystack []string) bool {
for _, item := range haystack {
if needle == item {
return true
}
}

return false
}

func fetchUntilMessagesResponseHas(t *testing.T, c *client.CSAPI, roomID string, check func(gjson.Result) bool) {
t.Helper()
start := time.Now()
Expand Down

0 comments on commit 6cf9986

Please sign in to comment.