From 11c167d24412191a5f1b480e7990770b9464a934 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 16 May 2022 13:49:36 +0200 Subject: [PATCH] Update tests, rename SelectRoomIDs --- roomserver/storage/postgres/rooms_table.go | 2 +- roomserver/storage/shared/storage.go | 2 +- roomserver/storage/sqlite3/rooms_table.go | 2 +- roomserver/storage/tables/interface.go | 2 +- .../storage/tables/room_aliases_table_test.go | 14 ++++++++++++- roomserver/storage/tables/rooms_table_test.go | 20 +++++++++++++++++-- .../storage/tables/state_block_table_test.go | 14 +++++++++++++ .../tables/state_snapshot_table_test.go | 7 +++++++ 8 files changed, 56 insertions(+), 7 deletions(-) diff --git a/roomserver/storage/postgres/rooms_table.go b/roomserver/storage/postgres/rooms_table.go index 0743c4eb3..24362af74 100644 --- a/roomserver/storage/postgres/rooms_table.go +++ b/roomserver/storage/postgres/rooms_table.go @@ -117,7 +117,7 @@ func PrepareRoomsTable(db *sql.DB) (tables.Rooms, error) { }.Prepare(db) } -func (s *roomStatements) SelectRoomIDs(ctx context.Context, txn *sql.Tx) ([]string, error) { +func (s *roomStatements) SelectRoomIDsWithEvents(ctx context.Context, txn *sql.Tx) ([]string, error) { stmt := sqlutil.TxStmt(txn, s.selectRoomIDsStmt) rows, err := stmt.QueryContext(ctx) if err != nil { diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 252e94c7e..cc4a9fff5 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -1216,7 +1216,7 @@ func (d *Database) GetKnownUsers(ctx context.Context, userID, searchString strin // GetKnownRooms returns a list of all rooms we know about. func (d *Database) GetKnownRooms(ctx context.Context) ([]string, error) { - return d.RoomsTable.SelectRoomIDs(ctx, nil) + return d.RoomsTable.SelectRoomIDsWithEvents(ctx, nil) } // ForgetRoom sets a users room to forgotten diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index b334ba1fa..03ad4b3d0 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -108,7 +108,7 @@ func PrepareRoomsTable(db *sql.DB) (tables.Rooms, error) { }.Prepare(db) } -func (s *roomStatements) SelectRoomIDs(ctx context.Context, txn *sql.Tx) ([]string, error) { +func (s *roomStatements) SelectRoomIDsWithEvents(ctx context.Context, txn *sql.Tx) ([]string, error) { stmt := sqlutil.TxStmt(txn, s.selectRoomIDsStmt) rows, err := stmt.QueryContext(ctx) if err != nil { diff --git a/roomserver/storage/tables/interface.go b/roomserver/storage/tables/interface.go index 95609787a..116e11c4e 100644 --- a/roomserver/storage/tables/interface.go +++ b/roomserver/storage/tables/interface.go @@ -72,7 +72,7 @@ type Rooms interface { UpdateLatestEventNIDs(ctx context.Context, txn *sql.Tx, roomNID types.RoomNID, eventNIDs []types.EventNID, lastEventSentNID types.EventNID, stateSnapshotNID types.StateSnapshotNID) error SelectRoomVersionsForRoomNIDs(ctx context.Context, txn *sql.Tx, roomNID []types.RoomNID) (map[types.RoomNID]gomatrixserverlib.RoomVersion, error) SelectRoomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*types.RoomInfo, error) - SelectRoomIDs(ctx context.Context, txn *sql.Tx) ([]string, error) + SelectRoomIDsWithEvents(ctx context.Context, txn *sql.Tx) ([]string, error) BulkSelectRoomIDs(ctx context.Context, txn *sql.Tx, roomNIDs []types.RoomNID) ([]string, error) BulkSelectRoomNIDs(ctx context.Context, txn *sql.Tx, roomIDs []string) ([]types.RoomNID, error) } diff --git a/roomserver/storage/tables/room_aliases_table_test.go b/roomserver/storage/tables/room_aliases_table_test.go index 9dd351f13..8fb57d5a4 100644 --- a/roomserver/storage/tables/room_aliases_table_test.go +++ b/roomserver/storage/tables/room_aliases_table_test.go @@ -38,11 +38,12 @@ func mustCreateRoomAliasesTable(t *testing.T, dbType test.DBType) (tab tables.Ro func TestRoomAliasesTable(t *testing.T) { alice := test.NewUser() room := test.NewRoom(t, alice) + room2 := test.NewRoom(t, alice) ctx := context.Background() test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) { tab, close := mustCreateRoomAliasesTable(t, dbType) defer close() - alias, alias2 := "#alias:localhost", "#alias2:localhost" + alias, alias2, alias3 := "#alias:localhost", "#alias2:localhost", "#alias3:localhost" // insert aliases err := tab.InsertRoomAlias(ctx, nil, alias, room.ID, alice.ID) assert.NoError(t, err) @@ -50,6 +51,9 @@ func TestRoomAliasesTable(t *testing.T) { err = tab.InsertRoomAlias(ctx, nil, alias2, room.ID, alice.ID) assert.NoError(t, err) + err = tab.InsertRoomAlias(ctx, nil, alias3, room2.ID, alice.ID) + assert.NoError(t, err) + // verify we can get the roomID for the alias roomID, err := tab.SelectRoomIDFromAlias(ctx, nil, alias) assert.NoError(t, err) @@ -64,6 +68,10 @@ func TestRoomAliasesTable(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "", creator) + roomID, err = tab.SelectRoomIDFromAlias(ctx, nil, "#doesntexist:localhost") + assert.NoError(t, err) + assert.Equal(t, "", roomID) + // get all aliases for a room aliases, err := tab.SelectAliasesFromRoomID(ctx, nil, room.ID) assert.NoError(t, err) @@ -80,5 +88,9 @@ func TestRoomAliasesTable(t *testing.T) { // deleting the same alias should be a no-op err = tab.DeleteRoomAlias(ctx, nil, alias2) assert.NoError(t, err) + + // Delete non-existent alias should be a no-op + err = tab.DeleteRoomAlias(ctx, nil, "#doesntexist:localhost") + assert.NoError(t, err) }) } diff --git a/roomserver/storage/tables/rooms_table_test.go b/roomserver/storage/tables/rooms_table_test.go index 75a711559..9872fb800 100644 --- a/roomserver/storage/tables/rooms_table_test.go +++ b/roomserver/storage/tables/rooms_table_test.go @@ -11,6 +11,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/test" + "github.com/matrix-org/util" "github.com/stretchr/testify/assert" ) @@ -47,10 +48,19 @@ func TestRoomsTable(t *testing.T) { wantRoomNID, err := tab.InsertRoomNID(ctx, nil, room.ID, room.Version) assert.NoError(t, err) + // Create dummy room + _, err = tab.InsertRoomNID(ctx, nil, util.RandomString(16), room.Version) + assert.NoError(t, err) + gotRoomNID, err := tab.SelectRoomNID(ctx, nil, room.ID) assert.NoError(t, err) assert.Equal(t, wantRoomNID, gotRoomNID) + // Ensure non existent roomNID errors + roomNID, err := tab.SelectRoomNID(ctx, nil, "!doesnotexist:localhost") + assert.Error(t, err) + assert.Equal(t, types.RoomNID(0), roomNID) + roomInfo, err := tab.SelectRoomInfo(ctx, nil, room.ID) assert.NoError(t, err) assert.Equal(t, &types.RoomInfo{ @@ -60,8 +70,12 @@ func TestRoomsTable(t *testing.T) { IsStub: true, // there are no latestEventNIDs }, roomInfo) + roomInfo, err = tab.SelectRoomInfo(ctx, nil, "!doesnotexist:localhost") + assert.NoError(t, err) + assert.Nil(t, roomInfo) + // There are no rooms with latestEventNIDs yet - roomIDs, err := tab.SelectRoomIDs(ctx, nil) + roomIDs, err := tab.SelectRoomIDsWithEvents(ctx, nil) assert.NoError(t, err) assert.Equal(t, 0, len(roomIDs)) @@ -81,8 +95,10 @@ func TestRoomsTable(t *testing.T) { assert.Equal(t, []types.RoomNID{wantRoomNID}, roomNIDs) wantEventNIDs := []types.EventNID{1, 2, 3} + lastEventSentNID := types.EventNID(3) + stateSnapshotNID := types.StateSnapshotNID(1) // make the room "usable" - err = tab.UpdateLatestEventNIDs(ctx, nil, wantRoomNID, wantEventNIDs, 3, 1) + err = tab.UpdateLatestEventNIDs(ctx, nil, wantRoomNID, wantEventNIDs, lastEventSentNID, stateSnapshotNID) assert.NoError(t, err) roomInfo, err = tab.SelectRoomInfo(ctx, nil, room.ID) diff --git a/roomserver/storage/tables/state_block_table_test.go b/roomserver/storage/tables/state_block_table_test.go index 8e6f1956e..de0b420bc 100644 --- a/roomserver/storage/tables/state_block_table_test.go +++ b/roomserver/storage/tables/state_block_table_test.go @@ -74,5 +74,19 @@ func TestStateBlockTable(t *testing.T) { // try to get a StateBlockNID which does not exist _, err = tab.BulkSelectStateBlockEntries(ctx, nil, types.StateBlockNIDs{5}) assert.Error(t, err) + + // This should return an error, since we can only retrieve 1 StateBlock + _, err = tab.BulkSelectStateBlockEntries(ctx, nil, types.StateBlockNIDs{1, 5}) + assert.Error(t, err) + + for i := 0; i < 65555; i++ { + entry := types.StateEntry{ + EventNID: types.EventNID(i), + } + entries2 = append(entries2, entry) + } + stateBlockNID, err = tab.BulkInsertStateData(ctx, nil, entries2) + assert.NoError(t, err) + assert.Equal(t, types.StateBlockNID(3), stateBlockNID) }) } diff --git a/roomserver/storage/tables/state_snapshot_table_test.go b/roomserver/storage/tables/state_snapshot_table_test.go index a05fd6d87..dcdb5d8f1 100644 --- a/roomserver/storage/tables/state_snapshot_table_test.go +++ b/roomserver/storage/tables/state_snapshot_table_test.go @@ -75,5 +75,12 @@ func TestStateSnapshotTable(t *testing.T) { // check we get an error if the state snapshot does not exist _, err = tab.BulkSelectStateBlockNIDs(ctx, nil, []types.StateSnapshotNID{2}) assert.Error(t, err) + + // create a second snapshot + for i := 0; i < 65555; i++ { + stateBlockNIDs2 = append(stateBlockNIDs2, types.StateBlockNID(i)) + } + _, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs2) + assert.NoError(t, err) }) }