diff --git a/sqlite_test.go b/sqlite_test.go index f9b380a..60d3a07 100644 --- a/sqlite_test.go +++ b/sqlite_test.go @@ -17,6 +17,7 @@ package sqlite_test import ( "bytes" "context" + "errors" "io" "io/ioutil" "os" @@ -864,3 +865,29 @@ func TestReturningClause(t *testing.T) { t.Fatalf("want returned fruit id to be 1, got %d", id) } } + +// See https://github.com/crawshaw/sqlite/issues/119 and +// https://github.com/zombiezen/go-sqlite/issues/14 +func TestConnWALClose(t *testing.T) { + dbName := filepath.Join(t.TempDir(), "wal-close.db") + conn, err := sqlite.OpenConn(dbName, 0) + if err != nil { + t.Fatal(err) + } + done := make(chan struct{}) + conn.SetInterrupt(done) + err = sqlitex.ExecTransient(conn, `CREATE TABLE foo (id integer primary key);`, nil) + if err != nil { + t.Error(err) + } + if _, err := os.Stat(dbName + "-wal"); err != nil { + t.Error(err) + } + close(done) + if err := conn.Close(); err != nil { + t.Error(err) + } + if _, err := os.Stat(dbName + "-wal"); !errors.Is(err, os.ErrNotExist) { + t.Error("wal file should not exist after close") + } +} diff --git a/sqlitex/pool_test.go b/sqlitex/pool_test.go index 771639d..dc846ef 100644 --- a/sqlitex/pool_test.go +++ b/sqlitex/pool_test.go @@ -16,6 +16,7 @@ package sqlitex_test import ( "context" + "errors" "fmt" "io/ioutil" "os" @@ -302,3 +303,30 @@ func TestPoolOpenInit(t *testing.T) { t.Fatal("a cancelled context should interrupt initialization") }) } + +// See https://github.com/crawshaw/sqlite/issues/119 and +// https://github.com/zombiezen/go-sqlite/issues/14 +func TestPoolWALClose(t *testing.T) { + dbName := filepath.Join(t.TempDir(), "wal-close.db") + pool, err := sqlitex.Open(dbName, 0, 10) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + conn := pool.Get(ctx) + if _, err := os.Stat(dbName + "-wal"); err != nil { + t.Error(err) + } + err = sqlitex.ExecTransient(conn, `CREATE TABLE foo (id integer primary key);`, nil) + if err != nil { + t.Error(err) + } + cancel() + pool.Put(conn) + if err := pool.Close(); err != nil { + t.Error(err) + } + if _, err := os.Stat(dbName + "-wal"); !errors.Is(err, os.ErrNotExist) { + t.Error("wal file should not exist after close") + } +}