Skip to content

Commit

Permalink
Add test for stack trace growth
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Nov 28, 2024
1 parent 4750359 commit d75fa0a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 37 deletions.
90 changes: 90 additions & 0 deletions tests/testthat/_snaps/stacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# integration tests

Code
df
Output
num call loc category
1 64 A 3] user
2 63 B 7] user
3 62 <reactive:C> 11] user
4 42 C user
5 41 renderTable 18] user
6 40 func user
7 39 force
8 38 withVisible
9 37 withCallingHandlers user

---

Code
df
Output
num call loc category
1 67 h
2 66 .handleSimpleError
3 65 stop
4 64 A 3] user
5 63 B 7] user
6 62 <reactive:C> 11] user
7 61 ..stacktraceon.. user
8 60 .func user
9 59 withVisible
10 58 withCallingHandlers user
11 57 contextFunc user
12 56 env$runWith user
13 55 withCallingHandlers user
14 54 domain$wrapSync user
15 53 promises::with_promise_domain
16 52 captureStackTraces user
17 51 force user
18 50 domain$wrapSync user
19 49 promises::with_promise_domain
20 48 withReactiveDomain user
21 47 domain$wrapSync user
22 46 promises::with_promise_domain
23 45 ctx$run user
24 44 self$.updateValue user
25 43 ..stacktraceoff.. user
26 42 C user
27 41 renderTable 18] user
28 40 func user
29 39 force
30 38 withVisible
31 37 withCallingHandlers user
32 36 domain$wrapSync user
33 35 promises::with_promise_domain
34 34 captureStackTraces user
35 33 doTryCatch user
36 32 tryCatchOne
37 31 tryCatchList
38 30 tryCatch
39 29 do user
40 28 hybrid_chain user
41 27 renderFunc user
42 26 renderTable({ C() }, server = FALSE) user
43 25 ..stacktraceon.. 17] user
44 24 contextFunc user
45 23 env$runWith user
46 22 withCallingHandlers user
47 21 domain$wrapSync user
48 20 promises::with_promise_domain
49 19 captureStackTraces user
50 18 force user
51 17 domain$wrapSync user
52 16 promises::with_promise_domain
53 15 withReactiveDomain user
54 14 domain$wrapSync user
55 13 promises::with_promise_domain
56 12 ctx$run user
57 11 ..stacktraceoff.. user
58 10 isolate user
59 9 withCallingHandlers 16] user
60 8 domain$wrapSync user
61 7 promises::with_promise_domain
62 6 captureStackTraces user
63 5 doTryCatch 15] user
64 4 tryCatchOne
65 3 tryCatchList
66 2 tryCatch
67 1 try

49 changes: 49 additions & 0 deletions tests/testthat/test-promise-domains.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
with_several_promise_domains <- function(expr) {
withReactiveDomain(MockShinySession$new(), {
promises::with_promise_domain(reactivePromiseDomain(), {
captureStackTraces({
expr
})
})
})
}

recursive_promise <- function(n, callback = identity) {
if (n <= 0) {
return(promise_resolve(0))
}

p <- promises::promise_resolve(TRUE)
promises::then(p, ~{
callback(n)
recursive_promise(n - 1, callback = callback)
})
}

test_that("Stack trace doesn't grow (resolution within domain)", {

depths <- list()
with_several_promise_domains({
recursive_promise(10, function(n) {
depths <<- c(depths, list(length(sys.calls())))
})
while (!later::loop_empty()) {
later::run_now()
}
})
expect_equal(diff(range(depths)), 0)
})

test_that("Stack trace doesn't grow (resolution outside domain)", {

depths <- list()
with_several_promise_domains({
recursive_promise(10, function(n) {
depths <<- c(depths, list(length(sys.calls())))
})
})
while (!later::loop_empty()) {
later::run_now()
}
expect_equal(diff(range(depths)), 0)
})
40 changes: 3 additions & 37 deletions tests/testthat/test-stacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,46 +129,12 @@ test_that("integration tests", {
df <- causeError(full = FALSE)
# dumpTests(df)

expect_equal(df$num, c(56L, 55L, 54L, 38L, 37L, 36L, 35L, 34L, 33L))
expect_equal(df$call, c("A", "B", "<reactive:C>", "C", "renderTable",
"func", "force", "withVisible", "withCallingHandlers"))
expect_equal(nzchar(df$loc), c(TRUE, TRUE, TRUE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE))
expect_snapshot(df)

df <- causeError(full = TRUE)
# dumpTests(df)

expect_equal(df$num, c(59L, 58L, 57L, 56L, 55L, 54L, 53L,
52L, 51L, 50L, 49L, 48L, 47L, 46L, 45L, 44L, 43L, 42L, 41L,
40L, 39L, 38L, 37L, 36L, 35L, 34L, 33L, 32L, 31L, 30L, 29L,
28L, 27L, 26L, 25L, 24L, 23L, 22L, 21L, 20L, 19L, 18L, 17L,
16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L,
3L, 2L, 1L))
expect_equal(df$call, c("h", ".handleSimpleError", "stop",
"A", "B", "<reactive:C>", "..stacktraceon..", ".func", "withVisible",
"withCallingHandlers", "contextFunc", "env$runWith", "force",
"domain$wrapSync", "promises::with_promise_domain",
"withReactiveDomain", "domain$wrapSync", "promises::with_promise_domain",
"ctx$run", "self$.updateValue", "..stacktraceoff..", "C",
"renderTable", "func", "force", "withVisible", "withCallingHandlers",
"domain$wrapSync", "promises::with_promise_domain",
"captureStackTraces", "doTryCatch", "tryCatchOne", "tryCatchList",
"tryCatch", "do", "hybrid_chain", "renderFunc", "renderTable({ C() }, server = FALSE)",
"..stacktraceon..", "contextFunc", "env$runWith", "force",
"domain$wrapSync", "promises::with_promise_domain",
"withReactiveDomain", "domain$wrapSync", "promises::with_promise_domain",
"ctx$run", "..stacktraceoff..", "isolate", "withCallingHandlers",
"domain$wrapSync", "promises::with_promise_domain",
"captureStackTraces", "doTryCatch", "tryCatchOne", "tryCatchList",
"tryCatch", "try"))
expect_equal(nzchar(df$loc), c(FALSE, FALSE, FALSE, TRUE,
TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE,
FALSE))
expect_snapshot(df)
# dumpTests(df)
})

test_that("shiny.error", {
Expand Down

0 comments on commit d75fa0a

Please sign in to comment.