| 1 |
# Do not move or rearrange this code - it defines helper functions used in multiple tests below |
| 2 |
get_reactive_objects <- function() { |
| 3 |
# Must use variables, otherwise the source reference is collapsed to a single line |
| 4 |
r <- reactive({ 42 }) |
| 5 |
rv <- reactiveVal("test") |
| 6 |
rvs <- reactiveValues(a = 1) |
| 7 |
o <- observe({ 43 }) |
| 8 |
rt <- renderText({ "text" }) |
| 9 |
oe <- observeEvent({"key"}, { 45 }) |
| 10 |
er <- eventReactive({"key"}, { 46 }) |
| 11 |
|
| 12 |
# Values below this line are to test file location, not file line |
| 13 |
r1a <- reactive({ 1 }) |> bindCache({"key"}) |
| 14 |
r2a <- reactive({ 2 }) |> bindEvent({"key"}) |
| 15 |
r3a <- reactive({ 3 }) |> bindCache({"key1"}) |> bindEvent({"key2"}) |
| 16 |
r1b <- bindCache(reactive({ 1 }), {"key"}) |
| 17 |
r2b <- bindEvent(reactive({ 2 }), {"key"}) |
| 18 |
r3b <- bindEvent(bindCache(reactive({ 3 }), {"key1"}), {"key2"}) |
| 19 |
|
| 20 |
rt1a <- renderText({"text"}) |> bindCache({"key"}) |
| 21 |
rt2a <- renderText({"text"}) |> bindEvent({"key"}) |
| 22 |
rt3a <- renderText({"text"}) |> bindCache({"key1"}) |> bindEvent({"key2"}) |
| 23 |
rt1b <- bindCache(renderText({"text"}), {"key"}) |
| 24 |
rt2b <- bindEvent(renderText({"text"}), {"key"}) |
| 25 |
rt3b <- bindEvent(bindCache(renderText({"text"}), {"key1"}), {"key2"}) |
| 26 |
|
| 27 |
o2a <- observe({ 44 }) |> bindEvent({"key"}) |
| 28 |
o2b <- bindEvent(observe({ 47 }), {"key"}) |
| 29 |
|
| 30 |
# Debounce and throttle |
| 31 |
r_debounce <- reactive({ 48 }) |> debounce(1000) |
| 32 |
r_throttle <- reactive({ 49 }) |> throttle(1000) |
| 33 |
|
| 34 |
# ExtendedTask |
| 35 |
ext_task <- ExtendedTask$new(function() { promises::promise_resolve(50) }) |
| 36 |
|
| 37 |
# Reactive with explicit label |
| 38 |
r_labeled <- reactive({ 51 }, label = "my_reactive") |
| 39 |
o_labeled <- observe({ 52 }, label = "my_observer") |
| 40 |
|
| 41 |
# Poll and File |
| 42 |
r_poll <- reactivePoll(1000, NULL, checkFunc = function() { TRUE}, valueFunc = function() { 53 }) |
| 43 |
r_file <- reactiveFileReader(1000, NULL, filePath = "path/to/file") |
| 44 |
|
| 45 |
list( |
| 46 |
reactive = r, |
| 47 |
reactiveVal = rv, |
| 48 |
reactiveValues = rvs, |
| 49 |
observe = o, |
| 50 |
renderText = rt, |
| 51 |
observeEvent = oe, |
| 52 |
eventReactive = er, |
| 53 |
reactiveCacheA = r1a, |
| 54 |
reactiveEventA = r2a, |
| 55 |
reactiveCacheEventA = r3a, |
| 56 |
reactiveCacheB = r1b, |
| 57 |
reactiveEventB = r2b, |
| 58 |
reactiveCacheEventB = r3b, |
| 59 |
renderCacheA = rt1a, |
| 60 |
renderEventA = rt2a, |
| 61 |
renderCacheEventA = rt3a, |
| 62 |
renderCacheB = rt1b, |
| 63 |
renderEventB = rt2b, |
| 64 |
renderCacheEventB = rt3b, |
| 65 |
observeEventA = o2a, |
| 66 |
observeEventB = o2b, |
| 67 |
debounce = r_debounce, |
| 68 |
throttle = r_throttle, |
| 69 |
extendedTask = ext_task, |
| 70 |
reactiveLabeled = r_labeled, |
| 71 |
observeLabeled = o_labeled, |
| 72 |
reactivePoll = r_poll, |
| 73 |
reactiveFileReader = r_file |
| 74 |
) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
# Helper function to create a mock srcref |
| 80 |
create_mock_srcref <- function( |
| 81 |
lines = c(10, 15), |
| 82 |
columns = c(5, 20), |
| 83 |
filename = "test_file.R" |
| 84 |
) { |
| 85 |
srcfile <- list(filename = filename) |
| 86 |
srcref <- structure( |
| 87 |
c(lines[1], columns[1], lines[2], columns[2], columns[1], columns[2]), |
| 88 |
class = "srcref" |
| 89 |
) |
| 90 |
attr(srcref, "srcfile") <- srcfile |
| 91 |
srcref |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
test_that("otel_srcref_attributes extracts attributes from srcref object", { |
| 96 |
srcref <- create_mock_srcref( |
| 97 |
lines = c(15, 18), |
| 98 |
columns = c(8, 25), |
| 99 |
filename = "/path/to/myfile.R" |
| 100 |
) |
| 101 |
|
| 102 |
attrs <- otel_srcref_attributes(srcref) |
| 103 |
|
| 104 |
# Preferred attribute names |
| 105 |
expect_equal(attrs[["code.file.path"]], "/path/to/myfile.R") |
| 106 |
expect_equal(attrs[["code.line.number"]], 15) |
| 107 |
expect_equal(attrs[["code.column.number"]], 8) |
| 108 |
expect_false("code.function.name" %in% names(attrs)) |
| 109 |
|
| 110 |
# Deprecated attribute names (for backward compatibility) |
| 111 |
expect_equal(attrs[["code.filepath"]], "/path/to/myfile.R") |
| 112 |
expect_equal(attrs[["code.lineno"]], 15) |
| 113 |
expect_equal(attrs[["code.column"]], 8) |
| 114 |
|
| 115 |
# Test with function name |
| 116 |
attrs_with_fn <- otel_srcref_attributes(srcref, fn_name = "myFunction") |
| 117 |
|
| 118 |
# Preferred names |
| 119 |
expect_equal(attrs_with_fn[["code.file.path"]], "/path/to/myfile.R") |
| 120 |
expect_equal(attrs_with_fn[["code.line.number"]], 15) |
| 121 |
expect_equal(attrs_with_fn[["code.column.number"]], 8) |
| 122 |
expect_equal(attrs_with_fn[["code.function.name"]], "myFunction") |
| 123 |
|
| 124 |
# Deprecated names |
| 125 |
expect_equal(attrs_with_fn[["code.filepath"]], "/path/to/myfile.R") |
| 126 |
expect_equal(attrs_with_fn[["code.lineno"]], 15) |
| 127 |
expect_equal(attrs_with_fn[["code.column"]], 8) |
| 128 |
}) |
| 129 |
|
| 130 |
test_that("otel_srcref_attributes handles NULL srcref", { |
| 131 |
attrs <- otel_srcref_attributes(NULL) |
| 132 |
expect_null(attrs) |
| 133 |
}) |
| 134 |
|
| 135 |
test_that("otel_srcref_attributes extracts from function with srcref", { |
| 136 |
mock_func <- function() { "test" } |
| 137 |
srcref <- create_mock_srcref( |
| 138 |
lines = c(42, 45), |
| 139 |
columns = c(12, 30), |
| 140 |
filename = "function_file.R" |
| 141 |
) |
| 142 |
|
| 143 |
with_mocked_bindings( |
| 144 |
getSrcRefs = function(func) { |
| 145 |
expect_identical(func, mock_func) |
| 146 |
list(list(srcref)) |
| 147 |
}, |
| 148 |
{ |
| 149 |
attrs <- otel_srcref_attributes(mock_func) |
| 150 |
|
| 151 |
expect_equal(attrs[["code.file.path"]], "function_file.R") |
| 152 |
expect_equal(attrs[["code.line.number"]], 42) |
| 153 |
expect_equal(attrs[["code.column.number"]], 12) |
| 154 |
expect_false("code.function.name" %in% names(attrs)) |
| 155 |
|
| 156 |
# Test with function name |
| 157 |
attrs_with_fn <- otel_srcref_attributes( |
| 158 |
mock_func, |
| 159 |
fn_name = "testFunction" |
| 160 |
) |
| 161 |
|
| 162 |
expect_equal(attrs_with_fn[["code.file.path"]], "function_file.R") |
| 163 |
expect_equal(attrs_with_fn[["code.line.number"]], 42) |
| 164 |
expect_equal(attrs_with_fn[["code.column.number"]], 12) |
| 165 |
expect_equal(attrs_with_fn[["code.function.name"]], "testFunction") |
| 166 |
} |
| 167 |
) |
| 168 |
}) |
| 169 |
|
| 170 |
test_that("otel_srcref_attributes handles function without srcref", { |
| 171 |
mock_func <- function() { "test" } |
| 172 |
|
| 173 |
with_mocked_bindings( |
| 174 |
getSrcRefs = function(func) { |
| 175 |
list(list(NULL)) |
| 176 |
}, |
| 177 |
{ |
| 178 |
attrs <- otel_srcref_attributes(mock_func) |
| 179 |
expect_null(attrs) |
| 180 |
} |
| 181 |
) |
| 182 |
}) |
| 183 |
|
| 184 |
test_that("otel_srcref_attributes handles function with empty getSrcRefs", { |
| 185 |
mock_func <- function() { "test" } |
| 186 |
|
| 187 |
with_mocked_bindings( |
| 188 |
getSrcRefs = function(func) { |
| 189 |
list() # Empty list |
| 190 |
}, |
| 191 |
{ |
| 192 |
expect_error( |
| 193 |
otel_srcref_attributes(mock_func), |
| 194 |
"subscript out of bounds|attempt to select less than one element" |
| 195 |
) |
| 196 |
} |
| 197 |
) |
| 198 |
}) |
| 199 |
|
| 200 |
test_that("otel_srcref_attributes validates srcref class", { |
| 201 |
invalid_srcref <- structure( |
| 202 |
c(10, 5, 15, 20, 5, 20), |
| 203 |
class = "not_srcref" |
| 204 |
) |
| 205 |
|
| 206 |
expect_error( |
| 207 |
otel_srcref_attributes(invalid_srcref), |
| 208 |
"inherits\\(srcref, \"srcref\"\\) is not TRUE" |
| 209 |
) |
| 210 |
}) |
| 211 |
|
| 212 |
test_that("otel_srcref_attributes drops NULL values", { |
| 213 |
# Create srcref with missing filename |
| 214 |
srcref <- structure( |
| 215 |
c(10, 5, 15, 20, 5, 20), |
| 216 |
class = "srcref" |
| 217 |
) |
| 218 |
attr(srcref, "srcfile") <- list(filename = NULL) |
| 219 |
|
| 220 |
attrs <- otel_srcref_attributes(srcref) |
| 221 |
|
| 222 |
# Should only contain lineno and column (both preferred and deprecated) |
| 223 |
expect_equal(length(attrs), 4) # 2 preferred + 2 deprecated |
| 224 |
# Preferred names |
| 225 |
expect_equal(attrs[["code.line.number"]], 10) |
| 226 |
expect_equal(attrs[["code.column.number"]], 5) |
| 227 |
expect_false("code.file.path" %in% names(attrs)) |
| 228 |
expect_false("code.function.name" %in% names(attrs)) |
| 229 |
# Deprecated names |
| 230 |
expect_equal(attrs[["code.lineno"]], 10) |
| 231 |
expect_equal(attrs[["code.column"]], 5) |
| 232 |
expect_false("code.filepath" %in% names(attrs)) |
| 233 |
|
| 234 |
# Test with function name - NULL fn_name should still be dropped |
| 235 |
attrs_with_null_fn <- otel_srcref_attributes(srcref, fn_name = NULL) |
| 236 |
expect_equal(length(attrs_with_null_fn), 4) |
| 237 |
expect_false("code.function.name" %in% names(attrs_with_null_fn)) |
| 238 |
|
| 239 |
# Test with function name provided |
| 240 |
attrs_with_fn <- otel_srcref_attributes(srcref, fn_name = "testFunc") |
| 241 |
expect_equal(length(attrs_with_fn), 5) # 4 location + 1 function name |
| 242 |
expect_equal(attrs_with_fn[["code.function.name"]], "testFunc") |
| 243 |
}) |
| 244 |
|
| 245 |
test_that("otel_srcref_attributes handles missing srcfile", { |
| 246 |
srcref <- structure( |
| 247 |
c(10, 5, 15, 20, 5, 20), |
| 248 |
class = "srcref" |
| 249 |
) |
| 250 |
# No srcfile attribute |
| 251 |
|
| 252 |
attrs <- otel_srcref_attributes(srcref) |
| 253 |
|
| 254 |
# Should only contain lineno and column (both preferred and deprecated) |
| 255 |
expect_equal(length(attrs), 4) # 2 preferred + 2 deprecated |
| 256 |
# Preferred names |
| 257 |
expect_equal(attrs[["code.line.number"]], 10) |
| 258 |
expect_equal(attrs[["code.column.number"]], 5) |
| 259 |
expect_false("code.file.path" %in% names(attrs)) |
| 260 |
# Deprecated names |
| 261 |
expect_equal(attrs[["code.lineno"]], 10) |
| 262 |
expect_equal(attrs[["code.column"]], 5) |
| 263 |
expect_false("code.filepath" %in% names(attrs)) |
| 264 |
}) |
| 265 |
|
| 266 |
# Integration tests with reactive functions |
| 267 |
test_that("reactive() captures otel attributes from source reference", { |
| 268 |
# This test verifies that reactive() functions get otel attributes set |
| 269 |
# We'll need to mock the internals since we can't easily control srcref in tests |
| 270 |
|
| 271 |
x <- get_reactive_objects()$reactive |
| 272 |
attrs <- attr(x, "observable")$.otelAttrs |
| 273 |
|
| 274 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 275 |
expect_equal(attrs[["code.line.number"]], 4) |
| 276 |
expect_equal(attrs[["code.column.number"]], 3) |
| 277 |
expect_equal(attrs[["code.function.name"]], "reactive") |
| 278 |
}) |
| 279 |
|
| 280 |
test_that("reactiveVal() captures otel attributes from source reference", { |
| 281 |
x <- get_reactive_objects()$reactiveVal |
| 282 |
|
| 283 |
# Test the attribute extraction that would be used in reactiveVal |
| 284 |
attrs <- attr(x, ".impl")$.otelAttrs |
| 285 |
|
| 286 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 287 |
expect_equal(attrs[["code.line.number"]], 5) |
| 288 |
expect_equal(attrs[["code.column.number"]], 3) |
| 289 |
expect_equal(attrs[["code.function.name"]], "reactiveVal") |
| 290 |
}) |
| 291 |
|
| 292 |
test_that("reactiveValues() captures otel attributes from source reference", { |
| 293 |
x <- get_reactive_objects()$reactiveValues |
| 294 |
|
| 295 |
attrs <- .subset2(x, "impl")$.otelAttrs |
| 296 |
|
| 297 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 298 |
expect_equal(attrs[["code.line.number"]], 6) |
| 299 |
expect_equal(attrs[["code.column.number"]], 3) |
| 300 |
expect_equal(attrs[["code.function.name"]], "reactiveValues") |
| 301 |
}) |
| 302 |
|
| 303 |
test_that("observe() captures otel attributes from source reference", { |
| 304 |
x <- get_reactive_objects()$observe |
| 305 |
attrs <- x$.otelAttrs |
| 306 |
|
| 307 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 308 |
expect_equal(attrs[["code.line.number"]], 7) |
| 309 |
expect_equal(attrs[["code.column.number"]], 3) |
| 310 |
expect_equal(attrs[["code.function.name"]], "observe") |
| 311 |
}) |
| 312 |
|
| 313 |
test_that("otel attributes integration with render functions", { |
| 314 |
x <- get_reactive_objects()$renderText |
| 315 |
attrs <- attr(x, "otelAttrs") |
| 316 |
|
| 317 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 318 |
expect_equal(attrs[["code.line.number"]], 8) |
| 319 |
expect_equal(attrs[["code.column.number"]], 20) |
| 320 |
# Render functions should NOT have code.function.name |
| 321 |
expect_false("code.function.name" %in% names(attrs)) |
| 322 |
}) |
| 323 |
|
| 324 |
test_that("observeEvent() captures otel attributes from source reference", { |
| 325 |
x <- get_reactive_objects()$observeEvent |
| 326 |
attrs <- x$.otelAttrs |
| 327 |
|
| 328 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 329 |
expect_equal(attrs[["code.line.number"]], 9) |
| 330 |
expect_equal(attrs[["code.column.number"]], 3) |
| 331 |
expect_equal(attrs[["code.function.name"]], "observeEvent") |
| 332 |
}) |
| 333 |
|
| 334 |
test_that("otel attributes follow OpenTelemetry semantic conventions", { |
| 335 |
# Test that the attribute names follow the official OpenTelemetry conventions |
| 336 |
# https://opentelemetry.io/docs/specs/semconv/registry/attributes/code/ |
| 337 |
|
| 338 |
srcref <- create_mock_srcref( |
| 339 |
lines = c(1, 1), |
| 340 |
columns = c(1, 10), |
| 341 |
filename = "convention_test.R" |
| 342 |
) |
| 343 |
|
| 344 |
attrs <- otel_srcref_attributes(srcref) |
| 345 |
|
| 346 |
# Check that preferred attribute names follow the convention |
| 347 |
expect_true("code.file.path" %in% names(attrs)) |
| 348 |
expect_true("code.line.number" %in% names(attrs)) |
| 349 |
expect_true("code.column.number" %in% names(attrs)) |
| 350 |
expect_false("code.function.name" %in% names(attrs)) |
| 351 |
|
| 352 |
# Check that deprecated names are also present |
| 353 |
expect_true("code.filepath" %in% names(attrs)) |
| 354 |
expect_true("code.lineno" %in% names(attrs)) |
| 355 |
expect_true("code.column" %in% names(attrs)) |
| 356 |
|
| 357 |
# Check that values are of correct types (preferred names) |
| 358 |
expect_true(is.character(attrs[["code.file.path"]])) |
| 359 |
expect_true(is.numeric(attrs[["code.line.number"]])) |
| 360 |
expect_true(is.numeric(attrs[["code.column.number"]])) |
| 361 |
|
| 362 |
# Check that deprecated names have same values |
| 363 |
expect_equal(attrs[["code.file.path"]], attrs[["code.filepath"]]) |
| 364 |
expect_equal(attrs[["code.line.number"]], attrs[["code.lineno"]]) |
| 365 |
expect_equal(attrs[["code.column.number"]], attrs[["code.column"]]) |
| 366 |
|
| 367 |
# Test with function name |
| 368 |
attrs_with_fn <- otel_srcref_attributes(srcref, fn_name = "myFunc") |
| 369 |
|
| 370 |
expect_true("code.function.name" %in% names(attrs_with_fn)) |
| 371 |
expect_true(is.character(attrs_with_fn[["code.function.name"]])) |
| 372 |
expect_equal(attrs_with_fn[["code.function.name"]], "myFunc") |
| 373 |
}) |
| 374 |
|
| 375 |
test_that("dropNulls helper works correctly in otel_srcref_attributes", { |
| 376 |
# Test with all values present |
| 377 |
srcref <- create_mock_srcref( |
| 378 |
lines = c(5, 8), |
| 379 |
columns = c(3, 15), |
| 380 |
filename = "complete_test.R" |
| 381 |
) |
| 382 |
|
| 383 |
attrs <- otel_srcref_attributes(srcref) |
| 384 |
expect_equal(length(attrs), 6) # 3 preferred + 3 deprecated |
| 385 |
|
| 386 |
# Test with missing filename (NULL) |
| 387 |
srcref_no_file <- structure( |
| 388 |
c(5, 3, 8, 15, 3, 15), |
| 389 |
class = "srcref" |
| 390 |
) |
| 391 |
attr(srcref_no_file, "srcfile") <- list(filename = NULL) |
| 392 |
|
| 393 |
attrs_no_file <- otel_srcref_attributes(srcref_no_file) |
| 394 |
expect_equal(length(attrs_no_file), 4) # 2 preferred + 2 deprecated |
| 395 |
expect_false("code.file.path" %in% names(attrs_no_file)) |
| 396 |
expect_false("code.filepath" %in% names(attrs_no_file)) |
| 397 |
}) |
| 398 |
|
| 399 |
test_that("otel attributes are used in reactive context execution", { |
| 400 |
# Test that otel attributes are properly passed through to spans |
| 401 |
mock_attrs <- list( |
| 402 |
"code.file.path" = "context_test.R", |
| 403 |
"code.line.number" = 42L, |
| 404 |
"code.column.number" = 8L |
| 405 |
) |
| 406 |
|
| 407 |
# Test the context info structure used in react.R |
| 408 |
otel_info <- ctx_otel_info_obj( |
| 409 |
isRecordingOtel = TRUE, |
| 410 |
otelLabel = "test_reactive", |
| 411 |
otelAttrs = mock_attrs |
| 412 |
) |
| 413 |
|
| 414 |
expect_true(otel_info$isRecordingOtel) |
| 415 |
expect_equal(otel_info$otelLabel, "test_reactive") |
| 416 |
expect_equal(otel_info$otelAttrs, mock_attrs) |
| 417 |
expect_equal(class(otel_info), "ctx_otel_info") |
| 418 |
}) |
| 419 |
|
| 420 |
test_that("otel attributes are combined with session attributes", { |
| 421 |
# Test that otel srcref attributes are properly combined with session attributes |
| 422 |
# as happens in the reactive system |
| 423 |
|
| 424 |
srcref_attrs <- list( |
| 425 |
"code.file.path" = "session_test.R", |
| 426 |
"code.line.number" = 15L, |
| 427 |
"code.column.number" = 5L |
| 428 |
) |
| 429 |
|
| 430 |
session_attrs <- list( |
| 431 |
"session.id" = "test-session-123" |
| 432 |
) |
| 433 |
|
| 434 |
# Simulate the combination as done in reactives.R |
| 435 |
combined_attrs <- c(srcref_attrs, session_attrs) |
| 436 |
|
| 437 |
expect_equal(length(combined_attrs), 4) |
| 438 |
expect_equal(combined_attrs[["code.file.path"]], "session_test.R") |
| 439 |
expect_equal(combined_attrs[["code.line.number"]], 15L) |
| 440 |
expect_equal(combined_attrs[["session.id"]], "test-session-123") |
| 441 |
}) |
| 442 |
|
| 443 |
test_that("eventReactive() captures otel attributes from source reference", { |
| 444 |
x <- get_reactive_objects()$eventReactive |
| 445 |
attrs <- attr(x, "observable")$.otelAttrs |
| 446 |
|
| 447 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 448 |
expect_equal(attrs[["code.line.number"]], 10) |
| 449 |
expect_equal(attrs[["code.column.number"]], 3) |
| 450 |
expect_equal(attrs[["code.function.name"]], "eventReactive") |
| 451 |
}) |
| 452 |
|
| 453 |
test_that("renderText() with bindCache() captures otel attributes", { |
| 454 |
x <- get_reactive_objects()$renderCacheA |
| 455 |
attrs <- attr(x, "otelAttrs") |
| 456 |
|
| 457 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 458 |
expect_gt(attrs[["code.line.number"]], 12) |
| 459 |
expect_false("code.function.name" %in% names(attrs)) |
| 460 |
}) |
| 461 |
|
| 462 |
test_that("renderText() with bindEvent() captures otel attributes", { |
| 463 |
x <- get_reactive_objects()$renderEventA |
| 464 |
attrs <- attr(x, "otelAttrs") |
| 465 |
|
| 466 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 467 |
expect_gt(attrs[["code.line.number"]], 12) |
| 468 |
expect_false("code.function.name" %in% names(attrs)) |
| 469 |
}) |
| 470 |
|
| 471 |
test_that( |
| 472 |
"renderText() with bindCache() |> bindEvent() captures otel attributes", |
| 473 |
{ |
| 474 |
x <- get_reactive_objects()$renderCacheEventA |
| 475 |
attrs <- attr(x, "otelAttrs") |
| 476 |
|
| 477 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 478 |
expect_gt(attrs[["code.line.number"]], 12) |
| 479 |
expect_false("code.function.name" %in% names(attrs)) |
| 480 |
} |
| 481 |
) |
| 482 |
|
| 483 |
test_that("bindCache() wrapping renderText() captures otel attributes", { |
| 484 |
x <- get_reactive_objects()$renderCacheB |
| 485 |
attrs <- attr(x, "otelAttrs") |
| 486 |
|
| 487 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 488 |
expect_gt(attrs[["code.line.number"]], 12) |
| 489 |
expect_false("code.function.name" %in% names(attrs)) |
| 490 |
}) |
| 491 |
|
| 492 |
test_that("bindEvent() wrapping renderText() captures otel attributes", { |
| 493 |
x <- get_reactive_objects()$renderEventB |
| 494 |
attrs <- attr(x, "otelAttrs") |
| 495 |
|
| 496 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 497 |
expect_gt(attrs[["code.line.number"]], 12) |
| 498 |
expect_false("code.function.name" %in% names(attrs)) |
| 499 |
}) |
| 500 |
|
| 501 |
test_that( |
| 502 |
"bindEvent() wrapping bindCache(renderText()) captures otel attributes", |
| 503 |
{ |
| 504 |
x <- get_reactive_objects()$renderCacheEventB |
| 505 |
attrs <- attr(x, "otelAttrs") |
| 506 |
|
| 507 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 508 |
expect_gt(attrs[["code.line.number"]], 12) |
| 509 |
expect_false("code.function.name" %in% names(attrs)) |
| 510 |
} |
| 511 |
) |
| 512 |
|
| 513 |
test_that("observe() with bindEvent() captures otel attributes", { |
| 514 |
x <- get_reactive_objects()$observeEventA |
| 515 |
attrs <- x$.otelAttrs |
| 516 |
|
| 517 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 518 |
expect_gt(attrs[["code.line.number"]], 12) |
| 519 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 520 |
}) |
| 521 |
|
| 522 |
test_that("bindEvent() wrapping observe() captures otel attributes", { |
| 523 |
x <- get_reactive_objects()$observeEventB |
| 524 |
attrs <- x$.otelAttrs |
| 525 |
|
| 526 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 527 |
expect_gt(attrs[["code.line.number"]], 12) |
| 528 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 529 |
}) |
| 530 |
|
| 531 |
test_that("reactive() with bindCache() captures otel attributes", { |
| 532 |
x <- get_reactive_objects()$reactiveCacheA |
| 533 |
attrs <- attr(x, "observable")$.otelAttrs |
| 534 |
|
| 535 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 536 |
expect_gt(attrs[["code.line.number"]], 12) |
| 537 |
expect_equal(attrs[["code.function.name"]], "bindCache") |
| 538 |
}) |
| 539 |
|
| 540 |
test_that("reactive() with bindEvent() captures otel attributes", { |
| 541 |
x <- get_reactive_objects()$reactiveEventA |
| 542 |
attrs <- attr(x, "observable")$.otelAttrs |
| 543 |
|
| 544 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 545 |
expect_gt(attrs[["code.line.number"]], 12) |
| 546 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 547 |
}) |
| 548 |
|
| 549 |
test_that( |
| 550 |
"reactive() with bindCache() |> bindEvent() captures otel attributes", |
| 551 |
{ |
| 552 |
x <- get_reactive_objects()$reactiveCacheEventA |
| 553 |
attrs <- attr(x, "observable")$.otelAttrs |
| 554 |
|
| 555 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 556 |
expect_gt(attrs[["code.line.number"]], 12) |
| 557 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 558 |
} |
| 559 |
) |
| 560 |
|
| 561 |
test_that("bindCache() wrapping reactive() captures otel attributes", { |
| 562 |
x <- get_reactive_objects()$reactiveCacheB |
| 563 |
attrs <- attr(x, "observable")$.otelAttrs |
| 564 |
|
| 565 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 566 |
expect_gt(attrs[["code.line.number"]], 12) |
| 567 |
expect_equal(attrs[["code.function.name"]], "bindCache") |
| 568 |
}) |
| 569 |
|
| 570 |
test_that("bindEvent() wrapping reactive() captures otel attributes", { |
| 571 |
x <- get_reactive_objects()$reactiveEventB |
| 572 |
attrs <- attr(x, "observable")$.otelAttrs |
| 573 |
|
| 574 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 575 |
expect_gt(attrs[["code.line.number"]], 12) |
| 576 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 577 |
}) |
| 578 |
|
| 579 |
test_that( |
| 580 |
"bindEvent() wrapping bindCache(reactive()) captures otel attributes", |
| 581 |
{ |
| 582 |
x <- get_reactive_objects()$reactiveCacheEventB |
| 583 |
attrs <- attr(x, "observable")$.otelAttrs |
| 584 |
|
| 585 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 586 |
expect_gt(attrs[["code.line.number"]], 12) |
| 587 |
expect_equal(attrs[["code.function.name"]], "bindEvent") |
| 588 |
} |
| 589 |
) |
| 590 |
|
| 591 |
# Tests for debounce/throttle |
| 592 |
test_that("debounce() creates new reactive with otel attributes", { |
| 593 |
x <- get_reactive_objects()$debounce |
| 594 |
attrs <- attr(x, "observable")$.otelAttrs |
| 595 |
|
| 596 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 597 |
expect_gt(attrs[["code.line.number"]], 12) |
| 598 |
expect_equal(attrs[["code.function.name"]], "debounce") |
| 599 |
}) |
| 600 |
|
| 601 |
test_that("throttle() creates new reactive with otel attributes", { |
| 602 |
x <- get_reactive_objects()$throttle |
| 603 |
attrs <- attr(x, "observable")$.otelAttrs |
| 604 |
|
| 605 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 606 |
expect_gt(attrs[["code.line.number"]], 12) |
| 607 |
expect_equal(attrs[["code.function.name"]], "throttle") |
| 608 |
}) |
| 609 |
|
| 610 |
# Tests for ExtendedTask |
| 611 |
test_that("ExtendedTask is created and is an R6 object", { |
| 612 |
x <- get_reactive_objects()$extendedTask |
| 613 |
expect_s3_class(x, "ExtendedTask") |
| 614 |
expect_s3_class(x, "R6") |
| 615 |
|
| 616 |
attrs <- .subset2(x, ".__enclos_env__")$private$otel_attrs |
| 617 |
|
| 618 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 619 |
expect_gt(attrs[["code.line.number"]], 12) |
| 620 |
expect_equal(attrs[["code.function.name"]], "ExtendedTask") |
| 621 |
}) |
| 622 |
|
| 623 |
# Tests for reactivePoll |
| 624 |
test_that("reactivePoll() captures otel attributes from source reference", { |
| 625 |
x <- get_reactive_objects()$reactivePoll |
| 626 |
impl <- attr(x, "observable", exact = TRUE) |
| 627 |
attrs <- impl$.otelAttrs |
| 628 |
otelLabel <- impl$.otelLabel |
| 629 |
|
| 630 |
expect_equal(as.character(otelLabel), "reactivePoll r_poll") |
| 631 |
|
| 632 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 633 |
expect_gt(attrs[["code.line.number"]], 12) |
| 634 |
expect_equal(attrs[["code.function.name"]], "reactivePoll") |
| 635 |
}) |
| 636 |
|
| 637 |
# Tests for reactiveFileReader |
| 638 |
test_that("reactiveFileReader() captures otel attributes from source reference", { |
| 639 |
x <- get_reactive_objects()$reactiveFileReader |
| 640 |
impl <- attr(x, "observable", exact = TRUE) |
| 641 |
attrs <- impl$.otelAttrs |
| 642 |
otelLabel <- impl$.otelLabel |
| 643 |
|
| 644 |
expect_equal(as.character(otelLabel), "reactiveFileReader r_file") |
| 645 |
|
| 646 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 647 |
expect_gt(attrs[["code.line.number"]], 12) |
| 648 |
expect_equal(attrs[["code.function.name"]], "reactiveFileReader") |
| 649 |
}) |
| 650 |
|
| 651 |
# Tests for explicit labels |
| 652 |
test_that("reactive() with explicit label still captures otel attributes", { |
| 653 |
x <- get_reactive_objects()$reactiveLabeled |
| 654 |
attrs <- attr(x, "observable")$.otelAttrs |
| 655 |
|
| 656 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 657 |
expect_equal(attrs[["code.line.number"]], 38) |
| 658 |
expect_equal(attrs[["code.column.number"]], 3) |
| 659 |
expect_equal(attrs[["code.function.name"]], "reactive") |
| 660 |
|
| 661 |
# Verify label is preserved |
| 662 |
label <- attr(x, "observable")$.label |
| 663 |
expect_equal(as.character(label), "my_reactive") |
| 664 |
}) |
| 665 |
|
| 666 |
test_that("observe() with explicit label still captures otel attributes", { |
| 667 |
x <- get_reactive_objects()$observeLabeled |
| 668 |
attrs <- x$.otelAttrs |
| 669 |
|
| 670 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 671 |
expect_equal(attrs[["code.line.number"]], 39) |
| 672 |
expect_equal(attrs[["code.column.number"]], 3) |
| 673 |
expect_equal(attrs[["code.function.name"]], "observe") |
| 674 |
|
| 675 |
# Verify label is preserved |
| 676 |
expect_equal(x$.label, "my_observer") |
| 677 |
}) |
| 678 |
|
| 679 |
# Edge case tests |
| 680 |
test_that("reactive created inside function captures function srcref", { |
| 681 |
create_reactive <- function() { |
| 682 |
reactive({ 100 }) |
| 683 |
} |
| 684 |
|
| 685 |
r <- create_reactive() |
| 686 |
attrs <- attr(r, "observable")$.otelAttrs |
| 687 |
|
| 688 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 689 |
# Line number should point to where reactive() is called inside the function |
| 690 |
expect_true(is.numeric(attrs[["code.line.number"]])) |
| 691 |
expect_true(is.numeric(attrs[["code.column.number"]])) |
| 692 |
}) |
| 693 |
|
| 694 |
test_that("observe created inside function captures function srcref", { |
| 695 |
create_observer <- function() { |
| 696 |
observe({ 101 }) |
| 697 |
} |
| 698 |
|
| 699 |
o <- create_observer() |
| 700 |
attrs <- o$.otelAttrs |
| 701 |
|
| 702 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 703 |
expect_true(is.numeric(attrs[["code.line.number"]])) |
| 704 |
expect_true(is.numeric(attrs[["code.column.number"]])) |
| 705 |
}) |
| 706 |
|
| 707 |
test_that("reactive returned from function preserves srcref", { |
| 708 |
make_counter <- function(initial = 0) { |
| 709 |
reactive({ initial + 1 }) |
| 710 |
} |
| 711 |
|
| 712 |
counter <- make_counter(42) |
| 713 |
attrs <- attr(counter, "observable")$.otelAttrs |
| 714 |
|
| 715 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 716 |
expect_true(is.numeric(attrs[["code.line.number"]])) |
| 717 |
}) |
| 718 |
|
| 719 |
test_that("reactiveVal created in function captures srcref", { |
| 720 |
create_val <- function() { |
| 721 |
reactiveVal("initial") |
| 722 |
} |
| 723 |
|
| 724 |
rv <- create_val() |
| 725 |
attrs <- attr(rv, ".impl")$.otelAttrs |
| 726 |
|
| 727 |
expect_equal(attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 728 |
expect_true(is.numeric(attrs[["code.line.number"]])) |
| 729 |
}) |
| 730 |
|
| 731 |
test_that("nested reactive expressions preserve individual srcrefs", { |
| 732 |
outer_reactive <- reactive({ |
| 733 |
inner_reactive <- reactive({ 200 }) |
| 734 |
inner_reactive |
| 735 |
}) |
| 736 |
|
| 737 |
outer_attrs <- attr(outer_reactive, "observable")$.otelAttrs |
| 738 |
expect_equal(outer_attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 739 |
expect_true(is.numeric(outer_attrs[["code.line.number"]])) |
| 740 |
|
| 741 |
# Get the inner reactive by executing outer |
| 742 |
withReactiveDomain(MockShinySession$new(), { |
| 743 |
inner_reactive <- isolate(outer_reactive()) |
| 744 |
inner_attrs <- attr(inner_reactive, "observable")$.otelAttrs |
| 745 |
|
| 746 |
expect_equal(inner_attrs[["code.file.path"]], "test-otel-attr-srcref.R") |
| 747 |
expect_true(is.numeric(inner_attrs[["code.line.number"]])) |
| 748 |
# Inner should have different line number than outer |
| 749 |
expect_false(inner_attrs[["code.line.number"]] == outer_attrs[["code.line.number"]]) |
| 750 |
}) |
| 751 |
}) |