| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "net/url" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "reasonix/internal/sessioncatalog" |
| 11 | "reasonix/internal/sqliteuri" |
| 12 | ) |
| 13 | |
| 14 | func TestStopSessionCatalogWaitsForEarlierTimedOutClose(t *testing.T) { |
| 15 | isolateDesktopUserDirs(t) |
| 16 | app := NewApp() |
| 17 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 18 | catalog, err := sessioncatalog.Open(t.Context(), sessioncatalog.Options{Path: path, DisableRepair: true}) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | app.sessionCatalog.Store(catalog) |
| 23 | t.Cleanup(func() { |
| 24 | if !app.stopSessionCatalog(sessionCatalogTestDeadline) { |
| 25 | t.Error("session catalog did not stop before directory cleanup") |
| 26 | } |
| 27 | }) |
| 28 | |
| 29 | // A reader holding a WAL snapshot makes the closing TRUNCATE checkpoint wait |
| 30 | // out its busy timeout, so the close outlives a short stop deadline. |
| 31 | dsn, err := sqliteuri.Disk(path, url.Values{"mode": {"ro"}}) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | reader, err := sql.Open("sqlite", dsn) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | t.Cleanup(func() { _ = reader.Close() }) |
| 40 | tx, err := reader.BeginTx(t.Context(), &sql.TxOptions{ReadOnly: true}) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | t.Cleanup(func() { _ = tx.Rollback() }) |
| 45 | var revision uint64 |
| 46 | if err := tx.QueryRow(`SELECT revision FROM catalog_state WHERE id=1`).Scan(&revision); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | |
| 50 | if app.stopSessionCatalog(time.Millisecond) { |
| 51 | t.Fatal("stop finished while a reader pinned the WAL checkpoint") |
| 52 | } |
| 53 | if !app.stopSessionCatalog(sessionCatalogTestDeadline) { |
| 54 | t.Fatal("a later stop did not wait out the earlier close") |
| 55 | } |
| 56 | if state := catalog.Status().State; state != sessioncatalog.StateClosed { |
| 57 | t.Fatalf("stop reported the catalog stopped while it was %q", state) |
| 58 | } |
| 59 | } |
| 60 |