mirror of
https://github.com/matrix-org/sliding-sync.git
synced 2025-03-10 13:37:11 +00:00
Assert with context where possible
This commit is contained in:
parent
2da72f9c44
commit
83e746ec3c
@ -390,7 +390,7 @@ func (c *roomUpdateCache) UserRoomMetadata() *UserRoomData {
|
||||
}
|
||||
|
||||
// snapshots the user cache / global cache data for this room for sending to connections
|
||||
func (c *UserCache) newRoomUpdate(roomID string) RoomUpdate {
|
||||
func (c *UserCache) newRoomUpdate(ctx context.Context, roomID string) RoomUpdate {
|
||||
u := c.LoadRoomData(roomID)
|
||||
var r *internal.RoomMetadata
|
||||
globalRooms := c.globalCache.LoadRooms(roomID)
|
||||
@ -404,7 +404,7 @@ func (c *UserCache) newRoomUpdate(roomID string) RoomUpdate {
|
||||
} else {
|
||||
r = globalRooms[roomID]
|
||||
}
|
||||
internal.Assert("missing global room metadata for room "+roomID, r != nil)
|
||||
internal.AssertWithContext(ctx, "missing global room metadata for room "+roomID, r != nil)
|
||||
return &roomUpdateCache{
|
||||
roomID: roomID,
|
||||
globalRoomData: r,
|
||||
@ -479,7 +479,7 @@ func (c *UserCache) OnEphemeralEvent(ctx context.Context, roomID string, ephEven
|
||||
switch evType {
|
||||
case "m.typing":
|
||||
update = &TypingUpdate{
|
||||
RoomUpdate: c.newRoomUpdate(roomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, roomID),
|
||||
}
|
||||
}
|
||||
if update == nil {
|
||||
@ -491,7 +491,7 @@ func (c *UserCache) OnEphemeralEvent(ctx context.Context, roomID string, ephEven
|
||||
|
||||
func (c *UserCache) OnReceipt(ctx context.Context, receipt internal.Receipt) {
|
||||
c.emitOnRoomUpdate(ctx, &ReceiptUpdate{
|
||||
RoomUpdate: c.newRoomUpdate(receipt.RoomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, receipt.RoomID),
|
||||
Receipt: receipt,
|
||||
})
|
||||
}
|
||||
@ -538,7 +538,7 @@ func (c *UserCache) OnUnreadCounts(ctx context.Context, roomID string, highlight
|
||||
c.roomToDataMu.Unlock()
|
||||
|
||||
roomUpdate := &UnreadCountUpdate{
|
||||
RoomUpdate: c.newRoomUpdate(roomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, roomID),
|
||||
HasCountDecreased: hasCountDecreased,
|
||||
}
|
||||
|
||||
@ -562,7 +562,7 @@ func (c *UserCache) OnSpaceUpdate(ctx context.Context, parentRoomID, childRoomID
|
||||
|
||||
// now we need to notify connections for the _child_
|
||||
roomUpdate := &RoomEventUpdate{
|
||||
RoomUpdate: c.newRoomUpdate(childRoomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, childRoomID),
|
||||
EventData: eventData,
|
||||
}
|
||||
|
||||
@ -595,7 +595,7 @@ func (c *UserCache) OnNewEvent(ctx context.Context, eventData *EventData) {
|
||||
c.roomToDataMu.Unlock()
|
||||
|
||||
roomUpdate := &RoomEventUpdate{
|
||||
RoomUpdate: c.newRoomUpdate(eventData.RoomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, eventData.RoomID),
|
||||
EventData: eventData,
|
||||
}
|
||||
|
||||
@ -721,7 +721,7 @@ func (c *UserCache) OnAccountData(ctx context.Context, datas []state.AccountData
|
||||
} else {
|
||||
roomUpdate := &RoomAccountDataUpdate{
|
||||
AccountData: updates,
|
||||
RoomUpdate: c.newRoomUpdate(roomID),
|
||||
RoomUpdate: c.newRoomUpdate(ctx, roomID),
|
||||
}
|
||||
c.emitOnRoomUpdate(ctx, roomUpdate)
|
||||
}
|
||||
|
@ -202,7 +202,7 @@ func (s *ConnState) onIncomingListRequest(ctx context.Context, builder *RoomsBui
|
||||
// this is either a new list or the filters changed, so we need to splat all the rooms to the client.
|
||||
subID := builder.AddSubscription(nextReqList.RoomSubscription)
|
||||
allRoomIDs := roomList.RoomIDs()
|
||||
builder.AddRoomsToSubscription(subID, allRoomIDs)
|
||||
builder.AddRoomsToSubscription(ctx, subID, allRoomIDs)
|
||||
return sync3.ResponseList{
|
||||
// send all the room IDs initially so the user knows which rooms in the top-level rooms map
|
||||
// correspond to this list.
|
||||
@ -252,7 +252,7 @@ func (s *ConnState) onIncomingListRequest(ctx context.Context, builder *RoomsBui
|
||||
roomList, _ = s.lists.AssignList(ctx, listKey, nextReqList.Filters, nextReqList.Sort, sync3.Overwrite)
|
||||
}
|
||||
// resort as either we changed the sort order or we added/removed a bunch of rooms
|
||||
if err := roomList.Sort(nextReqList.Sort); err != nil {
|
||||
if err := roomList.Sort(ctx, nextReqList.Sort); err != nil {
|
||||
logger.Err(err).Str("key", listKey).Msg("cannot sort list")
|
||||
internal.GetSentryHubFromContextOrDefault(ctx).CaptureException(err)
|
||||
}
|
||||
@ -284,7 +284,7 @@ func (s *ConnState) onIncomingListRequest(ctx context.Context, builder *RoomsBui
|
||||
sortableRooms := subslice[0].(*sync3.SortableRooms)
|
||||
roomIDs := sortableRooms.RoomIDs()
|
||||
// the builder will populate this with the right room data
|
||||
builder.AddRoomsToSubscription(subID, roomIDs)
|
||||
builder.AddRoomsToSubscription(ctx, subID, roomIDs)
|
||||
|
||||
responseOperations = append(responseOperations, &sync3.ResponseOpRange{
|
||||
Operation: sync3.OpSync,
|
||||
@ -327,7 +327,7 @@ func (s *ConnState) onIncomingListRequest(ctx context.Context, builder *RoomsBui
|
||||
joinedRoomIDs = append(joinedRoomIDs, roomID)
|
||||
}
|
||||
// the builder will populate this with the right room data
|
||||
builder.AddRoomsToSubscription(newSubID, joinedRoomIDs)
|
||||
builder.AddRoomsToSubscription(ctx, newSubID, joinedRoomIDs)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -373,7 +373,7 @@ func (s *ConnState) buildRoomSubscriptions(ctx context.Context, builder *RoomsBu
|
||||
}
|
||||
s.roomSubscriptions[roomID] = sub
|
||||
subID := builder.AddSubscription(sub)
|
||||
builder.AddRoomsToSubscription(subID, []string{roomID})
|
||||
builder.AddRoomsToSubscription(ctx, subID, []string{roomID})
|
||||
}
|
||||
for _, roomID := range unsubs {
|
||||
delete(s.roomSubscriptions, roomID)
|
||||
@ -534,11 +534,11 @@ func (s *ConnState) OnRoomUpdate(ctx context.Context, up caches.RoomUpdate) {
|
||||
// 0 -> this event was from a 'state' block, do not poke active connections
|
||||
return
|
||||
}
|
||||
internal.Assert("missing global room metadata", update.GlobalRoomMetadata() != nil)
|
||||
internal.AssertWithContext(ctx, "missing global room metadata", update.GlobalRoomMetadata() != nil)
|
||||
internal.Logf(ctx, "connstate", "queued update %d", update.EventData.LatestPos)
|
||||
s.live.onUpdate(update)
|
||||
case caches.RoomUpdate:
|
||||
internal.Assert("missing global room metadata", update.GlobalRoomMetadata() != nil)
|
||||
internal.AssertWithContext(ctx, "missing global room metadata", update.GlobalRoomMetadata() != nil)
|
||||
s.live.onUpdate(update)
|
||||
default:
|
||||
logger.Warn().Str("room_id", up.RoomID()).Msg("OnRoomUpdate unknown update type")
|
||||
|
@ -145,8 +145,8 @@ func (s *connStateLive) lazyLoadTypingMembers(ctx context.Context, response *syn
|
||||
}
|
||||
|
||||
func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update, response *sync3.Response) bool {
|
||||
internal.Assert("processLiveUpdate: response list length != internal list length", s.lists.Len() == len(response.Lists))
|
||||
internal.Assert("processLiveUpdate: request list length != internal list length", s.lists.Len() == len(s.muxedReq.Lists))
|
||||
internal.AssertWithContext(ctx, "processLiveUpdate: response list length != internal list length", s.lists.Len() == len(response.Lists))
|
||||
internal.AssertWithContext(ctx, "processLiveUpdate: request list length != internal list length", s.lists.Len() == len(s.muxedReq.Lists))
|
||||
roomUpdate, _ := up.(caches.RoomUpdate)
|
||||
roomEventUpdate, _ := up.(*caches.RoomEventUpdate)
|
||||
// if this is a room event update we may not want to process this if the event nid is < loadPos,
|
||||
@ -333,7 +333,7 @@ func (s *connStateLive) processLiveUpdateForList(
|
||||
if update.EventData.ForceInitial {
|
||||
// add room to sub: this applies for when we track all rooms too as we want joins/etc to come through with initial data
|
||||
subID := builder.AddSubscription(reqList.RoomSubscription)
|
||||
builder.AddRoomsToSubscription(subID, []string{update.RoomID()})
|
||||
builder.AddRoomsToSubscription(ctx, subID, []string{update.RoomID()})
|
||||
}
|
||||
case *caches.UnreadCountUpdate:
|
||||
logger.Trace().Str("user", s.userID).Str("room", update.RoomID()).Bool("count_decreased", update.HasCountDecreased).Msg("received unread count update")
|
||||
@ -376,7 +376,7 @@ func (s *connStateLive) resort(
|
||||
intList.Add(roomID)
|
||||
// ensure we send data when the user joins a new room
|
||||
subID := builder.AddSubscription(reqList.RoomSubscription)
|
||||
builder.AddRoomsToSubscription(subID, []string{roomID})
|
||||
builder.AddRoomsToSubscription(ctx, subID, []string{roomID})
|
||||
} else if listOp == sync3.ListOpDel {
|
||||
intList.Remove(roomID)
|
||||
}
|
||||
@ -386,7 +386,7 @@ func (s *connStateLive) resort(
|
||||
ops, subs := sync3.CalculateListOps(ctx, reqList, intList, roomID, listOp)
|
||||
if len(subs) > 0 { // handle rooms which have just come into the window
|
||||
subID := builder.AddSubscription(reqList.RoomSubscription)
|
||||
builder.AddRoomsToSubscription(subID, subs)
|
||||
builder.AddRoomsToSubscription(ctx, subID, subs)
|
||||
}
|
||||
|
||||
// there are updates if we have ops, new subs or if the triggering room is inside the range still
|
||||
|
@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
@ -56,8 +57,8 @@ func (rb *RoomsBuilder) AddSubscription(rs sync3.RoomSubscription) (id int) {
|
||||
}
|
||||
|
||||
// Add rooms to the subscription ID previously added. E.g rooms from a list.
|
||||
func (rb *RoomsBuilder) AddRoomsToSubscription(id int, roomIDs []string) {
|
||||
internal.Assert("subscription ID is unknown", id < len(rb.subs))
|
||||
func (rb *RoomsBuilder) AddRoomsToSubscription(ctx context.Context, id int, roomIDs []string) {
|
||||
internal.AssertWithContext(ctx, "subscription ID is unknown", id < len(rb.subs))
|
||||
rb.subToRooms[id] = append(rb.subToRooms[id], roomIDs...)
|
||||
}
|
||||
|
||||
|
@ -258,7 +258,7 @@ func TestRoomsBuilder(t *testing.T) {
|
||||
rb := NewRoomsBuilder()
|
||||
for _, bs := range tc.subsToAdd {
|
||||
id := rb.AddSubscription(bs.RoomSubscription)
|
||||
rb.AddRoomsToSubscription(id, bs.RoomIDs)
|
||||
rb.AddRoomsToSubscription(ctx, id, bs.RoomIDs)
|
||||
}
|
||||
got := rb.BuildSubscriptions()
|
||||
tc.want = sortBuiltSubs(tc.want)
|
||||
|
@ -192,7 +192,7 @@ func (s *InternalRequestLists) AssignList(ctx context.Context, listKey string, f
|
||||
|
||||
roomList := NewFilteredSortableRooms(s, roomIDs, filters)
|
||||
if sort != nil {
|
||||
err := roomList.Sort(sort)
|
||||
err := roomList.Sort(ctx, sort)
|
||||
if err != nil {
|
||||
logger.Err(err).Strs("sort_by", sort).Msg("failed to sort")
|
||||
internal.GetSentryHubFromContextOrDefault(ctx).CaptureException(err)
|
||||
|
@ -83,7 +83,7 @@ func CalculateListOps(ctx context.Context, reqList *RequestList, list List, room
|
||||
listFromIndex := listFromTo[0]
|
||||
listToIndex := listFromTo[1]
|
||||
wasUpdatedRoomInserted := listToIndex == toIndex
|
||||
toRoomID := list.Get(listToIndex)
|
||||
toRoomID := list.Get(ctx, listToIndex)
|
||||
if toRoomID == roomID && listFromIndex == listToIndex && listOp == ListOpChange && wasInsideRange && len(listFromTos) == 1 {
|
||||
// DELETE/INSERT have the same index, we're INSERTing the room that was updated, it was a Change not Add/Delete, it
|
||||
// was previously inside the window AND there's just 1 move operation = it's moving to and from the same index so
|
||||
|
@ -1,6 +1,7 @@
|
||||
package sync3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
@ -51,8 +52,8 @@ func (s *SortableRooms) Add(roomID string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SortableRooms) Get(index int) string {
|
||||
internal.Assert(fmt.Sprintf("index is within len(rooms) %v < %v", index, len(s.roomIDs)), index < len(s.roomIDs))
|
||||
func (s *SortableRooms) Get(ctx context.Context, index int) string {
|
||||
internal.AssertWithContext(ctx, fmt.Sprintf("index is within len(rooms) %v < %v", index, len(s.roomIDs)), index < len(s.roomIDs))
|
||||
return s.roomIDs[index]
|
||||
}
|
||||
|
||||
@ -75,6 +76,8 @@ func (s *SortableRooms) Len() int64 {
|
||||
return int64(len(s.roomIDs))
|
||||
}
|
||||
func (s *SortableRooms) Subslice(i, j int64) Subslicer {
|
||||
// TODO: find a way to plumb a context.Context through to this assert. Maybe as
|
||||
// a property of SortableRooms?
|
||||
internal.Assert("i < j and are within len(rooms)", i < j && i < int64(len(s.roomIDs)) && j <= int64(len(s.roomIDs)))
|
||||
return &SortableRooms{
|
||||
roomIDs: s.roomIDs[i:j],
|
||||
@ -82,8 +85,8 @@ func (s *SortableRooms) Subslice(i, j int64) Subslicer {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SortableRooms) Sort(sortBy []string) error {
|
||||
internal.Assert("sortBy is not empty", len(sortBy) != 0)
|
||||
func (s *SortableRooms) Sort(ctx context.Context, sortBy []string) error {
|
||||
internal.AssertWithContext(ctx, "sortBy is not empty", len(sortBy) != 0)
|
||||
comparators := []func(i, j int) int{}
|
||||
for _, sort := range sortBy {
|
||||
switch sort {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package sync3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -97,7 +98,7 @@ func TestSortBySingleOperation(t *testing.T) {
|
||||
f := newFinder(rooms)
|
||||
sr := NewSortableRooms(f, f.roomIDs)
|
||||
for sortBy, wantOrder := range wantMap {
|
||||
sr.Sort(strings.Split(sortBy, " "))
|
||||
sr.Sort(context.Background(), strings.Split(sortBy, " "))
|
||||
var gotRoomIDs []string
|
||||
for i := range sr.roomIDs {
|
||||
gotRoomIDs = append(gotRoomIDs, sr.roomIDs[i])
|
||||
@ -185,7 +186,7 @@ func TestSortByMultipleOperations(t *testing.T) {
|
||||
f := newFinder(rooms)
|
||||
sr := NewSortableRooms(f, f.roomIDs)
|
||||
for _, tc := range testCases {
|
||||
sr.Sort(tc.SortBy)
|
||||
sr.Sort(context.Background(), tc.SortBy)
|
||||
var gotRoomIDs []string
|
||||
for i := range sr.roomIDs {
|
||||
gotRoomIDs = append(gotRoomIDs, sr.roomIDs[i])
|
||||
@ -228,7 +229,7 @@ func TestSortableRoomsRemove(t *testing.T) {
|
||||
}
|
||||
f := newFinder(rooms)
|
||||
sr := NewSortableRooms(f, f.roomIDs)
|
||||
if err := sr.Sort([]string{SortByRecency}); err != nil { // room 1 is first, then room 2
|
||||
if err := sr.Sort(context.Background(), []string{SortByRecency}); err != nil { // room 1 is first, then room 2
|
||||
t.Fatalf("Sort: %s", err)
|
||||
}
|
||||
if i, ok := sr.IndexOf(room1); i != 0 || !ok {
|
||||
@ -358,7 +359,7 @@ func TestSortByNotificationLevel(t *testing.T) {
|
||||
t.Logf("%v", roomIDs)
|
||||
f := newFinder(rooms)
|
||||
sr := NewSortableRooms(f, roomIDs)
|
||||
if err := sr.Sort([]string{SortByNotificationLevel, SortByRecency}); err != nil {
|
||||
if err := sr.Sort(context.Background(), []string{SortByNotificationLevel, SortByRecency}); err != nil {
|
||||
t.Fatalf("Sort: %s", err)
|
||||
}
|
||||
var gotRoomIDs []string
|
||||
|
Loading…
x
Reference in New Issue
Block a user