| 1 |
# Tests for ExtendedTask otel behavior |
| 2 |
|
| 3 |
ex_task_42 <- function() { |
| 4 |
ExtendedTask$new(function() { |
| 5 |
promises::promise_resolve(42) |
| 6 |
}) |
| 7 |
} |
| 8 |
|
| 9 |
test_that("ExtendedTask captures otel collection state at initialization", { |
| 10 |
# Test that has_otel_collect is called at init, not at invoke time |
| 11 |
withr::local_options(list(shiny.otel.collect = "reactivity")) |
| 12 |
|
| 13 |
# Enable otel tracing |
| 14 |
local_mocked_bindings( |
| 15 |
otel_is_tracing_enabled = function() TRUE |
| 16 |
) |
| 17 |
|
| 18 |
task <- ex_task_42() |
| 19 |
|
| 20 |
# Check that is_recording_otel is captured at init time |
| 21 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 22 |
}) |
| 23 |
|
| 24 |
test_that("ExtendedTask sets is_recording_otel to FALSE when otel disabled", { |
| 25 |
# Enable otel tracing |
| 26 |
local_mocked_bindings( |
| 27 |
otel_is_tracing_enabled = function() FALSE |
| 28 |
) |
| 29 |
|
| 30 |
# Test with all level |
| 31 |
withr::with_options(list(shiny.otel.collect = "all"), { |
| 32 |
task1 <- ex_task_42() |
| 33 |
expect_false(task1$.__enclos_env__$private$is_recording_otel) |
| 34 |
}) |
| 35 |
|
| 36 |
# Test with reactivity level |
| 37 |
withr::with_options(list(shiny.otel.collect = "reactivity"), { |
| 38 |
task1 <- ex_task_42() |
| 39 |
expect_false(task1$.__enclos_env__$private$is_recording_otel) |
| 40 |
}) |
| 41 |
|
| 42 |
# Test with session level (should be FALSE) |
| 43 |
withr::with_options(list(shiny.otel.collect = "session"), { |
| 44 |
task2 <- ex_task_42() |
| 45 |
expect_false(task2$.__enclos_env__$private$is_recording_otel) |
| 46 |
}) |
| 47 |
|
| 48 |
# Test with none level (should be FALSE) |
| 49 |
withr::with_options(list(shiny.otel.collect = "none"), { |
| 50 |
task3 <- ex_task_42() |
| 51 |
expect_false(task3$.__enclos_env__$private$is_recording_otel) |
| 52 |
}) |
| 53 |
}) |
| 54 |
|
| 55 |
test_that("ExtendedTask sets is_recording_otel based on has_otel_collect at init", { |
| 56 |
# Enable otel tracing |
| 57 |
local_mocked_bindings( |
| 58 |
otel_is_tracing_enabled = function() TRUE |
| 59 |
) |
| 60 |
|
| 61 |
# Test with all level |
| 62 |
withr::with_options(list(shiny.otel.collect = "all"), { |
| 63 |
task1 <- ex_task_42() |
| 64 |
expect_true(task1$.__enclos_env__$private$is_recording_otel) |
| 65 |
}) |
| 66 |
|
| 67 |
# Test with reactivity level |
| 68 |
withr::with_options(list(shiny.otel.collect = "reactivity"), { |
| 69 |
task1 <- ex_task_42() |
| 70 |
expect_true(task1$.__enclos_env__$private$is_recording_otel) |
| 71 |
}) |
| 72 |
|
| 73 |
# Test with session level (should be FALSE) |
| 74 |
withr::with_options(list(shiny.otel.collect = "session"), { |
| 75 |
task2 <- ex_task_42() |
| 76 |
expect_false(task2$.__enclos_env__$private$is_recording_otel) |
| 77 |
}) |
| 78 |
|
| 79 |
# Test with none level (should be FALSE) |
| 80 |
withr::with_options(list(shiny.otel.collect = "none"), { |
| 81 |
task3 <- ex_task_42() |
| 82 |
expect_false(task3$.__enclos_env__$private$is_recording_otel) |
| 83 |
}) |
| 84 |
}) |
| 85 |
|
| 86 |
test_that("ExtendedTask uses init-time otel setting even if option changes later", { |
| 87 |
|
| 88 |
# Enable otel tracing |
| 89 |
local_mocked_bindings( |
| 90 |
otel_is_tracing_enabled = function() TRUE |
| 91 |
) |
| 92 |
|
| 93 |
# Test that changing the option after init doesn't affect the task |
| 94 |
withr::with_options(list(shiny.otel.collect = "reactivity"), { |
| 95 |
task <- ex_task_42() |
| 96 |
}) |
| 97 |
|
| 98 |
# Capture the initial state |
| 99 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 100 |
|
| 101 |
# Change the option after initialization |
| 102 |
withr::with_options(list(shiny.otel.collect = "none"), { |
| 103 |
# The task should still have the init-time setting |
| 104 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 105 |
}) |
| 106 |
|
| 107 |
}) |
| 108 |
|
| 109 |
test_that("ExtendedTask respects session level otel collection", { |
| 110 |
# Test that session level doesn't enable reactivity spans |
| 111 |
withr::local_options(list(shiny.otel.collect = "session")) |
| 112 |
|
| 113 |
task <- ex_task_42() |
| 114 |
|
| 115 |
# Should not record otel at session level |
| 116 |
expect_false(task$.__enclos_env__$private$is_recording_otel) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("ExtendedTask respects reactive_update level otel collection", { |
| 120 |
# Test that reactive_update level doesn't enable reactivity spans |
| 121 |
withr::local_options(list(shiny.otel.collect = "reactive_update")) |
| 122 |
|
| 123 |
task <- ex_task_42() |
| 124 |
|
| 125 |
# Should not record otel at reactive_update level |
| 126 |
expect_false(task$.__enclos_env__$private$is_recording_otel) |
| 127 |
}) |
| 128 |
|
| 129 |
test_that("ExtendedTask creates span only when is_recording_otel is TRUE", { |
| 130 |
skip_if_not_installed("otelsdk") |
| 131 |
# Test that span is only created when otel is enabled |
| 132 |
withr::local_options(list(shiny.otel.collect = "reactivity")) |
| 133 |
|
| 134 |
span_created <- FALSE |
| 135 |
|
| 136 |
local_mocked_bindings( |
| 137 |
start_otel_span = function(...) { |
| 138 |
span_created <<- TRUE |
| 139 |
create_mock_otel_span("extended_task") |
| 140 |
}, |
| 141 |
otel_is_tracing_enabled = function() TRUE |
| 142 |
) |
| 143 |
|
| 144 |
with_shiny_otel_record({ |
| 145 |
withReactiveDomain(MockShinySession$new(), { |
| 146 |
task <- ex_task_42() |
| 147 |
|
| 148 |
# Reset the flag |
| 149 |
span_created <- FALSE |
| 150 |
|
| 151 |
# Invoke the task |
| 152 |
isolate({ |
| 153 |
task$invoke() |
| 154 |
}) |
| 155 |
}) |
| 156 |
}) |
| 157 |
|
| 158 |
|
| 159 |
# Span should have been created because is_recording_otel is TRUE |
| 160 |
expect_true(span_created) |
| 161 |
}) |
| 162 |
|
| 163 |
test_that("ExtendedTask does not create span when is_recording_otel is FALSE", { |
| 164 |
# Test that span is not created when otel is disabled |
| 165 |
withr::local_options(list(shiny.otel.collect = "none")) |
| 166 |
|
| 167 |
span_created <- FALSE |
| 168 |
|
| 169 |
local_mocked_bindings( |
| 170 |
start_otel_span = function(...) { |
| 171 |
span_created <<- TRUE |
| 172 |
create_mock_otel_span("extended_task") |
| 173 |
} |
| 174 |
) |
| 175 |
|
| 176 |
withReactiveDomain(MockShinySession$new(), { |
| 177 |
task <- ex_task_42() |
| 178 |
|
| 179 |
# Invoke the task |
| 180 |
isolate({ |
| 181 |
task$invoke() |
| 182 |
}) |
| 183 |
}) |
| 184 |
|
| 185 |
# Span should not have been created because is_recording_otel is FALSE |
| 186 |
expect_false(span_created) |
| 187 |
}) |
| 188 |
|
| 189 |
|
| 190 |
test_that("Multiple ExtendedTask invocations use same is_recording_otel value", { |
| 191 |
# Enable otel tracing |
| 192 |
withr::local_options(list(shiny.otel.collect = "reactivity")) |
| 193 |
local_mocked_bindings( |
| 194 |
otel_is_tracing_enabled = function() TRUE |
| 195 |
) |
| 196 |
|
| 197 |
withReactiveDomain(MockShinySession$new(), { |
| 198 |
task <- ex_task_42() |
| 199 |
|
| 200 |
# Verify is_recording_otel is TRUE at init |
| 201 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 202 |
|
| 203 |
# Change option after initialization (should not affect the task) |
| 204 |
withr::with_options( |
| 205 |
list(shiny.otel.collect = "none"), |
| 206 |
{ |
| 207 |
# The task should still have the init-time setting |
| 208 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 209 |
|
| 210 |
# Verify is_recording_otel doesn't change on invocation |
| 211 |
isolate({ |
| 212 |
task$invoke() |
| 213 |
}) |
| 214 |
|
| 215 |
# Still should be TRUE after invoke |
| 216 |
expect_true(task$.__enclos_env__$private$is_recording_otel) |
| 217 |
} |
| 218 |
) |
| 219 |
}) |
| 220 |
}) |