Skip to content

Commit

Permalink
debug: Add debugging messages for debounce/throttle test
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Sep 25, 2024
1 parent d84aa94 commit c7ca49c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/testthat/test-reactivity.r
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ test_that("debounce/throttle work properly (with priming)", {
src <- observe({
invalidateLater(100)
rv$a <- isolate(rv$a) + 1
message(the_time(), "input ", isolate(rv$a))
})
on.exit(src$destroy(), add = TRUE)

Expand All @@ -1258,12 +1259,14 @@ test_that("debounce/throttle work properly (with priming)", {
dr_fired <- 0
dr_monitor <- observeEvent(dr(), {
dr_fired <<- dr_fired + 1
message(the_time(), "debounced ", dr_fired)
})
on.exit(dr_monitor$destroy(), add = TRUE)

tr_fired <- 0
tr_monitor <- observeEvent(tr(), {
tr_fired <<- tr_fired + 1
message(the_time(), "throttle ", tr_fired)
})
on.exit(tr_monitor$destroy(), add = TRUE)

Expand All @@ -1277,12 +1280,16 @@ test_that("debounce/throttle work properly (with priming)", {
}

# Pump timer and reactives for about 1.3 seconds
stopAt <- Sys.time() + 1.3
start_time <- Sys.time()
the_time <- function() sprintf("%0.5f ", Sys.time() - start_time)
message(the_time(), "==== start [run for 1.3s]")
stopAt <- start_time + 1.3
while (Sys.time() < stopAt) {
timerCallbacks$executeElapsed()
flushReact()
Sys.sleep(0.001)
}
message(the_time(), "==== end [run for 1.3s]")

# dr() should not have had time to fire, other than the initial run, since
# there haven't been long enough gaps between invalidations.
Expand All @@ -1297,11 +1304,13 @@ test_that("debounce/throttle work properly (with priming)", {
# Now let some time pass without any more updates.
src$destroy() # No more updates
stopAt <- Sys.time() + 1
message(the_time(), "==== start [finish running]")
while (Sys.time() < stopAt) {
timerCallbacks$executeElapsed()
flushReact()
Sys.sleep(0.001)
}
message(the_time(), "==== end [finish running]")

# dr should've fired, and we should have converged on the right answer.
expect_identical(dr_fired, 2)
Expand Down

0 comments on commit c7ca49c

Please sign in to comment.