Skip to content

Commit 5eced0c

Browse files
authored
Merge pull request #385 from zozlak/issue-344-tests
- Expand tests for `dbConnect(check_interrupts = TRUE)` (#385, @zozlak).
2 parents 0e08da2 + a3a29d1 commit 5eced0c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tests/testthat/test-checkInterrupts.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
context("checkInterrupts")
22

3-
test_that("check_interrupts = TRUE works with queries < 1 second (#244)", {
3+
test_that("check_interrupts = TRUE works with queries < 1 second", {
44
con <- postgresDefault(check_interrupts = TRUE)
55
time <- system.time(
66
expect_equal(dbGetQuery(con, "SELECT pg_sleep(0.2), 'foo' AS x")$x, "foo")
@@ -9,6 +9,15 @@ test_that("check_interrupts = TRUE works with queries < 1 second (#244)", {
99
dbDisconnect(con)
1010
})
1111

12+
test_that("check_interrupts = TRUE works with queries > 1 second (#244)", {
13+
con <- postgresDefault(check_interrupts = TRUE)
14+
time <- system.time(
15+
expect_equal(dbGetQuery(con, "SELECT pg_sleep(2), 'foo' AS x")$x, "foo")
16+
)
17+
expect_gt(time[["elapsed"]], 1.5)
18+
dbDisconnect(con)
19+
})
20+
1221
test_that("check_interrupts = TRUE interrupts immediately (#336)", {
1322
skip_if_not(postgresHasDefault())
1423
skip_if(Sys.getenv("R_COVR") != "")
@@ -20,6 +29,8 @@ test_that("check_interrupts = TRUE interrupts immediately (#336)", {
2029
session$run(function() {
2130
library(RPostgres)
2231
.GlobalEnv$conn <- postgresDefault(check_interrupts = TRUE)
32+
.GlobalEnv$connPid <- dbGetQuery(.GlobalEnv$conn, "SELECT pg_backend_pid() AS pid")$pid
33+
.GlobalEnv$checkConn <- postgresDefault()
2334
invisible()
2435
})
2536

@@ -31,8 +42,13 @@ test_that("check_interrupts = TRUE interrupts immediately (#336)", {
3142
session$interrupt()
3243
# Interrupts are slow on Windows, give ample time
3344
expect_equal(session$poll_process(2000), "ready")
45+
session$read()
46+
47+
queryStatus = session$run(function() {
48+
dbGetQuery(.GlobalEnv$checkConn, "SELECT state FROM pg_stat_activity WHERE pid = $1", params = .GlobalEnv$connPid)
49+
})
50+
expect_equal(queryStatus$state, "idle")
3451

35-
# Tests for error behavior are brittle
3652

3753
session$close()
3854
})

0 commit comments

Comments
 (0)