mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 21:32:46 +00:00
Add roomserver tests (3/4) (#2447)
* Add Room Aliases tests * Add Rooms table test * Move StateKeyTuplerSorter to the types package * Add StateBlock tests Some optimizations * Add State Snapshot tests Some optimization * Return []int64 and convert to pq.Int64Array for postgres * Move []types.EventNID back to rows.Next() * Update tests, rename SelectRoomIDs
This commit is contained in:
parent
6af35385ba
commit
05607d6b87
22 changed files with 570 additions and 313 deletions
|
@ -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)
|
||||
}
|
||||
|
|
96
roomserver/storage/tables/room_aliases_table_test.go
Normal file
96
roomserver/storage/tables/room_aliases_table_test.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func mustCreateRoomAliasesTable(t *testing.T, dbType test.DBType) (tab tables.RoomAliases, close func()) {
|
||||
t.Helper()
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
assert.NoError(t, err)
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
err = postgres.CreateRoomAliasesTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = postgres.PrepareRoomAliasesTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
err = sqlite3.CreateRoomAliasesTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = sqlite3.PrepareRoomAliasesTable(db)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
return tab, close
|
||||
}
|
||||
|
||||
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, alias3 := "#alias:localhost", "#alias2:localhost", "#alias3:localhost"
|
||||
// insert aliases
|
||||
err := tab.InsertRoomAlias(ctx, nil, alias, room.ID, alice.ID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
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)
|
||||
assert.Equal(t, room.ID, roomID)
|
||||
|
||||
// .. and the creator
|
||||
creator, err := tab.SelectCreatorIDFromAlias(ctx, nil, alias)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, alice.ID, creator)
|
||||
|
||||
creator, err = tab.SelectCreatorIDFromAlias(ctx, nil, "#doesntexist:localhost")
|
||||
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)
|
||||
assert.Equal(t, []string{alias, alias2}, aliases)
|
||||
|
||||
// delete an alias and verify it's deleted
|
||||
err = tab.DeleteRoomAlias(ctx, nil, alias2)
|
||||
assert.NoError(t, err)
|
||||
|
||||
aliases, err = tab.SelectAliasesFromRoomID(ctx, nil, room.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{alias}, aliases)
|
||||
|
||||
// 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)
|
||||
})
|
||||
}
|
128
roomserver/storage/tables/rooms_table_test.go
Normal file
128
roomserver/storage/tables/rooms_table_test.go
Normal file
|
@ -0,0 +1,128 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"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"
|
||||
)
|
||||
|
||||
func mustCreateRoomsTable(t *testing.T, dbType test.DBType) (tab tables.Rooms, close func()) {
|
||||
t.Helper()
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
assert.NoError(t, err)
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
err = postgres.CreateRoomsTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = postgres.PrepareRoomsTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
err = sqlite3.CreateRoomsTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = sqlite3.PrepareRoomsTable(db)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
return tab, close
|
||||
}
|
||||
|
||||
func TestRoomsTable(t *testing.T) {
|
||||
alice := test.NewUser()
|
||||
room := test.NewRoom(t, alice)
|
||||
ctx := context.Background()
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
tab, close := mustCreateRoomsTable(t, dbType)
|
||||
defer close()
|
||||
|
||||
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{
|
||||
RoomNID: wantRoomNID,
|
||||
RoomVersion: room.Version,
|
||||
StateSnapshotNID: 0,
|
||||
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.SelectRoomIDsWithEvents(ctx, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(roomIDs))
|
||||
|
||||
roomVersions, err := tab.SelectRoomVersionsForRoomNIDs(ctx, nil, []types.RoomNID{wantRoomNID, 1337})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, roomVersions[wantRoomNID], room.Version)
|
||||
// Room does not exist
|
||||
_, ok := roomVersions[1337]
|
||||
assert.False(t, ok)
|
||||
|
||||
roomIDs, err = tab.BulkSelectRoomIDs(ctx, nil, []types.RoomNID{wantRoomNID, 1337})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{room.ID}, roomIDs)
|
||||
|
||||
roomNIDs, err := tab.BulkSelectRoomNIDs(ctx, nil, []string{room.ID, "!doesnotexist:localhost"})
|
||||
assert.NoError(t, err)
|
||||
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, lastEventSentNID, stateSnapshotNID)
|
||||
assert.NoError(t, err)
|
||||
|
||||
roomInfo, err = tab.SelectRoomInfo(ctx, nil, room.ID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &types.RoomInfo{
|
||||
RoomNID: wantRoomNID,
|
||||
RoomVersion: room.Version,
|
||||
StateSnapshotNID: 1,
|
||||
IsStub: false,
|
||||
}, roomInfo)
|
||||
|
||||
eventNIDs, snapshotNID, err := tab.SelectLatestEventNIDs(ctx, nil, wantRoomNID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, wantEventNIDs, eventNIDs)
|
||||
assert.Equal(t, types.StateSnapshotNID(1), snapshotNID)
|
||||
|
||||
// Again, doesn't exist
|
||||
_, _, err = tab.SelectLatestEventNIDs(ctx, nil, 1337)
|
||||
assert.Error(t, err)
|
||||
|
||||
eventNIDs, eventNID, snapshotNID, err := tab.SelectLatestEventsNIDsForUpdate(ctx, nil, wantRoomNID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, wantEventNIDs, eventNIDs)
|
||||
assert.Equal(t, types.EventNID(3), eventNID)
|
||||
assert.Equal(t, types.StateSnapshotNID(1), snapshotNID)
|
||||
})
|
||||
}
|
92
roomserver/storage/tables/state_block_table_test.go
Normal file
92
roomserver/storage/tables/state_block_table_test.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func mustCreateStateBlockTable(t *testing.T, dbType test.DBType) (tab tables.StateBlock, close func()) {
|
||||
t.Helper()
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
assert.NoError(t, err)
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
err = postgres.CreateStateBlockTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = postgres.PrepareStateBlockTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
err = sqlite3.CreateStateBlockTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = sqlite3.PrepareStateBlockTable(db)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
return tab, close
|
||||
}
|
||||
|
||||
func TestStateBlockTable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
tab, close := mustCreateStateBlockTable(t, dbType)
|
||||
defer close()
|
||||
|
||||
// generate some dummy data
|
||||
var entries types.StateEntries
|
||||
for i := 0; i < 100; i++ {
|
||||
entry := types.StateEntry{
|
||||
EventNID: types.EventNID(i),
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
stateBlockNID, err := tab.BulkInsertStateData(ctx, nil, entries)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, types.StateBlockNID(1), stateBlockNID)
|
||||
|
||||
// generate a different hash, to get a new StateBlockNID
|
||||
var entries2 types.StateEntries
|
||||
for i := 100; i < 300; 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(2), stateBlockNID)
|
||||
|
||||
eventNIDs, err := tab.BulkSelectStateBlockEntries(ctx, nil, types.StateBlockNIDs{1, 2})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(entries), len(eventNIDs[0]))
|
||||
assert.Equal(t, len(entries2), len(eventNIDs[1]))
|
||||
|
||||
// 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)
|
||||
})
|
||||
}
|
86
roomserver/storage/tables/state_snapshot_table_test.go
Normal file
86
roomserver/storage/tables/state_snapshot_table_test.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func mustCreateStateSnapshotTable(t *testing.T, dbType test.DBType) (tab tables.StateSnapshot, close func()) {
|
||||
t.Helper()
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
assert.NoError(t, err)
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
err = postgres.CreateStateSnapshotTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = postgres.PrepareStateSnapshotTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
err = sqlite3.CreateStateSnapshotTable(db)
|
||||
assert.NoError(t, err)
|
||||
tab, err = sqlite3.PrepareStateSnapshotTable(db)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
return tab, close
|
||||
}
|
||||
|
||||
func TestStateSnapshotTable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
tab, close := mustCreateStateSnapshotTable(t, dbType)
|
||||
defer close()
|
||||
|
||||
// generate some dummy data
|
||||
var stateBlockNIDs types.StateBlockNIDs
|
||||
for i := 0; i < 100; i++ {
|
||||
stateBlockNIDs = append(stateBlockNIDs, types.StateBlockNID(i))
|
||||
}
|
||||
stateNID, err := tab.InsertState(ctx, nil, 1, stateBlockNIDs)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, types.StateSnapshotNID(1), stateNID)
|
||||
|
||||
// verify ON CONFLICT; Note: this updates the sequence!
|
||||
stateNID, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, types.StateSnapshotNID(1), stateNID)
|
||||
|
||||
// create a second snapshot
|
||||
var stateBlockNIDs2 types.StateBlockNIDs
|
||||
for i := 100; i < 150; i++ {
|
||||
stateBlockNIDs2 = append(stateBlockNIDs2, types.StateBlockNID(i))
|
||||
}
|
||||
|
||||
stateNID, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs2)
|
||||
assert.NoError(t, err)
|
||||
// StateSnapshotNID is now 3, since the DO UPDATE SET statement incremented the sequence
|
||||
assert.Equal(t, types.StateSnapshotNID(3), stateNID)
|
||||
|
||||
nidLists, err := tab.BulkSelectStateBlockNIDs(ctx, nil, []types.StateSnapshotNID{1, 3})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, stateBlockNIDs, types.StateBlockNIDs(nidLists[0].StateBlockNIDs))
|
||||
assert.Equal(t, stateBlockNIDs2, types.StateBlockNIDs(nidLists[1].StateBlockNIDs))
|
||||
|
||||
// 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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue