Skip to content

Commit

Permalink
Test checkpoints on close will not fail
Browse files Browse the repository at this point in the history
Checkpoint on close must also not force a new generation if we didn't
actually lose our lock while another process wrote to the WAL.
  • Loading branch information
hifi committed Dec 20, 2024
1 parent faf59e1 commit 1fc19ac
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,50 @@ func TestDB_Sync(t *testing.T) {
})
}

// Ensure a checkpoint on close will succeed.
func TestDB_CheckpointOnClose(t *testing.T) {
db, sqldb := MustOpenDBs(t)
defer db.Close(context.Background())
defer MustCloseSQLDB(t, sqldb)

// Force checkpoint after every write.
db.MinCheckpointPageN = 1

// Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err)
} else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err)
}

// Get generation to test later it didn't change.
firstGeneration, err := db.CurrentGeneration()
if err != nil {
t.Fatal(err)
}

// Write again and sync.
if _, err := sqldb.Exec(`INSERT INTO foo (bar) VALUES ('baz');`); err != nil {
t.Fatal(err)
} else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err)
}

// Write to WAL & close.
if _, err := sqldb.Exec(`INSERT INTO foo (bar) VALUES ('baz');`); err != nil {
t.Fatal(err)
} else if err := db.Close(context.Background()); err != nil {
t.Fatal(err)
}

finalGeneration, err := db.CurrentGeneration()
if err != nil {
t.Fatal(err)
} else if finalGeneration != firstGeneration {
t.Fatal("generation changed", firstGeneration, "!=", finalGeneration)
}
}

// MustOpenDBs returns a new instance of a DB & associated SQL DB.
func MustOpenDBs(tb testing.TB) (*litestream.DB, *sql.DB) {
tb.Helper()
Expand Down

0 comments on commit 1fc19ac

Please sign in to comment.