Debian Codegraph @

tests/testthat/test-otel-reactive-update.R

raw
1 # Tests for otel-reactive-update.R functions
2
3 # Helper function to create a mock otel span
4 create_mock_otel_span <- function(name, attributes = NULL, ended = FALSE) {
5 structure(
6 list(name = name, attributes = attributes, ended = ended),
7 class = c("mock_otel_span", "otel_span")
8 )
9 }
10
11 test_that("otel_span_reactive_update_init returns early when otel not enabled", {
12 domain <- MockShinySession$new()
13
14 # Convince has_otel_collect to return FALSE
15 withr::local_options(list(shiny.otel.collect = "none"))
16
17 # Should return early without creating span
18 result <- otel_span_reactive_update_init(domain = domain)
19 expect_null(result)
20 expect_null(domain$userData[["_otel_span_reactive_update"]])
21 })
22
23 test_that("otel_span_reactive_update_init sets up session cleanup on first call", {
24 callback_added <- FALSE
25 TestMockShinySession <- R6::R6Class(
26 "TestMockShinySession",
27 inherit = MockShinySession,
28 portable = FALSE,
29 lock_objects = FALSE,
30 public = list(
31 # Mock onSessionEnded to track if callback is added
32 onSessionEnded = function(callback) {
33 callback_added <<- TRUE
34 expect_true(is.function(callback))
35 }
36 )
37 )
38 domain <- TestMockShinySession$new()
39
40 withr::local_options(list(shiny.otel.collect = "reactive_update"))
41
42 local_mocked_bindings(
43 has_otel_collect = function(level) level == "reactive_update",
44 start_otel_span = function(name, ..., attributes = NULL) create_mock_otel_span(name, attributes = attributes),
45 otel_session_id_attrs = function(domain) list(session_id = "mock-session-id")
46 )
47
48 otel_span_reactive_update_init(domain = domain)
49
50 expect_true(callback_added)
51 expect_true(domain$userData[["_otel_has_reactive_cleanup"]])
52 expect_equal(
53 domain$userData[["_otel_span_reactive_update"]],
54 create_mock_otel_span("reactive_update", attributes = list(session_id = "mock-session-id"))
55 )
56 })
57
58 test_that("otel_span_reactive_update_init errors when span already exists", {
59 domain <- MockShinySession$new()
60 domain$token <- "mock-session-token"
61
62 # Set up existing span
63 existing_otel_span <- create_mock_otel_span("reactive_update", attributes = list(session.id = "mock-session-token"))
64 domain$userData[["_otel_span_reactive_update"]] <- existing_otel_span
65
66 local_mocked_bindings(
67 has_otel_collect = function(level) level == "reactive_update"
68 )
69
70 expect_error(
71 otel_span_reactive_update_init(domain = domain),
72 "Reactive update span already exists"
73 )
74 })
75
76 test_that("otel_span_reactive_update_init doesn't setup cleanup twice", {
77 TestMockShinySession <- R6::R6Class(
78 "TestMockShinySession",
79 inherit = MockShinySession,
80 portable = FALSE,
81 lock_objects = FALSE,
82 public = list(
83 # Mock onSessionEnded to track how many times callback is added
84 callback_count = 0,
85 onSessionEnded = function(callback) {
86 self$callback_count <- self$callback_count + 1
87 expect_true(is.function(callback))
88 }
89 )
90 )
91 domain <- TestMockShinySession$new()
92
93 # Set cleanup flag manually
94 domain$userData[["_otel_has_reactive_cleanup"]] <- TRUE
95
96 local_mocked_bindings(
97 has_otel_collect = function(level) level == "reactive_update",
98 start_otel_span = function(...) create_mock_otel_span("reactive_update")
99 )
100
101 otel_span_reactive_update_init(domain = domain)
102
103 # Should not have called onSessionEnded since cleanup was already set
104 expect_equal(domain$callback_count, 0)
105 })
106
107 test_that("otel_span_reactive_update_teardown ends span when it exists", {
108 domain <- MockShinySession$new()
109 mock_otel_span <- create_mock_otel_span("reactive_update")
110 domain$userData[["_otel_span_reactive_update"]] <- mock_otel_span
111
112 span_ended <- FALSE
113
114 local_mocked_bindings(
115 end_span = function(span) {
116 span_ended <<- TRUE
117 expect_equal(span, mock_otel_span)
118 },
119 .package = "otel"
120 )
121
122 otel_span_reactive_update_teardown(domain = domain)
123
124 expect_true(span_ended)
125 expect_null(domain$userData[["_otel_span_reactive_update"]])
126 })
127
128 test_that("otel_span_reactive_update_teardown handles missing span gracefully", {
129 domain <- MockShinySession$new()
130
131 # No span exists
132 expect_null(domain$userData[["_otel_span_reactive_update"]])
133
134 # Should not error
135 expect_no_error(otel_span_reactive_update_teardown(domain = domain))
136 })
137
138 test_that("with_otel_span_reactive_update executes expr without span", {
139 domain <- MockShinySession$new()
140
141 # No span exists
142 test_value <- "initial"
143
144 local_mocked_bindings(
145 is_otel_span = function(x) FALSE
146 )
147
148 result <- with_otel_span_reactive_update({
149 test_value <- "modified"
150 "result_value"
151 }, domain = domain)
152
153 expect_equal(result, "result_value")
154 expect_equal(test_value, "modified")
155 })
156
157 test_that("with_otel_span_reactive_update executes expr with active span", {
158 domain <- MockShinySession$new()
159 mock_otel_span <- create_mock_otel_span("reactive_update")
160 domain$userData[["_otel_span_reactive_update"]] <- mock_otel_span
161
162 span_was_active <- FALSE
163 test_value <- "initial"
164
165 local_mocked_bindings(
166 with_active_span = function(span, expr) {
167 span_was_active <<- TRUE
168 expect_equal(span, mock_otel_span)
169 force(expr)
170 },
171 .package = "otel"
172 )
173
174 result <- with_otel_span_reactive_update({
175 test_value <- "modified"
176 "result_value"
177 }, domain = domain)
178
179 expect_true(span_was_active)
180 expect_equal(result, "result_value")
181 expect_equal(test_value, "modified")
182 })
183
184 test_that("session cleanup callback works correctly", {
185 TestMockShinySession <- R6::R6Class(
186 "TestMockShinySession",
187 inherit = MockShinySession,
188 portable = FALSE,
189 lock_objects = FALSE,
190 public = list(
191 # Mock onSessionEnded to capture the callback
192 onSessionEnded = function(callback) {
193 self$cleanup_callback <<- callback
194 },
195 cleanup_callback = NULL
196 )
197 )
198 domain <- TestMockShinySession$new()
199 mock_otel_span <- create_mock_otel_span("reactive_update")
200
201 with_mocked_bindings(
202 has_otel_collect = function(level) level == "reactive_update",
203 start_otel_span = function(...) mock_otel_span,
204 otel_session_id_attrs = function(domain) list(session_id = "test"),
205 {
206 otel_span_reactive_update_init(domain = domain)
207 }
208 )
209
210 # Verify cleanup callback was registered
211 expect_true(is.function(domain$cleanup_callback))
212
213 # Set up span and test cleanup
214 domain$userData[["_otel_span_reactive_update"]] <- mock_otel_span
215 domain$userData[["_otel_has_reactive_cleanup"]] <- TRUE
216
217 span_ended <- FALSE
218
219 with_mocked_bindings(
220 otel_span_reactive_update_teardown = function(domain = NULL) {
221 span_ended <<- TRUE
222 },
223 {
224 # Execute the cleanup callback
225 domain$cleanup_callback()
226 expect_true(span_ended)
227 }
228 )
229 })