mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Add federation peeking table tests (#2920)
As the title says, adds tests for inbound/outbound peeking federation table tests. Also removes some unused code
This commit is contained in:
parent
76db8e90de
commit
d3db542fbf
16 changed files with 503 additions and 217 deletions
148
federationapi/storage/tables/inbound_peeks_table_test.go
Normal file
148
federationapi/storage/tables/inbound_peeks_table_test.go
Normal file
|
@ -0,0 +1,148 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/tables"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
func mustCreateInboundpeeksTable(t *testing.T, dbType test.DBType) (tables.FederationInboundPeeks, func()) {
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open database: %s", err)
|
||||
}
|
||||
var tab tables.FederationInboundPeeks
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
tab, err = postgres.NewPostgresInboundPeeksTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
tab, err = sqlite3.NewSQLiteInboundPeeksTable(db)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create table: %s", err)
|
||||
}
|
||||
return tab, close
|
||||
}
|
||||
|
||||
func TestInboundPeeksTable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
alice := test.NewUser(t)
|
||||
room := test.NewRoom(t, alice)
|
||||
_, serverName, _ := gomatrixserverlib.SplitID('@', alice.ID)
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
tab, closeDB := mustCreateInboundpeeksTable(t, dbType)
|
||||
defer closeDB()
|
||||
|
||||
// Insert a peek
|
||||
peekID := util.RandomString(8)
|
||||
var renewalInterval int64 = 1000
|
||||
if err := tab.InsertInboundPeek(ctx, nil, serverName, room.ID, peekID, renewalInterval); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// select the newly inserted peek
|
||||
inboundPeek1, err := tab.SelectInboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Assert fields are set as expected
|
||||
if inboundPeek1.PeekID != peekID {
|
||||
t.Fatalf("unexpected inbound peek ID: %s, want %s", inboundPeek1.PeekID, peekID)
|
||||
}
|
||||
if inboundPeek1.RoomID != room.ID {
|
||||
t.Fatalf("unexpected inbound peek room ID: %s, want %s", inboundPeek1.RoomID, peekID)
|
||||
}
|
||||
if inboundPeek1.ServerName != serverName {
|
||||
t.Fatalf("unexpected inbound peek servername: %s, want %s", inboundPeek1.ServerName, serverName)
|
||||
}
|
||||
if inboundPeek1.RenewalInterval != renewalInterval {
|
||||
t.Fatalf("unexpected inbound peek renewal interval: %d, want %d", inboundPeek1.RenewalInterval, renewalInterval)
|
||||
}
|
||||
|
||||
// Renew the peek
|
||||
if err = tab.RenewInboundPeek(ctx, nil, serverName, room.ID, peekID, 2000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// verify the values changed
|
||||
inboundPeek2, err := tab.SelectInboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.DeepEqual(inboundPeek1, inboundPeek2) {
|
||||
t.Fatal("expected a change peek, but they are the same")
|
||||
}
|
||||
if inboundPeek1.ServerName != inboundPeek2.ServerName {
|
||||
t.Fatalf("unexpected servername change: %s -> %s", inboundPeek1.ServerName, inboundPeek2.ServerName)
|
||||
}
|
||||
if inboundPeek1.RoomID != inboundPeek2.RoomID {
|
||||
t.Fatalf("unexpected roomID change: %s -> %s", inboundPeek1.RoomID, inboundPeek2.RoomID)
|
||||
}
|
||||
|
||||
// delete the peek
|
||||
if err = tab.DeleteInboundPeek(ctx, nil, serverName, room.ID, peekID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// There should be no peek anymore
|
||||
peek, err := tab.SelectInboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if peek != nil {
|
||||
t.Fatalf("got a peek which should be deleted: %+v", peek)
|
||||
}
|
||||
|
||||
// insert some peeks
|
||||
var peekIDs []string
|
||||
for i := 0; i < 5; i++ {
|
||||
peekID = util.RandomString(8)
|
||||
if err = tab.InsertInboundPeek(ctx, nil, serverName, room.ID, peekID, 1000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
peekIDs = append(peekIDs, peekID)
|
||||
}
|
||||
|
||||
// Now select them
|
||||
inboundPeeks, err := tab.SelectInboundPeeks(ctx, nil, room.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(inboundPeeks) != len(peekIDs) {
|
||||
t.Fatalf("inserted %d peeks, selected %d", len(peekIDs), len(inboundPeeks))
|
||||
}
|
||||
for i := range inboundPeeks {
|
||||
if inboundPeeks[i].PeekID != peekIDs[i] {
|
||||
t.Fatalf("")
|
||||
}
|
||||
}
|
||||
|
||||
// And delete them again
|
||||
if err = tab.DeleteInboundPeeks(ctx, nil, room.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// they should be gone now
|
||||
inboundPeeks, err = tab.SelectInboundPeeks(ctx, nil, room.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(inboundPeeks) > 0 {
|
||||
t.Fatal("got inbound peeks which should be deleted")
|
||||
}
|
||||
|
||||
})
|
||||
}
|
|
@ -28,7 +28,6 @@ type FederationQueuePDUs interface {
|
|||
InsertQueuePDU(ctx context.Context, txn *sql.Tx, transactionID gomatrixserverlib.TransactionID, serverName gomatrixserverlib.ServerName, nid int64) error
|
||||
DeleteQueuePDUs(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName, jsonNIDs []int64) error
|
||||
SelectQueuePDUReferenceJSONCount(ctx context.Context, txn *sql.Tx, jsonNID int64) (int64, error)
|
||||
SelectQueuePDUCount(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName) (int64, error)
|
||||
SelectQueuePDUs(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName, limit int) ([]int64, error)
|
||||
SelectQueuePDUServerNames(ctx context.Context, txn *sql.Tx) ([]gomatrixserverlib.ServerName, error)
|
||||
}
|
||||
|
@ -38,7 +37,6 @@ type FederationQueueEDUs interface {
|
|||
DeleteQueueEDUs(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName, jsonNIDs []int64) error
|
||||
SelectQueueEDUs(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName, limit int) ([]int64, error)
|
||||
SelectQueueEDUReferenceJSONCount(ctx context.Context, txn *sql.Tx, jsonNID int64) (int64, error)
|
||||
SelectQueueEDUCount(ctx context.Context, txn *sql.Tx, serverName gomatrixserverlib.ServerName) (int64, error)
|
||||
SelectQueueEDUServerNames(ctx context.Context, txn *sql.Tx) ([]gomatrixserverlib.ServerName, error)
|
||||
SelectExpiredEDUs(ctx context.Context, txn *sql.Tx, expiredBefore gomatrixserverlib.Timestamp) ([]int64, error)
|
||||
DeleteExpiredEDUs(ctx context.Context, txn *sql.Tx, expiredBefore gomatrixserverlib.Timestamp) error
|
||||
|
|
147
federationapi/storage/tables/outbound_peeks_table_test.go
Normal file
147
federationapi/storage/tables/outbound_peeks_table_test.go
Normal file
|
@ -0,0 +1,147 @@
|
|||
package tables_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/postgres"
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/sqlite3"
|
||||
"github.com/matrix-org/dendrite/federationapi/storage/tables"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
func mustCreateOutboundpeeksTable(t *testing.T, dbType test.DBType) (tables.FederationOutboundPeeks, func()) {
|
||||
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||
db, err := sqlutil.Open(&config.DatabaseOptions{
|
||||
ConnectionString: config.DataSource(connStr),
|
||||
}, sqlutil.NewExclusiveWriter())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open database: %s", err)
|
||||
}
|
||||
var tab tables.FederationOutboundPeeks
|
||||
switch dbType {
|
||||
case test.DBTypePostgres:
|
||||
tab, err = postgres.NewPostgresOutboundPeeksTable(db)
|
||||
case test.DBTypeSQLite:
|
||||
tab, err = sqlite3.NewSQLiteOutboundPeeksTable(db)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create table: %s", err)
|
||||
}
|
||||
return tab, close
|
||||
}
|
||||
|
||||
func TestOutboundPeeksTable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
alice := test.NewUser(t)
|
||||
room := test.NewRoom(t, alice)
|
||||
_, serverName, _ := gomatrixserverlib.SplitID('@', alice.ID)
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
tab, closeDB := mustCreateOutboundpeeksTable(t, dbType)
|
||||
defer closeDB()
|
||||
|
||||
// Insert a peek
|
||||
peekID := util.RandomString(8)
|
||||
var renewalInterval int64 = 1000
|
||||
if err := tab.InsertOutboundPeek(ctx, nil, serverName, room.ID, peekID, renewalInterval); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// select the newly inserted peek
|
||||
outboundPeek1, err := tab.SelectOutboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Assert fields are set as expected
|
||||
if outboundPeek1.PeekID != peekID {
|
||||
t.Fatalf("unexpected outbound peek ID: %s, want %s", outboundPeek1.PeekID, peekID)
|
||||
}
|
||||
if outboundPeek1.RoomID != room.ID {
|
||||
t.Fatalf("unexpected outbound peek room ID: %s, want %s", outboundPeek1.RoomID, peekID)
|
||||
}
|
||||
if outboundPeek1.ServerName != serverName {
|
||||
t.Fatalf("unexpected outbound peek servername: %s, want %s", outboundPeek1.ServerName, serverName)
|
||||
}
|
||||
if outboundPeek1.RenewalInterval != renewalInterval {
|
||||
t.Fatalf("unexpected outbound peek renewal interval: %d, want %d", outboundPeek1.RenewalInterval, renewalInterval)
|
||||
}
|
||||
|
||||
// Renew the peek
|
||||
if err = tab.RenewOutboundPeek(ctx, nil, serverName, room.ID, peekID, 2000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// verify the values changed
|
||||
outboundPeek2, err := tab.SelectOutboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.DeepEqual(outboundPeek1, outboundPeek2) {
|
||||
t.Fatal("expected a change peek, but they are the same")
|
||||
}
|
||||
if outboundPeek1.ServerName != outboundPeek2.ServerName {
|
||||
t.Fatalf("unexpected servername change: %s -> %s", outboundPeek1.ServerName, outboundPeek2.ServerName)
|
||||
}
|
||||
if outboundPeek1.RoomID != outboundPeek2.RoomID {
|
||||
t.Fatalf("unexpected roomID change: %s -> %s", outboundPeek1.RoomID, outboundPeek2.RoomID)
|
||||
}
|
||||
|
||||
// delete the peek
|
||||
if err = tab.DeleteOutboundPeek(ctx, nil, serverName, room.ID, peekID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// There should be no peek anymore
|
||||
peek, err := tab.SelectOutboundPeek(ctx, nil, serverName, room.ID, peekID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if peek != nil {
|
||||
t.Fatalf("got a peek which should be deleted: %+v", peek)
|
||||
}
|
||||
|
||||
// insert some peeks
|
||||
var peekIDs []string
|
||||
for i := 0; i < 5; i++ {
|
||||
peekID = util.RandomString(8)
|
||||
if err = tab.InsertOutboundPeek(ctx, nil, serverName, room.ID, peekID, 1000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
peekIDs = append(peekIDs, peekID)
|
||||
}
|
||||
|
||||
// Now select them
|
||||
outboundPeeks, err := tab.SelectOutboundPeeks(ctx, nil, room.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(outboundPeeks) != len(peekIDs) {
|
||||
t.Fatalf("inserted %d peeks, selected %d", len(peekIDs), len(outboundPeeks))
|
||||
}
|
||||
for i := range outboundPeeks {
|
||||
if outboundPeeks[i].PeekID != peekIDs[i] {
|
||||
t.Fatalf("")
|
||||
}
|
||||
}
|
||||
|
||||
// And delete them again
|
||||
if err = tab.DeleteOutboundPeeks(ctx, nil, room.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// they should be gone now
|
||||
outboundPeeks, err = tab.SelectOutboundPeeks(ctx, nil, room.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(outboundPeeks) > 0 {
|
||||
t.Fatal("got outbound peeks which should be deleted")
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue