| 1 |
# Dev CRAN had some issues with comparisons between integer(0) and character(0) |
| 2 |
# Skipping tests on CRAN as CI is enough to verify functionality |
| 3 |
# Related: https://github.com/rstudio/shiny/issues/4326 |
| 4 |
skip_on_cran() |
| 5 |
|
| 6 |
test_that("Scheduling works", { |
| 7 |
ran <- FALSE |
| 8 |
fun <- function() { |
| 9 |
ran <<- TRUE |
| 10 |
} |
| 11 |
|
| 12 |
timerCallbacks$schedule(500, fun) |
| 13 |
|
| 14 |
timerCallbacks$executeElapsed() |
| 15 |
expect_false(ran) |
| 16 |
|
| 17 |
Sys.sleep(0.1) |
| 18 |
timerCallbacks$executeElapsed() |
| 19 |
expect_false(ran) |
| 20 |
|
| 21 |
Sys.sleep(0.5) |
| 22 |
expect_true(timerCallbacks$executeElapsed()) |
| 23 |
expect_true(ran) |
| 24 |
|
| 25 |
# Empty timerCallbacks should return FALSE |
| 26 |
expect_false(timerCallbacks$executeElapsed()) |
| 27 |
expect_equal(0, nrow(timerCallbacks$takeElapsed())) |
| 28 |
}) |
| 29 |
|
| 30 |
test_that("Unscheduling works", { |
| 31 |
origTimes <- timerCallbacks$.times |
| 32 |
origFuncKeys <- timerCallbacks$.funcs$keys() |
| 33 |
|
| 34 |
taskHandle <- scheduleTask(1000, function() { |
| 35 |
message("Whatever") |
| 36 |
}) |
| 37 |
# Unregister |
| 38 |
taskHandle() |
| 39 |
|
| 40 |
# Split into two sections to avoid `expect_equal(integer(0), character(0))` comparison on dev CRAN |
| 41 |
if (length(origTimes) == 0) { |
| 42 |
expect_equal(0, length(timerCallbacks$.times)) |
| 43 |
} else { |
| 44 |
expect_equal(timerCallbacks$.times, origTimes) |
| 45 |
} |
| 46 |
expect_equal(timerCallbacks$.funcs$keys(), origFuncKeys) |
| 47 |
}) |
| 48 |
|
| 49 |
test_that("Vectorized unscheduling works", { |
| 50 |
key1 <- timerCallbacks$schedule(1000, function() {}) |
| 51 |
key2 <- timerCallbacks$schedule(1000, function() {}) |
| 52 |
key3 <- timerCallbacks$schedule(1000, function() {}) |
| 53 |
|
| 54 |
expect_identical(timerCallbacks$unschedule(key2), TRUE) |
| 55 |
expect_identical(timerCallbacks$unschedule(c(key1, key2, key3)), c(TRUE, FALSE, TRUE)) |
| 56 |
}) |
| 57 |
|
| 58 |
test_that("defineScheduler works", { |
| 59 |
expect_identical(defineScheduler(NULL), scheduleTask) |
| 60 |
expect_identical(defineScheduler(list()), scheduleTask) |
| 61 |
expect_identical(defineScheduler(list(.scheduleTask=123)), 123) |
| 62 |
}) |
| 63 |
|
| 64 |
test_that("mockableTimer works", { |
| 65 |
mt <- MockableTimerCallbacks$new() |
| 66 |
called <- FALSE |
| 67 |
mt$schedule(50, function(){ |
| 68 |
called <<- TRUE |
| 69 |
}) |
| 70 |
expect_false(mt$executeElapsed()) |
| 71 |
|
| 72 |
# Prove that we're not bound to a real clock |
| 73 |
Sys.sleep(.1) |
| 74 |
expect_false(mt$executeElapsed()) |
| 75 |
expect_false(called) |
| 76 |
|
| 77 |
mt$elapse(51) |
| 78 |
expect_true(mt$executeElapsed()) |
| 79 |
expect_true(called) |
| 80 |
}) |
| 81 |
|
| 82 |
test_that("getDomainTimeMs works", { |
| 83 |
start <- as.numeric(Sys.time()) * 1000 |
| 84 |
t1 <- getDomainTimeMs(NULL) |
| 85 |
t2 <- getDomainTimeMs(list()) |
| 86 |
t3 <- getDomainTimeMs(list(.now = function(){456})) |
| 87 |
end <- as.numeric(Sys.time()) * 1000 |
| 88 |
|
| 89 |
expect_gte(t1, start) |
| 90 |
expect_gte(t2, start) |
| 91 |
expect_lte(t1, end) |
| 92 |
expect_lte(t2, end) |
| 93 |
|
| 94 |
expect_equal(t3, 456) |
| 95 |
}) |