Debian Codegraph @

tests/testthat/test-reactivity.r

raw symbols SCIP
1 test_that("reactive and reactiveVal are functions", {
2 expect_s3_class(reactive({1}), "function")
3 expect_s3_class(reactiveVal(1), "function")
4 })
5
6
7
8 test_that("ReactiveVal", {
9 val <- reactiveVal()
10
11 isolate({
12 expect_null(val())
13
14 # Set to a simple value
15 val(1)
16 expect_equal(val(), 1)
17
18 # Set to a complex value
19 val(cars)
20 expect_equal(val(), cars)
21
22 # Check that passing in an initial value works
23 expect_equal(reactiveVal(10)(), 10)
24 })
25
26 o <- observe({
27 val()
28 })
29 flushReact()
30 expect_equal(execCount(o), 1)
31 # Just making sure o is stable
32 flushReact()
33 expect_equal(execCount(o), 1)
34
35 # Changing value causes o to invalidate
36 val(10)
37 flushReact()
38 expect_equal(execCount(o), 2)
39
40 # Setting new value that's same as current value is a no-op
41 val(10)
42 flushReact()
43 expect_equal(execCount(o), 2) #
44
45 o$destroy()
46 })
47
48 test_that("ReactiveVals have independent dependencies", {
49 # Issue 1710
50 x <- reactiveVal(0)
51 y <- reactiveVal(0)
52
53 o <- observe({
54 y()
55 })
56
57 # The observer always fires the first time
58 x(1)
59 flushReact()
60 expect_equal(execCount(o), 1)
61
62 # Changing x again shouldn't invalidate the observer
63 x(2)
64 flushReact()
65 expect_equal(execCount(o), 1)
66
67 o$destroy()
68 })
69
70
71 test_that("ReactiveVal labels", {
72 val <- reactiveVal()
73 expect_equal(attr(val, "label", exact = TRUE), "val")
74
75 name.with.dots = reactiveVal()
76 expect_equal(attr(name.with.dots, "label", exact = TRUE), "name.with.dots")
77 })
78
79 # Test for correct behavior of ReactiveValues
80 test_that("ReactiveValues", {
81 # Creation and indexing into ReactiveValues -------------------------------
82 values <- reactiveValues()
83
84 # $ indexing
85 values$a <- 3
86 expect_equal(isolate(values$a), 3)
87
88 # [[ indexing
89 values[['a']] <- 4
90 expect_equal(isolate(values[['a']]), 4)
91
92 # Create with initialized values
93 values <- reactiveValues(a=1, b=2)
94 expect_equal(isolate(values$a), 1)
95 expect_equal(isolate(values[['b']]), 2)
96
97 # NULL values -------------------------------------------------------------
98 # Initializing with NULL value
99 values <- reactiveValues(a=NULL, b=2)
100 # a should exist and be NULL
101 expect_setequal(isolate(names(values)), c("a", "b"))
102 expect_null(isolate(values$a))
103
104 # Assigning NULL should keep object (not delete it), and set value to NULL
105 values$b <- NULL
106 expect_setequal(isolate(names(values)), c("a", "b"))
107 expect_null(isolate(values$b))
108
109
110 # Errors -----------------------------------------------------------------
111 # Error: indexing with non-string
112 expect_error(isolate(values[[1]]))
113 expect_error(isolate(values[[NULL]]))
114 expect_error(isolate(values[[list('a')]]))
115
116 # Error: [ indexing shouldn't work
117 expect_error(isolate(values['a']))
118 expect_error(isolate(values['a'] <- 1))
119
120 # Error: unnamed arguments
121 expect_error(reactiveValues(1))
122 expect_error(reactiveValues(1, b=2))
123
124 # Error: assignment to readonly values
125 values <- .createReactiveValues(ReactiveValues$new(), readonly = TRUE)
126 expect_error(values$a <- 1)
127 })
128
129 test_that("reactiveValues keys are sorted", {
130 values <- reactiveValues(b=2, a=0)
131 values$C <- 13
132 values$A <- 0
133 values$c <- 3
134 values$B <- 12
135 # Setting an existing value shouldn't change order
136 values$a <- 1
137 values$A <- 11
138
139 expect_identical(isolate(names(values)), c("b", "a", "C", "A", "c", "B"))
140 expect_identical(
141 isolate(reactiveValuesToList(values)),
142 list(b=2, a=1, C=13, A=11, c=3, B=12)
143 )
144 })
145
146 test_that("reactiveValues() has useful print method", {
147 expect_snapshot_output({
148 x <- reactiveValues(x = 1, y = 2, z = 3)
149 x
150 })
151 })
152
153 # Test for overreactivity. funcB has an indirect dependency on valueA (via
154 # funcA) and also a direct dependency on valueA. When valueA changes, funcB
155 # should only execute once.
156 test_that("Functions are not over-reactive", {
157
158 values <- reactiveValues(A=10)
159
160 funcA <- reactive({
161 values$A
162 })
163
164 funcB <- reactive({
165 funcA()
166 values$A
167 })
168
169 obsC <- observe({
170 funcB()
171 })
172
173 flushReact()
174 expect_equal(execCount(funcB), 1)
175 expect_equal(execCount(obsC), 1)
176
177 values$A <- 11
178 flushReact()
179 expect_equal(execCount(funcB), 2)
180 expect_equal(execCount(obsC), 2)
181 })
182
183 ## "foo => bar" is defined as "foo is a dependency of bar"
184 ##
185 ## vA => fB
186 ## (fB, vA) => obsE
187 ## (fB, vA) => obsF
188 ##
189 ## obsE and obsF should each execute once when vA changes.
190 test_that("overreactivity2", {
191 # ----------------------------------------------
192 # Test 1
193 # B depends on A, and observer depends on A and B. The observer uses A and
194 # B, in that order.
195
196 # This is to store the value from observe()
197 observed_value1 <- NA
198 observed_value2 <- NA
199
200 values <- reactiveValues(A=1)
201 funcB <- reactive({
202 values$A + 5
203 })
204 obsC <- observe({
205 observed_value1 <<- funcB() * values$A
206 })
207 obsD <- observe({
208 observed_value2 <<- funcB() * values$A
209 })
210
211 flushReact()
212 expect_equal(observed_value1, 6) # Should be 1 * (1 + 5) = 6
213 expect_equal(observed_value2, 6) # Should be 1 * (1 + 5) = 6
214 expect_equal(execCount(funcB), 1)
215 expect_equal(execCount(obsC), 1)
216 expect_equal(execCount(obsD), 1)
217
218 values$A <- 2
219 flushReact()
220 expect_equal(observed_value1, 14) # Should be 2 * (2 + 5) = 14
221 expect_equal(observed_value2, 14) # Should be 2 * (2 + 5) = 14
222 expect_equal(execCount(funcB), 2)
223 expect_equal(execCount(obsC), 2)
224 expect_equal(execCount(obsD), 2)
225 })
226
227 ## Test for isolation. funcB depends on funcA depends on valueA. When funcA
228 ## is invalidated, if its new result is not different than its old result,
229 ## then it doesn't invalidate its dependents. This is done by adding an observer
230 ## (valueB) between obsA and funcC.
231 ##
232 ## valueA => obsB => valueC => funcD => obsE
233 test_that("isolation", {
234 values <- reactiveValues(A=10, C=NULL)
235
236 obsB <- observe({
237 values$C <- values$A > 0
238 })
239
240 funcD <- reactive({
241 values$C
242 })
243
244 obsE <- observe({
245 funcD()
246 })
247
248 flushReact()
249 countD <- execCount(funcD)
250
251 values$A <- 11
252 flushReact()
253 expect_equal(execCount(funcD), countD)
254 })
255
256
257 ## Test for laziness. With lazy evaluation, the observers should "pull" values
258 ## from their dependent functions. In contrast, eager evaluation would have
259 ## reactive values and functions "push" their changes down to their descendents.
260 test_that("laziness", {
261
262 values <- reactiveValues(A=10)
263
264 funcA <- reactive({
265 values$A > 0
266 })
267
268 funcB <- reactive({
269 funcA()
270 })
271
272 obsC <- observe({
273 if (values$A > 10)
274 return()
275 funcB()
276 })
277
278 flushReact()
279 expect_equal(execCount(funcA), 1)
280 expect_equal(execCount(funcB), 1)
281 expect_equal(execCount(obsC), 1)
282
283 values$A <- 11
284 flushReact()
285 expect_equal(execCount(funcA), 1)
286 expect_equal(execCount(funcB), 1)
287 expect_equal(execCount(obsC), 2)
288 })
289
290
291 ## Suppose B depends on A and C depends on A and B. Then when A is changed,
292 ## the evaluation order should be A, B, C. Also, each time A is changed, B and
293 ## C should be run once, if we want to be maximally efficient.
294 test_that("order of evaluation", {
295 # ----------------------------------------------
296 # Test 1
297 # B depends on A, and observer depends on A and B. The observer uses A and
298 # B, in that order.
299
300 # This is to store the value from observe()
301 observed_value <- NA
302
303 values <- reactiveValues(A=1)
304 funcB <- reactive({
305 values$A + 5
306 })
307 obsC <- observe({
308 observed_value <<- values$A * funcB()
309 })
310
311 flushReact()
312 expect_equal(observed_value, 6) # Should be 1 * (1 + 5) = 6
313 expect_equal(execCount(funcB), 1)
314 expect_equal(execCount(obsC), 1)
315
316 values$A <- 2
317 flushReact()
318 expect_equal(observed_value, 14) # Should be 2 * (2 + 5) = 14
319 expect_equal(execCount(funcB), 2)
320 expect_equal(execCount(obsC), 2)
321
322
323 # ----------------------------------------------
324 # Test 2:
325 # Same as Test 1, except the observer uses A and B in reversed order.
326 # Resulting values should be the same.
327
328 observed_value <- NA
329
330 values <- reactiveValues(A=1)
331 funcB <- reactive({
332 values$A + 5
333 })
334 obsC <- observe({
335 observed_value <<- funcB() * values$A
336 })
337
338 flushReact()
339 # Should be 1 * (1 + 5) = 6
340 expect_equal(observed_value, 6)
341 expect_equal(execCount(funcB), 1)
342 expect_equal(execCount(obsC), 1)
343
344 values$A <- 2
345 flushReact()
346 # Should be 2 * (2 + 5) = 14
347 expect_equal(observed_value, 14)
348 expect_equal(execCount(funcB), 2)
349 expect_equal(execCount(obsC), 2)
350 })
351
352
353 ## Expressions in isolate() should not invalidate the parent context.
354 test_that("isolate() blocks invalidations from propagating", {
355
356 obsC_value <- NA
357 obsD_value <- NA
358
359 values <- reactiveValues(A=1, B=10)
360 funcB <- reactive({
361 values$B + 100
362 })
363
364 # References to valueB and funcB are isolated
365 obsC <- observe({
366 obsC_value <<-
367 values$A + isolate(values$B) + isolate(funcB())
368 })
369
370 # In contrast with obsC, this has a non-isolated reference to funcB
371 obsD <- observe({
372 obsD_value <<-
373 values$A + isolate(values$B) + funcB()
374 })
375
376
377 flushReact()
378 expect_equal(obsC_value, 121)
379 expect_equal(execCount(obsC), 1)
380 expect_equal(obsD_value, 121)
381 expect_equal(execCount(obsD), 1)
382
383 # Changing A should invalidate obsC and obsD
384 values$A <- 2
385 flushReact()
386 expect_equal(obsC_value, 122)
387 expect_equal(execCount(obsC), 2)
388 expect_equal(obsD_value, 122)
389 expect_equal(execCount(obsD), 2)
390
391 # Changing B shouldn't invalidate obsC becuause references to B are in isolate()
392 # But it should invalidate obsD.
393 values$B <- 20
394 flushReact()
395 expect_equal(obsC_value, 122)
396 expect_equal(execCount(obsC), 2)
397 expect_equal(obsD_value, 142)
398 expect_equal(execCount(obsD), 3)
399
400 # Changing A should invalidate obsC and obsD, and they should see updated
401 # values for valueA, valueB, and funcB
402 values$A <- 3
403 flushReact()
404 expect_equal(obsC_value, 143)
405 expect_equal(execCount(obsC), 3)
406 expect_equal(obsD_value, 143)
407 expect_equal(execCount(obsD), 4)
408 })
409
410
411 test_that("isolate() evaluates expressions in calling environment", {
412 outside <- 1
413 inside <- 1
414 loc <- 1
415
416 outside <- isolate(2) # Assignment outside isolate
417 isolate(inside <- 2) # Assignment inside isolate
418 # Should affect vars in the calling environment
419 expect_equal(outside, 2)
420 expect_equal(inside, 2)
421
422 isolate(local(loc <<- 2)) # <<- inside isolate(local)
423 isolate(local(loc <- 3)) # <- inside isolate(local) - should have no effect
424 expect_equal(loc, 2)
425 })
426
427
428 test_that("Circular refs/reentrancy in reactive functions work", {
429
430 values <- reactiveValues(A=3)
431
432 funcB <- reactive({
433 # Each time fB executes, it reads and then writes valueA,
434 # effectively invalidating itself--until valueA becomes 0.
435 if (values$A == 0)
436 return()
437 values$A <- values$A - 1
438 return(values$A)
439 })
440
441 obsC <- observe({
442 funcB()
443 })
444
445 flushReact()
446 expect_equal(execCount(obsC), 4)
447
448 values$A <- 3
449
450 flushReact()
451 expect_equal(execCount(obsC), 8)
452
453 })
454
455 test_that("Simple recursion", {
456
457 values <- reactiveValues(A=5)
458 funcB <- reactive({
459 if (values$A == 0)
460 return(0)
461 values$A <- values$A - 1
462 funcB()
463 })
464
465 obsC <- observe({
466 funcB()
467 })
468
469 flushReact()
470 expect_equal(execCount(obsC), 2)
471 expect_equal(execCount(funcB), 6)
472 })
473
474 test_that("Non-reactive recursion", {
475 nonreactiveA <- 3
476 outputD <- NULL
477
478 funcB <- reactive({
479 if (nonreactiveA == 0)
480 return(0)
481 nonreactiveA <<- nonreactiveA - 1
482 return(funcB())
483 })
484 obsC <- observe({
485 outputD <<- funcB()
486 })
487
488 flushReact()
489 expect_equal(execCount(funcB), 4)
490 expect_equal(outputD, 0)
491 })
492
493 test_that("Circular dep with observer only", {
494
495 values <- reactiveValues(A=3)
496 obsB <- observe({
497 if (values$A == 0)
498 return()
499 values$A <- values$A - 1
500 })
501
502 flushReact()
503 expect_equal(execCount(obsB), 4)
504 })
505
506 test_that("Writing then reading value is not circular", {
507
508 values <- reactiveValues(A=3)
509 funcB <- reactive({
510 values$A <- isolate(values$A) - 1
511 values$A
512 })
513
514 obsC <- observe({
515 funcB()
516 })
517
518 flushReact()
519 expect_equal(execCount(obsC), 1)
520
521 values$A <- 10
522
523 flushReact()
524 expect_equal(execCount(obsC), 2)
525 })
526
527 test_that("names() and reactiveValuesToList()", {
528
529 values <- reactiveValues(A=1, .B=2)
530
531 # Dependent on names
532 depNames <- observe({
533 names(values)
534 })
535
536 # Dependent on all non-hidden objects
537 depValues <- observe({
538 reactiveValuesToList(values)
539 })
540
541 # Dependent on all objects, including hidden
542 depAllValues <- observe({
543 reactiveValuesToList(values, all.names = TRUE)
544 })
545
546 # names() returns all names
547 expect_setequal(isolate(names(values)), c(".B", "A"))
548 # Assigning names fails
549 expect_error(isolate(names(v) <- c('x', 'y')))
550
551 expect_mapequal(isolate(reactiveValuesToList(values)), list(A=1))
552 expect_mapequal(isolate(reactiveValuesToList(values, all.names=TRUE)), list(A=1, .B=2))
553
554
555 flushReact()
556 expect_equal(execCount(depNames), 1)
557 expect_equal(execCount(depValues), 1)
558 expect_equal(execCount(depAllValues), 1)
559
560 # Update existing variable
561 values$A <- 2
562 flushReact()
563 expect_equal(execCount(depNames), 1)
564 expect_equal(execCount(depValues), 2)
565 expect_equal(execCount(depAllValues), 2)
566
567 # Update existing hidden variable
568 values$.B <- 3
569 flushReact()
570 expect_equal(execCount(depNames), 1)
571 expect_equal(execCount(depValues), 2)
572 expect_equal(execCount(depAllValues), 3)
573
574 # Add new variable
575 values$C <- 1
576 flushReact()
577 expect_equal(execCount(depNames), 2)
578 expect_equal(execCount(depValues), 3)
579 expect_equal(execCount(depAllValues), 4)
580
581 # Add new hidden variable
582 values$.D <- 1
583 flushReact()
584 expect_equal(execCount(depNames), 3)
585 expect_equal(execCount(depValues), 3)
586 expect_equal(execCount(depAllValues), 5)
587 })
588
589 test_that("Observer pausing works", {
590 values <- reactiveValues(a=1)
591
592 funcA <- reactive({
593 values$a
594 })
595
596 obsB <- observe({
597 funcA()
598 })
599
600 # Important: suspend() only affects observer at invalidation time
601
602 # Observers are invalidated at creation time, so it will run once regardless
603 # of being suspended
604 obsB$suspend()
605 flushReact()
606 expect_equal(execCount(funcA), 1)
607 expect_equal(execCount(obsB), 1)
608
609 # When resuming, if nothing changed, don't do anything
610 obsB$resume()
611 flushReact()
612 expect_equal(execCount(funcA), 1)
613 expect_equal(execCount(obsB), 1)
614
615 # Make sure suspended observers do not flush, but do invalidate
616 obsB_invalidated <- FALSE
617 obsB$onInvalidate(function() {obsB_invalidated <<- TRUE})
618 obsB$suspend()
619 values$a <- 2
620 flushReact()
621 expect_equal(obsB_invalidated, TRUE)
622 expect_equal(execCount(funcA), 1)
623 expect_equal(execCount(obsB), 1)
624
625 obsB$resume()
626 values$a <- 2.5
627 obsB$suspend()
628 flushReact()
629 expect_equal(execCount(funcA), 2)
630 expect_equal(execCount(obsB), 2)
631
632 values$a <- 3
633 flushReact()
634
635 expect_equal(execCount(funcA), 2)
636 expect_equal(execCount(obsB), 2)
637
638 # If onInvalidate() is added _after_ obsB is suspended and the values$a
639 # changes, then it shouldn't get run (onInvalidate runs on invalidation, not
640 # on flush)
641 values$a <- 4
642 obsB_invalidated2 <- FALSE
643 obsB$onInvalidate(function() {obsB_invalidated2 <<- TRUE})
644 obsB$resume()
645 flushReact()
646
647 expect_equal(execCount(funcA), 3)
648 expect_equal(execCount(obsB), 3)
649 expect_equal(obsB_invalidated2, FALSE)
650 })
651
652 test_that("suspended/resumed observers run at most once", {
653
654 values <- reactiveValues(A=1)
655 obs <- observe({
656 values$A
657 })
658 expect_equal(execCount(obs), 0)
659
660 # First flush should run obs once
661 flushReact()
662 expect_equal(execCount(obs), 1)
663
664 # Modify the dependency at each stage of suspend/resume/flush should still
665 # only result in one run of obs()
666 values$A <- 2
667 obs$suspend()
668 values$A <- 3
669 obs$resume()
670 values$A <- 4
671 flushReact()
672 expect_equal(execCount(obs), 2)
673
674 })
675
676
677 test_that("reactive() accepts injected quosures", {
678 # Normal usage - no quosures
679 a <- 1
680 f <- reactive({ a + 10 })
681 a <- 2
682 expect_identical(isolate(f()), 12)
683
684 # quosures can be used in reactive()
685 a <- 1
686 f <- reactive({ rlang::eval_tidy(rlang::quo(!!a + 10)) })
687 a <- 2
688 expect_identical(isolate(f()), 12)
689
690 # inject() with quosures
691 a <- 1
692 exp <- rlang::quo(a + 10)
693 f <- inject(reactive(!!exp))
694 a <- 2
695 expect_identical(isolate(f()), 12)
696
697 # inject() with !!!
698 a <- 1
699 exp <- list(rlang::quo(a + 10))
700 f <- inject(reactive(!!!exp))
701 a <- 2
702 expect_identical(isolate(f()), 12)
703
704 # inject() with captured environment
705 a <- 1
706 exp <- local({
707 q <- rlang::quo(a + 10)
708 a <- 2
709 q
710 })
711 f <- inject(reactive(!! exp ))
712 a <- 3
713 expect_identical(isolate(f()), 12)
714
715 # inject() with nested quosures
716 a <- 1
717 y <- quo(a)
718 exp <- quo(!!y + 10)
719 a <- 2
720 ff <- inject(reactive(!! exp ))
721 a <- 3
722 expect_identical(isolate(ff()), 13)
723 })
724
725 test_that("observe() accepts injected quosures", {
726 # Normal usage - no quosures
727 val <- NULL
728 a <- 1
729 observe({ val <<- a + 10 })
730 a <- 2
731 flushReact()
732 expect_identical(val, 12)
733
734 # quosures can be used in reactive()
735 val <- NULL
736 a <- 1
737 f <- observe({ val <<- rlang::eval_tidy(rlang::quo(!!a + 10)) })
738 a <- 2
739 flushReact()
740 expect_identical(val, 12)
741
742 # inject() with quosures
743 val <- NULL
744 a <- 1
745 exp <- rlang::quo(val <<- a + 10)
746 f <- inject(observe(!!exp))
747 a <- 2
748 flushReact()
749 expect_identical(val, 12)
750
751 # inject() with !!!
752 val <- NULL
753 a <- 1
754 exp <- list(quo(val <<- a + 10))
755 f <- inject(observe(!!!exp))
756 a <- 2
757 flushReact()
758 expect_identical(val, 12)
759
760 # inject() with captured environment
761 val <- NULL
762 a <- 1
763 exp <- local({
764 q <- rlang::quo(val <<- a + 10)
765 a <- 2
766 q
767 })
768 f <- inject(observe(!! exp ))
769 a <- 3
770 flushReact()
771 expect_identical(val, 12)
772
773 # inject() with nested quosures
774 val <- NULL
775 a <- 1
776 y <- quo(a)
777 exp <- rlang::quo(val <<- !!y + 10)
778 a <- 2
779 f <- inject(observe(!!exp))
780 a <- 3
781 flushReact()
782 expect_identical(val, 13)
783 })
784
785
786 test_that("reactive() accepts quoted and unquoted expressions", {
787 vals <- reactiveValues(A=1)
788
789 # Unquoted expression, with curly braces
790 fun <- reactive({ vals$A + 1 })
791 expect_equal(isolate(fun()), 2)
792
793 # Unquoted expression, no curly braces
794 fun <- reactive(vals$A + 1)
795 expect_equal(isolate(fun()), 2)
796
797 # Quoted expression
798 fun <- reactive(quote(vals$A + 1), quoted = TRUE)
799 expect_equal(isolate(fun()), 2)
800
801 # Quoted expression, saved in a variable
802 q_expr <- quote(vals$A + 1)
803 fun <- reactive(q_expr, quoted = TRUE)
804 expect_equal(isolate(fun()), 2)
805
806 # Functions being passed to reactives is no longer treated specially
807 fun <- reactive(function() { vals$A + 1 })
808 expect_true(is.function(isolate(fun())))
809
810
811 # Check that environment is correct - parent of parent environment should be
812 # this one. Note that rlang::as_function() injects an intermediate
813 # environment.
814 this_env <- environment()
815 fun <- reactive(environment())
816 expect_identical(isolate(parent.env(parent.env(fun()))), this_env)
817
818 # Sanity check: environment structure for a reactive() should be the same as for
819 # a normal function
820 fun <- function() environment()
821 expect_identical(parent.env(fun()), this_env)
822 })
823
824 test_that("observe() accepts quoted and unquoted expressions", {
825 vals <- reactiveValues(A=0)
826 valB <- 0
827
828 # Unquoted expression, with curly braces
829 observe({ valB <<- vals$A + 1})
830 flushReact()
831 expect_equal(valB, 1)
832
833 # Unquoted expression, no curly braces
834 observe({ valB <<- vals$A + 2})
835 flushReact()
836 expect_equal(valB, 2)
837
838 # Quoted expression
839 observe(quote(valB <<- vals$A + 3), quoted = TRUE)
840 flushReact()
841 expect_equal(valB, 3)
842
843 # Quoted expression, saved in a variable
844 q_expr <- quote(valB <<- vals$A + 4)
845 fun <- observe(q_expr, quoted = TRUE)
846 flushReact()
847 expect_equal(valB, 4)
848
849 # Functions are no longer treated specially
850 observe(function() { valB <<- vals$A + 5 })
851 flushReact()
852 expect_equal(valB, 4)
853
854
855 # Check that environment is correct - parent of parent environment should be
856 # this one. rlang::as_function() injects one intermediate env.
857 this_env <- environment()
858 inside_env <- NULL
859 fun <- observe(inside_env <<- environment())
860 flushReact()
861 expect_identical(parent.env(parent.env(inside_env)), this_env)
862 })
863
864 test_that("Observer priorities are respected", {
865 results <- c()
866 observe(results <<- c(results, 10), priority=10)
867 observe(results <<- c(results, 30), priority=30)
868 observe(results <<- c(results, 20), priority=20L)
869 observe(results <<- c(results, 21), priority=20)
870 observe(results <<- c(results, 22), priority=20L)
871
872 flushReact()
873
874 expect_identical(results, c(30, 20, 21, 22, 10))
875 })
876
877
878 # The specific order that observers fire in does not necessarily need to be the
879 # one below. However, it is important that they fire in an order that is
880 # consistent across platforms, so that developers don't see one behavior on
881 # their dev platform and another on their deployment platform. (#2466)
882 test_that("Observers fire in consistent order across platforms", {
883
884 # Reset the counter for the reactive environment. Not a good thing to do in
885 # general, but necessary for this test.
886 reactive_env <- .getReactiveEnvironment()
887 reactive_env$.nextId <- 0L
888
889 v <- reactiveVal(0)
890 order <- list()
891 order[1:20] <- list(integer())
892
893 observe({
894 order[[v()]] <<- c(order[[v()]], 1L)
895 message(v(), ": observer 1")
896 })
897 observe({
898 order[[v()]] <<- c(order[[v()]], 2L)
899 message(v(), ": observer 2")
900 })
901 observe({
902 order[[v()]] <<- c(order[[v()]], 3L)
903 message(v(), ": observer 3")
904 })
905
906 for (i in 1:20) {
907 suppressMessages({
908 v(isolate(v()) + 1); shiny:::flushReact()
909 })
910 }
911
912 expected_order <- list()
913 expected_order[1:2] <- list(c(1L, 2L, 3L))
914 expected_order[3:20] <- list(c(2L, 3L, 1L))
915 expect_identical(order, expected_order)
916 })
917
918
919 test_that("installExprFunction doesn't rely on name being `expr`", {
920 justExecute <- function(anExpression, envirToUse = parent.frame(), isQuoted = FALSE) {
921 shiny:::installExprFunction(anExpression, "myFunc", envirToUse, quoted = isQuoted)
922 myFunc()
923 }
924
925 expect_identical(-1, justExecute({-1}))
926 })
927
928 test_that("reactivePoll and reactiveFileReader", {
929 path <- tempfile('file')
930 on.exit(unlink(path))
931 write.csv(cars, file=path, row.names=FALSE)
932 rfr <- reactiveFileReader(100, NULL, path, read.csv)
933 expect_equal(isolate(rfr()), cars)
934
935 write.csv(rbind(cars, cars), file=path, row.names=FALSE)
936 Sys.sleep(0.15)
937 timerCallbacks$executeElapsed()
938 expect_equal(isolate(rfr()), cars)
939 flushReact()
940 expect_equal(isolate(rfr()), rbind(cars, cars))
941 })
942
943
944 test_that("classes of reactive object", {
945 v <- reactiveValues(a = 1)
946 r <- reactive({ v$a + 1 })
947 o <- observe({ print(r()) })
948
949 expect_false(is.reactivevalues(12))
950 expect_true(is.reactivevalues(v))
951 expect_false(is.reactivevalues(r))
952 expect_false(is.reactivevalues(o))
953
954 expect_false(is.reactive(12))
955 expect_false(is.reactive(v))
956 expect_true(is.reactive(r))
957 expect_false(is.reactive(o))
958
959 o$destroy()
960 })
961
962 test_that("{} and NULL also work in reactive()", {
963 expect_no_error(reactive({}))
964 expect_no_error(reactive(NULL))
965 })
966
967 test_that("shiny.suppressMissingContextError option works", {
968 options(shiny.suppressMissingContextError=TRUE)
969 on.exit(options(shiny.suppressMissingContextError=FALSE), add = TRUE)
970
971 expect_true(reactive(TRUE)())
972 })
973
974 test_that("reactive domains are inherited", {
975
976 domainA <- createMockDomain()
977 domainB <- createMockDomain()
978
979 local({
980 domainY <- NULL
981 domainZ <- NULL
982 x <- observe({
983
984 y <- observe({
985 # Should be domainA (inherited from observer x)
986 domainY <<- getDefaultReactiveDomain()
987 })
988
989 z <- observe({
990 # Should be domainB (explicitly passed in)
991 domainZ <<- getDefaultReactiveDomain()
992 }, domain = domainB)
993
994 }, domain = domainA)
995
996 flushReact()
997 flushReact()
998
999 expect_identical(domainY, domainA)
1000 expect_identical(domainZ, domainB)
1001 })
1002
1003 local({
1004 domainY <- 1
1005 x <- NULL
1006 y <- NULL
1007 z <- NULL
1008 r3 <- NULL
1009 domainR3 <- NULL
1010
1011 r1 <- reactive({
1012 y <<- observe({
1013 # Should be NULL (r1 has no domain)
1014 domainY <<- getDefaultReactiveDomain()
1015 })
1016 })
1017 r2 <- reactive({
1018 z <<- observe({
1019 # Should be domainB (r2 has explicit domainB)
1020 domainZ <<- getDefaultReactiveDomain()
1021 })
1022 }, domain = domainB)
1023
1024 observe({
1025 r3 <<- reactive({
1026 # This should be domainA. Doesn't matter where r3 is invoked, it only
1027 # matters where it was created.
1028 domainR3 <<- getDefaultReactiveDomain()
1029 })
1030 r1()
1031 r2()
1032 }, domain = domainA)
1033
1034 flushReact()
1035 flushReact()
1036 isolate(r3())
1037
1038 expect_identical(execCount(y), 1L)
1039 expect_identical(execCount(z), 1L)
1040 expect_identical(domainY, NULL)
1041 expect_identical(domainZ, domainB)
1042 expect_identical(domainR3, domainA)
1043 })
1044 })
1045
1046 test_that("observers autodestroy (or not)", {
1047
1048 domainA <- createMockDomain()
1049 local({
1050 a <- observe(NULL, domain = domainA)
1051
1052 b <- observe(NULL, domain = domainA, autoDestroy = FALSE)
1053
1054 c <- observe(NULL, domain = domainA)
1055 c$setAutoDestroy(FALSE)
1056
1057 d <- observe(NULL, domain = domainA, autoDestroy = FALSE)
1058 d$setAutoDestroy(TRUE)
1059
1060 e <- observe(NULL)
1061
1062 domainA$end()
1063
1064 flushReact()
1065
1066 expect_identical(execCount(a), 0L)
1067 expect_identical(execCount(b), 1L)
1068 expect_identical(execCount(c), 1L)
1069 expect_identical(execCount(d), 0L)
1070 expect_identical(execCount(e), 1L)
1071 })
1072 })
1073
1074 test_that("observers are garbage collected when destroyed", {
1075 domain <- createMockDomain()
1076 rv <- reactiveValues(x = 1)
1077
1078 # Auto-destroy. GC on domain end.
1079 a <- observe(rv$x, domain = domain)
1080 # No auto-destroy. GC with rv.
1081 b <- observe(rv$x, domain = domain, autoDestroy = FALSE)
1082 # No auto-destroy and no reactive dependencies. GC immediately.
1083 c <- observe({}, domain = domain)
1084 c$setAutoDestroy(FALSE)
1085 # Similar to b, but we'll set it to autoDestroy later.
1086 d <- observe(rv$x, domain = domain, autoDestroy = FALSE)
1087 # Like a, but we'll destroy it immediately.
1088 e <- observe(rx$x, domain = domain)
1089 e$destroy()
1090
1091 collected <- new.env(parent = emptyenv())
1092
1093 reg.finalizer(a, function(o) collected$a <- TRUE)
1094 reg.finalizer(b, function(o) collected$b <- TRUE)
1095 reg.finalizer(c, function(o) collected$c <- TRUE)
1096 reg.finalizer(d, function(o) collected$d <- TRUE)
1097 reg.finalizer(e, function(o) collected$e <- TRUE)
1098
1099 rm(list = c("a", "b", "c", "e")) # Not "d"
1100
1101 gc()
1102 # Nothing can be GC'd yet, because all of the observers are
1103 # pending execution (i.e. waiting for flushReact).
1104 expect_equal(ls(collected), character())
1105
1106 flushReact()
1107 # Now "c" can be garbage collected, because it ran and took
1108 # no dependencies (and isn't tied to the session in any way).
1109 # And "e" can also be garbage collected, it's been destroyed.
1110 gc()
1111 expect_equal(ls(collected), c("c", "e"))
1112
1113 domain$end()
1114 # We can GC "a" as well; even though it references rv, it is
1115 # destroyed when the session ends.
1116 gc()
1117 expect_equal(sort(ls(collected)), c("a", "c", "e"))
1118
1119 # It's OK to turn on auto-destroy even after the session was
1120 # destroyed.
1121 d$setAutoDestroy(TRUE)
1122 # This should no-op.
1123 d$setAutoDestroy(FALSE)
1124 rm(d)
1125 gc()
1126 expect_equal(sort(ls(collected)), c("a", "c", "d", "e"))
1127
1128 rm(rv)
1129 # Both rv and "b" can now be collected.
1130 gc()
1131 expect_equal(sort(ls(collected)), c("a", "b", "c", "d", "e"))
1132 })
1133
1134 test_that("maskReactiveContext blocks use of reactives", {
1135 vals <- reactiveValues(x = 123)
1136
1137 # Block reactive contexts (created by isolate)
1138 expect_error(isolate(maskReactiveContext(vals$x)))
1139 expect_error(isolate(isolate(maskReactiveContext(vals$x))))
1140
1141 # Reactive contexts within maskReactiveContext shouldn't be blocked
1142 expect_identical(maskReactiveContext(isolate(vals$x)), 123)
1143 expect_identical(isolate(maskReactiveContext(isolate(vals$x))), 123)
1144 })
1145
1146 test_that("Flush completes even when errors occur", {
1147 vals <- reactiveValues(x = 1)
1148
1149 r <- reactive({
1150 if (vals$x == 0) stop("x is zero!")
1151 else vals$x
1152 })
1153
1154 # Set up counters
1155 n11 <- n12 <- n21 <- n22 <- 0
1156
1157 observe({
1158 n11 <<- n11 + 1
1159 r()
1160 n12 <<- n12 + 1
1161 })
1162 observe({
1163 n21 <<- n21 + 1
1164 r()
1165 n22 <<- n22 + 1
1166 })
1167
1168 flushReact()
1169 expect_true(all(c(n11, n12, n21, n22) == 1))
1170
1171 # Trigger an error
1172 vals$x <- 0
1173 suppress_stacktrace(
1174 # Errors in reactive are translated to warnings in observers by default
1175 expect_warning(expect_warning(flushReact()))
1176 )
1177 # Both observers should run up until the reactive that errors
1178 expect_true(all(c(n11, n12, n21, n22) == c(2,1,2,1)))
1179
1180 # Nothing should happen on next flush
1181 flushReact()
1182 expect_true(all(c(n11, n12, n21, n22) == c(2,1,2,1)))
1183 })
1184
1185 test_that("event handling helpers take correct dependencies", {
1186 vals <- reactiveValues(action = NULL, x = 1)
1187
1188 o1_count <- 0
1189 o1 <- observeEvent(vals$action, {
1190 vals$x
1191 o1_count <<- o1_count + 1
1192 })
1193 o2_count <- 0
1194 o2 <- observeEvent(ignoreNULL = FALSE, vals$action, {
1195 vals$x
1196 o2_count <<- o2_count + 1
1197 })
1198 r1 <- eventReactive(vals$action, {
1199 vals$x
1200 })
1201 r2 <- eventReactive(ignoreNULL = FALSE, vals$action, {
1202 vals$x
1203 })
1204
1205 flushReact()
1206
1207 expect_error(isolate(r1()))
1208 expect_identical(isolate(r2()), 1)
1209 expect_equal(o1_count, 0)
1210 expect_equal(o2_count, 1)
1211 expect_equal(execCount(o1), 1)
1212 expect_equal(execCount(o2), 1)
1213
1214 vals$x <- 2
1215 flushReact()
1216
1217 expect_error(isolate(r1()))
1218 expect_identical(isolate(r2()), 1)
1219 expect_equal(o1_count, 0)
1220 expect_equal(o2_count, 1)
1221 expect_equal(execCount(o1), 1)
1222 expect_equal(execCount(o2), 1)
1223
1224 vals$action <- 1
1225 flushReact()
1226 expect_identical(isolate(r1()), 2)
1227 expect_identical(isolate(r2()), 2)
1228 expect_equal(o1_count, 1)
1229 expect_equal(o2_count, 2)
1230 expect_equal(execCount(o1), 2)
1231 expect_equal(execCount(o2), 2)
1232 })
1233
1234
1235 for (do_priming in c(TRUE, FALSE)) {
1236 label <- if (do_priming) "with priming" else "without priming"
1237 test_that(sprintf("debounce/throttle work properly (%s)", label), {
1238 # Some of the CRAN test machines are heavily loaded and so the timing for
1239 # these tests isn't reliable. https://github.com/rstudio/shiny/pull/2789
1240 skip_on_cran()
1241
1242 # The changing of rv$a will be the (chatty) source of reactivity.
1243 rv <- reactiveValues(a = 0)
1244
1245 # This observer will be what changes rv$a.
1246 src <- observe({
1247 invalidateLater(300)
1248 rv$a <- isolate(rv$a) + 1
1249 })
1250 on.exit(src$destroy(), add = TRUE)
1251
1252 # Make a debounced reactive to test.
1253 dr <- debounce(reactive(rv$a), 500)
1254
1255 # Make a throttled reactive to test.
1256 tr <- throttle(reactive(rv$a), 500)
1257
1258 # Keep track of how often dr/tr are fired
1259 dr_fired <- 0
1260 dr_monitor <- observeEvent(dr(), {
1261 dr_fired <<- dr_fired + 1
1262 })
1263 on.exit(dr_monitor$destroy(), add = TRUE)
1264
1265 tr_fired <- 0
1266 tr_monitor <- observeEvent(tr(), {
1267 tr_fired <<- tr_fired + 1
1268 })
1269 on.exit(tr_monitor$destroy(), add = TRUE)
1270
1271 # Starting values are both 0. Earlier I found that the tests behaved
1272 # differently if I accessed the values of dr/tr before the first call to
1273 # flushReact(). That bug was fixed, but to ensure that similar bugs don't
1274 # appear undetected, we run this test with and without do_priming.
1275 if (do_priming) {
1276 expect_identical(isolate(dr()), 0)
1277 expect_identical(isolate(tr()), 0)
1278 }
1279
1280 # Pump timer and reactives for about 1.3 seconds
1281 stopAt <- Sys.time() + 1.3
1282 while (Sys.time() < stopAt) {
1283 timerCallbacks$executeElapsed()
1284 flushReact()
1285 Sys.sleep(0.001)
1286 }
1287
1288 # dr() should not have had time to fire, other than the initial run, since
1289 # there haven't been long enough gaps between invalidations.
1290 expect_identical(dr_fired, 1)
1291 # The value of dr() should not have updated either.
1292 expect_identical(isolate(dr()), 0)
1293
1294 # tr() however, has had time to fire multiple times and update its value.
1295 expect_identical(tr_fired, 3)
1296 expect_identical(isolate(tr()), 4)
1297
1298 # Now let some time pass without any more updates.
1299 src$destroy() # No more updates
1300 stopAt <- Sys.time() + 1
1301 while (Sys.time() < stopAt) {
1302 timerCallbacks$executeElapsed()
1303 flushReact()
1304 Sys.sleep(0.001)
1305 }
1306
1307 # dr should've fired, and we should have converged on the right answer.
1308 expect_identical(dr_fired, 2)
1309 isolate(expect_identical(rv$a, dr()))
1310 # be sure tr() converged on the right answer; (We've already confirmed throttle-like behavior above)
1311 isolate(expect_identical(rv$a, tr()))
1312 })
1313 }
1314
1315 test_that("reactive domain works across async handlers", {
1316 obj <- new.env()
1317 hasReactiveDomain <- NULL
1318 withReactiveDomain(obj, {
1319 promises::then(
1320 promises::promise_resolve(TRUE),
1321 ~{hasReactiveDomain <<- identical(getDefaultReactiveDomain(), obj)}
1322 )
1323 })
1324
1325 while (is.null(hasReactiveDomain) && !later::loop_empty()) {
1326 later::run_now()
1327 }
1328
1329 testthat::expect_true(hasReactiveDomain)
1330 })
1331
1332 # For #2441, #2423
1333 test_that("Unreachable reactives are GC'd", {
1334 v <- reactiveVal(1)
1335 r <- reactive({
1336 v()
1337 12345
1338 })
1339 o <- observe({
1340 r()
1341 })
1342 # Finalizer on the reactive's underlying Observable object
1343 r_finalized <- FALSE
1344 reg.finalizer(attr(r, "observable"), function(e) {
1345 r_finalized <<- TRUE
1346 })
1347
1348 # Finalizer on the Observer
1349 o_finalized <- FALSE
1350 reg.finalizer(o, function(e) {
1351 o_finalized <<- TRUE
1352 })
1353
1354 flushReact()
1355 gc()
1356 expect_false(r_finalized)
1357
1358 rm(r) # Remove the only (strong) reference to r
1359 gc()
1360 expect_true(r_finalized)
1361 expect_false(o_finalized)
1362
1363 rm(o) # Remove the only reference to o
1364 gc()
1365 expect_true(o_finalized)
1366
1367 rm(v)
1368 gc()
1369
1370 # Same, with reactiveValues instead of reactiveVal
1371 v <- reactiveValues(x = 1)
1372 r <- reactive({
1373 v$x
1374 12345
1375 })
1376 o <- observe({
1377 r()
1378 })
1379 # Finalizer on the reactive's underlying Observable object
1380 r_finalized <- FALSE
1381 reg.finalizer(attr(r, "observable"), function(e) {
1382 r_finalized <<- TRUE
1383 })
1384
1385 # Finalizer on the Observer
1386 o_finalized <- FALSE
1387 reg.finalizer(o, function(e) {
1388 o_finalized <<- TRUE
1389 })
1390
1391 flushReact()
1392 gc()
1393 expect_false(r_finalized)
1394
1395 rm(r) # Remove the only (strong) reference to r
1396 gc()
1397 expect_true(r_finalized)
1398 expect_false(o_finalized)
1399
1400 rm(o) # Remove the only reference to o
1401 gc()
1402 expect_true(o_finalized)
1403 })
1404
1405
1406
1407 test_that("Reactive contexts are not GC'd too early", {
1408 # When a ReactiveVal or ReactiveValue has an dependency arrow pointing to a
1409 # reactive expression (Observable object), it's implemented by having a weak
1410 # reference to a reactive context. We need to make sure that the reactive
1411 # context is not GC'd too early. This is done by having the Observable have a
1412 # strong reference to the context.
1413
1414 # Check reactiveVal
1415 v <- reactiveVal(1)
1416 r <- reactive({
1417 v()
1418 })
1419 o <- observe({
1420 r()
1421 gc()
1422 })
1423 # Finalizer on the reactive's underlying Observable object
1424 r_finalized <- FALSE
1425 reg.finalizer(attr(r, "observable"), function(e) {
1426 r_finalized <<- TRUE
1427 })
1428
1429 for (i in 1:3) {
1430 v(isolate(v()) + 1)
1431 flushReact()
1432 }
1433
1434 expect_identical(execCount(r), 3L)
1435 expect_false(r_finalized)
1436 o$destroy()
1437 rm(v, r, o)
1438 gc()
1439 expect_true(r_finalized)
1440
1441
1442 # Same, but with reactiveValues
1443 v <- reactiveValues(x=1)
1444 r <- reactive({
1445 v$x
1446 })
1447 o <- observe({
1448 r()
1449 gc()
1450 })
1451 # Finalizer on the reactive's underlying Observable object
1452 r_finalized <- FALSE
1453 reg.finalizer(attr(r, "observable"), function(e) {
1454 r_finalized <<- TRUE
1455 })
1456
1457 for (i in 1:3) {
1458 v$x <- (isolate(v$x) + 1)
1459 flushReact()
1460 }
1461
1462 expect_identical(execCount(r), 3L)
1463 expect_false(r_finalized)
1464 })
1465
1466
1467 test_that("reactivePoll doesn't leak observer (#1548)", {
1468 i <- 0
1469 count <- reactivePoll(50, NULL,
1470 checkFunc = function() {
1471 i <<- i + 1
1472 i
1473 },
1474 valueFunc = function() i
1475 )
1476
1477 observe({
1478 count()
1479 })
1480
1481 while (i < 3) {
1482 Sys.sleep(0.05)
1483 shiny:::timerCallbacks$executeElapsed()
1484 shiny:::flushReact()
1485 }
1486
1487 # Removing the reference to count means that no one can use it anymore, and so
1488 # the finalizer should run. The finalizer sets a flag which will allow the
1489 # observer (which calls `checkFunc`) to run one more time; in that run, it
1490 # will remove itself.
1491 rm(count)
1492 gc()
1493
1494 # If the reactivePoll was cleaned up, then the first run of this loop will
1495 # increment i (bringing its value to 4), but in that run, the observer will
1496 # remove itself so subsequent runs will no longer run `checkFunc`.
1497 for (n in 1:3) {
1498 Sys.sleep(0.05)
1499 shiny:::timerCallbacks$executeElapsed()
1500 shiny:::flushReact()
1501 }
1502
1503 expect_equal(i, 3L)
1504 })
1505
1506 test_that("reactivePoll prefers session$scheduleTask", {
1507 called <- 0
1508 session <- list(reactlog = function(...){}, onEnded = function(...){}, .scheduleTask = function(millis, cb){
1509 expect_equal(millis, 50)
1510 called <<- called + 1
1511 })
1512
1513 count <- reactivePoll(50, session, function(){}, function(){})
1514 observe({
1515 count()
1516 })
1517
1518 for (i in 1:4) {
1519 Sys.sleep(0.05)
1520 shiny:::flushReact()
1521 }
1522 expect_gt(called, 0)
1523 })
1524
1525 test_that("invalidateLater prefers session$scheduleTask", {
1526 called <- 0
1527 session <- list(reactlog = function(...){}, onEnded = function(...){}, .scheduleTask = function(millis, cb){
1528 expect_equal(millis, 10)
1529 called <<- called + 1
1530 })
1531
1532 observe({
1533 invalidateLater(10, session)
1534 })
1535
1536 for (i in 1:4) {
1537 Sys.sleep(0.05)
1538 shiny:::flushReact()
1539 }
1540 expect_gt(called, 0)
1541 })
1542
1543 test_that("reactiveTimer prefers session$scheduleTask", {
1544 called <- 0
1545 session <- list(reactlog = function(...){}, onEnded = function(...){}, .scheduleTask = function(millis, cb){
1546 expect_equal(millis, 10)
1547 called <<- called + 1
1548 })
1549
1550 rt <- reactiveTimer(10, session)
1551 observe({
1552 rt()
1553 })
1554
1555 for (i in 1:4) {
1556 Sys.sleep(0.05)
1557 shiny:::flushReact()
1558 }
1559 expect_gt(called, 0)
1560 })
1561
1562
1563 test_that("Reactive expression visibility", {
1564 res <- NULL
1565 rv <- reactive(1)
1566 o <- observe({
1567 res <<- withVisible(rv())
1568 })
1569 flushReact()
1570 expect_identical(res, list(value = 1, visible = TRUE))
1571
1572
1573 res <- NULL
1574 rv <- reactive(invisible(1))
1575 o <- observe({
1576 res <<- withVisible(rv())
1577 })
1578 flushReact()
1579 expect_identical(res, list(value = 1, visible = FALSE))
1580
1581 # isolate
1582 expect_identical(
1583 withVisible(isolate(1)),
1584 list(value = 1, visible = TRUE)
1585 )
1586 expect_identical(
1587 withVisible(isolate(invisible(1))),
1588 list(value = 1, visible = FALSE)
1589 )
1590 })
1591
1592
1593 test_that("Reactive expression labels", {
1594 r <- list()
1595
1596 # Automatic label
1597 r$x <- reactive({
1598 a+1;b+ 2
1599 })
1600 # Printed output - uses expression, not `label`
1601 expect_identical(
1602 capture.output(print(r$x)),
1603 c("reactive({", " a + 1", " b + 2", "}) ")
1604 )
1605 # Label used for debugging
1606 expect_identical(
1607 as.character(attr(r$x, "observable")$.label),
1608 "r$x"
1609 )
1610
1611 # With explicit label
1612 r$y <- reactive({ a+1;b+ 2 }, label = "hello")
1613 expect_identical(
1614 capture.output(print(r$y)),
1615 c("reactive({", " a + 1", " b + 2", "}) ")
1616 )
1617 expect_identical(
1618 as.character(attr(r$y, "observable")$.label),
1619 "hello"
1620 )
1621 })
1622
1623
1624 test_that("Contexts can be masked off", {
1625 expect_error(
1626 {
1627 r <- reactiveVal()
1628 isolate({
1629 maskReactiveContext({
1630 r()
1631 })
1632 })
1633 },
1634 regexp = "Operation not allowed without an active reactive context"
1635 )
1636 })
1637
1638
1639 test_that("Contexts can be masked off via promise domains", {
1640 r <- reactiveVal()
1641 done <- FALSE
1642 isolate({
1643 maskReactiveContext({
1644 promises::promise_resolve(NULL)$then(function(value) {
1645 done <<- TRUE
1646 expect_error(
1647 {
1648 r()
1649 },
1650 regexp = "Operation not allowed without an active reactive context"
1651 )
1652 })
1653 })
1654 })
1655 while (!done) {
1656 later::run_now(all=FALSE)
1657 }
1658 })