Fix bug with nil interface return & add test

This commit is contained in:
Devon Hudson 2023-05-18 13:41:47 -06:00
parent 345f025ee3
commit 027a9b8ce0
No known key found for this signature in database
GPG Key ID: CD06B18E77F6A628
2 changed files with 38 additions and 1 deletions

View File

@ -868,7 +868,11 @@ func (r *Queryer) QueryRoomInfo(ctx context.Context, roomID spec.RoomID) (*types
} }
func (r *Queryer) CurrentStateEvent(ctx context.Context, roomID spec.RoomID, eventType string, stateKey string) (gomatrixserverlib.PDU, error) { func (r *Queryer) CurrentStateEvent(ctx context.Context, roomID spec.RoomID, eventType string, stateKey string) (gomatrixserverlib.PDU, error) {
return r.DB.GetStateEvent(ctx, roomID.String(), string(eventType), "") res, err := r.DB.GetStateEvent(ctx, roomID.String(), string(eventType), "")
if res == nil {
return nil, err
}
return res, err
} }
func (r *Queryer) UserJoinedToRoom(ctx context.Context, roomNID types.RoomNID, userID spec.UserID) (bool, error) { func (r *Queryer) UserJoinedToRoom(ctx context.Context, roomNID types.RoomNID, userID spec.UserID) (bool, error) {

View File

@ -18,10 +18,16 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"testing" "testing"
"time"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/roomserver/storage"
"github.com/matrix-org/dendrite/roomserver/types" "github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/test" "github.com/matrix-org/dendrite/test"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
) )
// used to implement RoomserverInternalAPIEventDB to test getAuthChain // used to implement RoomserverInternalAPIEventDB to test getAuthChain
@ -155,3 +161,30 @@ func TestGetAuthChainMultiple(t *testing.T) {
t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs) t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs)
} }
} }
func mustCreateDatabase(t *testing.T, dbType test.DBType) (storage.Database, func()) {
conStr, close := test.PrepareDBConnectionString(t, dbType)
caches := caching.NewRistrettoCache(8*1024*1024, time.Hour, caching.DisableMetrics)
cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
db, err := storage.Open(context.Background(), cm, &config.DatabaseOptions{ConnectionString: config.DataSource(conStr)}, caches)
if err != nil {
t.Fatalf("failed to create Database: %v", err)
}
return db, close
}
func TestCurrentEventIsNil(t *testing.T) {
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
db, close := mustCreateDatabase(t, dbType)
defer close()
querier := Queryer{
DB: db,
}
roomID, _ := spec.NewRoomID("!room:server")
event, _ := querier.CurrentStateEvent(context.Background(), *roomID, spec.MRoomMember, "@user:server")
if event != nil {
t.Fatal("Event should equal nil, most likely this is failing because the interface type is not nil, but the value is.")
}
})
}