CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Implements PLAN.md Phase 10. Simplified Sliding Sync (MSC4186) is served at the unstable org.matrix.simplified_msc3575 endpoint and advertised in /versions. Lists are sorted by activity and filtered by DM, invite, encryption, room type, space and tag; room subscriptions, required_state with wildcards, $ME and $LAZY, and the to_device, e2ee, account_data, receipts and typing extensions are supported. Per-connection state is kept in memory and an unknown pos answers M_UNKNOWN_POS. Threads and relations: m.relates_to is indexed on roomserver_events (migration 0012), /relations (with recursion) and /threads are served, and every event a timeline read returns carries its m.thread, m.replace and m.reference summaries in unsigned.m.relations. Notifications carry a thread ID (migration 0013): threaded receipts clear only their thread, and sync reports unread_thread_notifications when the filter asks. Spaces: /hierarchy walks a space breadth first, asking other servers about rooms this one is not in through the new federation hierarchy endpoint; room_summary describes one room. /search runs on FTS5 (sqlite, pocketbase) or tsvector (postgres), bounded to what each user may see, with context, profiles, state, grouping and paging. The PocketBase driver is real: the sqlite schema and store on an embedded PocketBase app's database, with media in its filesystem, local or S3 (database.pocketbase.s3). It runs the full storage test suite. Ops: OpenTelemetry tracing over OTLP/HTTP (tracing.*), with server spans per route, component spans, and trace propagation on federation, push and application service calls. New metrics for persisted events, waiting syncs, sliding sync connections and the database pool, with a Grafana dashboard. deploy/ adds a Compose setup (PostgreSQL, Prometheus, Grafana, Jaeger) and a Helm chart; cmd/warp-loadtest loads a server and reports request and delivery latencies. The load tester found a long-standing bug, fixed here: on a server where no account data had been written, every incremental /sync re-sent the push rules and returned at once, and an all-zero token was taken for an initial sync, so long polls became busy loops. The PostgreSQL migrations and search dialect compile but were not run against a live PostgreSQL in this change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
198 lines
5.9 KiB
Go
198 lines
5.9 KiB
Go
package storage_test
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/dariuszjaniak/warp/server/internal/storage"
|
|
_ "github.com/dariuszjaniak/warp/server/internal/storage/drivers"
|
|
)
|
|
|
|
// openTestDB opens each driver that can run without external services. The
|
|
// postgres driver joins in when WARP_TEST_POSTGRES_DSN is set, which is how
|
|
// CI runs the same tests against both engines.
|
|
func openTestDB(t *testing.T, driver string) storage.Database {
|
|
t.Helper()
|
|
|
|
cfg := storage.Config{Driver: driver, MaxOpenConns: 4, MaxIdleConns: 2}
|
|
switch driver {
|
|
case storage.DriverSQLite:
|
|
path := filepath.ToSlash(filepath.Join(t.TempDir(), "warp.db"))
|
|
cfg.ConnectionString = "file:" + path + "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
|
|
case storage.DriverPostgres:
|
|
dsn := os.Getenv("WARP_TEST_POSTGRES_DSN")
|
|
if dsn == "" {
|
|
t.Skip("set WARP_TEST_POSTGRES_DSN to test the postgres driver")
|
|
}
|
|
cfg.ConnectionString = dsn
|
|
case storage.DriverPocketBase:
|
|
cfg.ConnectionString = filepath.Join(t.TempDir(), "pb_data")
|
|
default:
|
|
t.Fatalf("no test setup for driver %q", driver)
|
|
}
|
|
|
|
db, err := storage.Open(t.Context(), cfg, nil)
|
|
if err != nil {
|
|
t.Fatalf("Open(%s): %v", driver, err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
return db
|
|
}
|
|
|
|
// testDrivers are run by every storage test. PocketBase is here because
|
|
// parity with sqlite is its whole contract.
|
|
var testDrivers = []string{storage.DriverSQLite, storage.DriverPostgres, storage.DriverPocketBase}
|
|
|
|
func TestDriversRegistered(t *testing.T) {
|
|
got := storage.Drivers()
|
|
for _, want := range []string{storage.DriverSQLite, storage.DriverPostgres, storage.DriverPocketBase} {
|
|
if !slices.Contains(got, want) {
|
|
t.Errorf("driver %q is not registered (have %v)", want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenUnknownDriver(t *testing.T) {
|
|
_, err := storage.Open(t.Context(), storage.Config{Driver: "mysql"}, nil)
|
|
if err == nil {
|
|
t.Fatal("expected an error for an unknown driver")
|
|
}
|
|
}
|
|
|
|
func TestPocketBaseNeedsADataDirectory(t *testing.T) {
|
|
if _, err := storage.Open(t.Context(), storage.Config{Driver: storage.DriverPocketBase}, nil); err == nil {
|
|
t.Fatal("expected an error for a pocketbase driver with no data directory")
|
|
}
|
|
}
|
|
|
|
func TestPocketBaseKeepsMediaInItsFilesystem(t *testing.T) {
|
|
db := openTestDB(t, storage.DriverPocketBase)
|
|
provider, ok := db.(storage.BlobProvider)
|
|
if !ok {
|
|
t.Fatal("the pocketbase driver does not provide its own blob store")
|
|
}
|
|
blobs := provider.Blobs()
|
|
ctx := t.Context()
|
|
|
|
info, err := blobs.Put(ctx, strings.NewReader("hello media"), 1024)
|
|
if err != nil {
|
|
t.Fatalf("Put: %v", err)
|
|
}
|
|
if info.Size != 11 || len(info.SHA256) != 64 {
|
|
t.Fatalf("Put returned %+v", info)
|
|
}
|
|
if again, err := blobs.Put(ctx, strings.NewReader("hello media"), 1024); err != nil || again != info {
|
|
t.Errorf("the same content again = %+v, %v", again, err)
|
|
}
|
|
if _, err := blobs.Put(ctx, strings.NewReader("too long"), 3); !errors.Is(err, storage.ErrBlobTooLarge) {
|
|
t.Errorf("an oversize Put = %v, want ErrBlobTooLarge", err)
|
|
}
|
|
|
|
r, err := blobs.Open(ctx, info.SHA256)
|
|
if err != nil {
|
|
t.Fatalf("Open: %v", err)
|
|
}
|
|
body, err := io.ReadAll(r)
|
|
_ = r.Close()
|
|
if err != nil || string(body) != "hello media" {
|
|
t.Errorf("read back %q, %v", body, err)
|
|
}
|
|
|
|
if err := blobs.Delete(ctx, info.SHA256); err != nil {
|
|
t.Fatalf("Delete: %v", err)
|
|
}
|
|
if err := blobs.Delete(ctx, info.SHA256); err != nil {
|
|
t.Errorf("deleting twice = %v", err)
|
|
}
|
|
if _, err := blobs.Open(ctx, info.SHA256); !errors.Is(err, storage.ErrNoSuchBlob) {
|
|
t.Errorf("Open after Delete = %v, want ErrNoSuchBlob", err)
|
|
}
|
|
if _, err := blobs.Open(ctx, "../../etc/passwd"); !errors.Is(err, storage.ErrNoSuchBlob) {
|
|
t.Errorf("Open of a non-hash = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMigrateAndSettings(t *testing.T) {
|
|
for _, driver := range testDrivers {
|
|
t.Run(driver, func(t *testing.T) {
|
|
ctx := t.Context()
|
|
db := openTestDB(t, driver)
|
|
|
|
pending, err := db.PendingMigrations(ctx)
|
|
if err != nil {
|
|
t.Fatalf("PendingMigrations: %v", err)
|
|
}
|
|
if len(pending) == 0 {
|
|
t.Skip("database already migrated by a previous run")
|
|
}
|
|
|
|
if err := db.Migrate(ctx); err != nil {
|
|
t.Fatalf("Migrate: %v", err)
|
|
}
|
|
// Migrating twice must be a no-op, not an error: it runs on
|
|
// every boot.
|
|
if err := db.Migrate(ctx); err != nil {
|
|
t.Fatalf("second Migrate: %v", err)
|
|
}
|
|
pending, err = db.PendingMigrations(ctx)
|
|
if err != nil {
|
|
t.Fatalf("PendingMigrations: %v", err)
|
|
}
|
|
if len(pending) != 0 {
|
|
t.Errorf("after Migrate, still pending: %v", pending)
|
|
}
|
|
|
|
settings := db.Settings()
|
|
if _, found, err := settings.Get(ctx, "federation.enabled"); err != nil {
|
|
t.Fatalf("Get: %v", err)
|
|
} else if found {
|
|
t.Fatal("expected no stored value on a fresh database")
|
|
}
|
|
|
|
if err := settings.Set(ctx, "federation.enabled", "true"); err != nil {
|
|
t.Fatalf("Set: %v", err)
|
|
}
|
|
if err := settings.Set(ctx, "federation.enabled", "false"); err != nil {
|
|
t.Fatalf("Set (overwrite): %v", err)
|
|
}
|
|
value, found, err := settings.Get(ctx, "federation.enabled")
|
|
if err != nil || !found {
|
|
t.Fatalf("Get after Set: value=%q found=%v err=%v", value, found, err)
|
|
}
|
|
if value != "false" {
|
|
t.Errorf("value = %q, want %q (the upsert must overwrite)", value, "false")
|
|
}
|
|
|
|
all, err := settings.GetAll(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GetAll: %v", err)
|
|
}
|
|
if all["federation.enabled"] != "false" {
|
|
t.Errorf("GetAll = %v", all)
|
|
}
|
|
|
|
if err := settings.Delete(ctx, "federation.enabled"); err != nil {
|
|
t.Fatalf("Delete: %v", err)
|
|
}
|
|
if err := settings.Delete(ctx, "federation.enabled"); err != nil {
|
|
t.Errorf("deleting an absent key must not fail: %v", err)
|
|
}
|
|
if _, found, _ := settings.Get(ctx, "federation.enabled"); found {
|
|
t.Error("value survived Delete")
|
|
}
|
|
|
|
if err := db.Ping(ctx); err != nil {
|
|
t.Errorf("Ping: %v", err)
|
|
}
|
|
if db.Driver() != driver {
|
|
t.Errorf("Driver() = %q, want %q", db.Driver(), driver)
|
|
}
|
|
})
|
|
}
|
|
}
|