tests/testthat/test-shutdown.R

test_that("disconnect releases database file", {
  skip_if_not(TEST_RE2)

  db_path <- withr::local_tempfile(fileext = ".duckdb")

  session_1 <- callr::r_session$new()
  withr::defer(session_1$kill())
  session_2 <- callr::r_session$new()
  withr::defer(session_2$kill())

  session_1$run(
    function(db_path) {
      .GlobalEnv$con <- DBI::dbConnect(duckdb::duckdb(), db_path)
      DBI::dbWriteTable(con, "test", data.frame(a = 1))
    },
    list(db_path = db_path)
  )

  expect_error(session_2$run(
    function(db_path) {
      .GlobalEnv$con <- DBI::dbConnect(duckdb::duckdb(), db_path)
    },
    list(db_path = db_path)
  ))

  session_1$run(function() {
    DBI::dbDisconnect(con, shutdown = TRUE)
  })

  session_2$run(
    function(db_path) {
      .GlobalEnv$con <- DBI::dbConnect(duckdb::duckdb(), db_path)
      DBI::dbDisconnect(con, shutdown = TRUE)
    },
    list(db_path = db_path)
  )
})

Try the duckdb package in your browser

Any scripts or data that you put into this service are public.

duckdb documentation built on Oct. 30, 2024, 5:06 p.m.