From 22b9f67a18afec4d856cf11d6474290e235c388c Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Fri, 17 Mar 2023 09:23:05 +0100 Subject: [PATCH] Add ConnectionManager test --- internal/httputil/routing_test.go | 4 +- internal/sqlutil/connection_manager_test.go | 56 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 internal/sqlutil/connection_manager_test.go diff --git a/internal/httputil/routing_test.go b/internal/httputil/routing_test.go index 46f245957..21e2bf48a 100644 --- a/internal/httputil/routing_test.go +++ b/internal/httputil/routing_test.go @@ -18,7 +18,7 @@ func TestRoutersError(t *testing.T) { t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String()) } if ct := rec.Header().Get("Content-Type"); ct != "application/json" { - t.Logf("unexpected content-type: %s", ct) + t.Fatalf("unexpected content-type: %s", ct) } // not allowed test @@ -33,6 +33,6 @@ func TestRoutersError(t *testing.T) { t.Fatalf("unexpected status code: %d - %s", rec.Code, rec.Body.String()) } if ct := rec.Header().Get("Content-Type"); ct != "application/json" { - t.Logf("unexpected content-type: %s", ct) + t.Fatalf("unexpected content-type: %s", ct) } } diff --git a/internal/sqlutil/connection_manager_test.go b/internal/sqlutil/connection_manager_test.go new file mode 100644 index 000000000..610629d5e --- /dev/null +++ b/internal/sqlutil/connection_manager_test.go @@ -0,0 +1,56 @@ +package sqlutil_test + +import ( + "reflect" + "testing" + + "github.com/matrix-org/dendrite/internal/sqlutil" + "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/dendrite/test" +) + +func TestConnectionManager(t *testing.T) { + test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) { + conStr, close := test.PrepareDBConnectionString(t, dbType) + t.Cleanup(close) + cm := sqlutil.NewConnectionManager() + + dbProps := &config.DatabaseOptions{ConnectionString: config.DataSource(string(conStr))} + db, writer, err := cm.Connection(dbProps) + if err != nil { + t.Fatal(err) + } + + switch dbType { + case test.DBTypeSQLite: + _, ok := writer.(*sqlutil.ExclusiveWriter) + if !ok { + t.Fatalf("expected exclusive writer") + } + case test.DBTypePostgres: + _, ok := writer.(*sqlutil.DummyWriter) + if !ok { + t.Fatalf("expected dummy writer") + } + } + + // test global db pool + dbGlobal, writerGlobal, err := cm.Connection(&config.DatabaseOptions{}) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(db, dbGlobal) { + t.Fatalf("expected database connection to be reused") + } + if !reflect.DeepEqual(writer, writerGlobal) { + t.Fatalf("expected database writer to be reused") + } + + // test invalid connection string configured + cm = sqlutil.NewConnectionManager() + _, _, err = cm.Connection(&config.DatabaseOptions{ConnectionString: "http://"}) + if err == nil { + t.Fatal("expected an error but got none") + } + }) +}