|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + _ "github.com/mattn/go-sqlite3" |
| 12 | + "github.com/psanford/sqlite3vfs" |
| 13 | + |
| 14 | + "github.com/benbjohnson/litestream" |
| 15 | + "github.com/benbjohnson/litestream/file" |
| 16 | + "github.com/benbjohnson/litestream/internal/testingutil" |
| 17 | +) |
| 18 | + |
| 19 | +func TestVFS_Integration(t *testing.T) { |
| 20 | + t.Run("Simple", func(t *testing.T) { |
| 21 | + client := file.NewReplicaClient(t.TempDir()) |
| 22 | + vfs := newVFS(t, client) |
| 23 | + if err := sqlite3vfs.RegisterVFS("litestream", vfs); err != nil { |
| 24 | + t.Fatalf("failed to register litestream vfs: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + db := testingutil.NewDB(t, filepath.Join(t.TempDir(), "db")) |
| 28 | + db.MonitorInterval = 100 * time.Millisecond |
| 29 | + db.Replica = litestream.NewReplica(db) |
| 30 | + db.Replica.Client = client |
| 31 | + if err := db.Open(); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + sqldb0 := testingutil.MustOpenSQLDB(t, db.Path()) |
| 35 | + defer testingutil.MustCloseSQLDB(t, sqldb0) |
| 36 | + |
| 37 | + if _, err := sqldb0.Exec("CREATE TABLE t (x)"); err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + if _, err := sqldb0.Exec("INSERT INTO t (x) VALUES (100)"); err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + time.Sleep(2 * db.MonitorInterval) |
| 44 | + |
| 45 | + sqldb1, err := sql.Open("sqlite3", "file:/tmp/test.db?vfs=litestream") |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("failed to open database: %v", err) |
| 48 | + } |
| 49 | + defer sqldb1.Close() |
| 50 | + |
| 51 | + // Execute query |
| 52 | + var x int |
| 53 | + if err := sqldb1.QueryRow("SELECT * FROM t").Scan(&x); err != nil { |
| 54 | + t.Fatalf("failed to query database: %v", err) |
| 55 | + } else if got, want := x, 100; got != want { |
| 56 | + t.Fatalf("got %d, want %d", got, want) |
| 57 | + } |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +func newVFS(tb testing.TB, client litestream.ReplicaClient) *litestream.VFS { |
| 62 | + tb.Helper() |
| 63 | + logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 64 | + return litestream.NewVFS(client, logger) |
| 65 | +} |
0 commit comments