|
289 | 289 | end |
290 | 290 | end |
291 | 291 |
|
| 292 | + describe 'fibers' do |
| 293 | + it 'is isolated with respect to Fiber-local variable manipulation' do |
| 294 | + OpenTelemetry::TestHelpers.with_test_logger do |log_stream| |
| 295 | + ctx = new_context |
| 296 | + Context.with_current(ctx) do |
| 297 | + # This is based on code in ActionController::Live#process: |
| 298 | + # https://github.com/rails/rails/blob/ad0105c13f61d145a659004efb928a643104e973/actionpack/lib/action_controller/metal/live.rb#L270 |
| 299 | + t1 = Thread.current |
| 300 | + locals = t1.keys.map { |key| [key, t1[key]] } |
| 301 | + Fiber.new do |
| 302 | + t2 = Thread.current |
| 303 | + locals.each { |k, v| t2[k] = v } |
| 304 | + # Manipulate _this fiber's_ context stack. |
| 305 | + Context.attach(new_context) |
| 306 | + ensure |
| 307 | + locals.each { |k, _| t2[k] = nil } |
| 308 | + end.resume |
| 309 | + end |
| 310 | + _(log_stream.string).must_be_empty |
| 311 | + end |
| 312 | + end |
| 313 | + |
| 314 | + it 'is isolated with respect to Fiber-local storage manipulation' do |
| 315 | + OpenTelemetry::TestHelpers.with_test_logger do |log_stream| |
| 316 | + ctx = new_context |
| 317 | + Context.with_current(ctx) do |
| 318 | + # This is based on code in ActionController::Live#process, modified to use Fiber-local storage: |
| 319 | + # https://github.com/rails/rails/blob/ad0105c13f61d145a659004efb928a643104e973/actionpack/lib/action_controller/metal/live.rb#L270 |
| 320 | + f1 = Fiber.current |
| 321 | + Fiber[:foo] = :bar |
| 322 | + locals = f1.storage |
| 323 | + Fiber.new do |
| 324 | + f2 = Fiber.current |
| 325 | + locals.each { |k, v| f2.storage[k] = v } |
| 326 | + # Manipulate _this fiber's_ context stack. |
| 327 | + Context.attach(new_context) |
| 328 | + ensure |
| 329 | + locals.each { |k, _| f2.storage[k] = nil } |
| 330 | + end.resume |
| 331 | + end |
| 332 | + _(log_stream.string).must_be_empty |
| 333 | + end |
| 334 | + end |
| 335 | + end |
| 336 | + |
292 | 337 | describe 'threading' do |
| 338 | + it 'is isolated with respect to Fiber-local variable manipulation' do |
| 339 | + OpenTelemetry::TestHelpers.with_test_logger do |log_stream| |
| 340 | + ctx = new_context |
| 341 | + Context.with_current(ctx) do |
| 342 | + # This is based on code in ActionController::Live#process: |
| 343 | + # https://github.com/rails/rails/blob/ad0105c13f61d145a659004efb928a643104e973/actionpack/lib/action_controller/metal/live.rb#L270 |
| 344 | + t1 = Thread.current |
| 345 | + locals = t1.keys.map { |key| [key, t1[key]] } |
| 346 | + Thread.new do |
| 347 | + t2 = Thread.current |
| 348 | + locals.each { |k, v| t2[k] = v } |
| 349 | + # Manipulate _this thread's_ context stack. |
| 350 | + Context.attach(new_context) |
| 351 | + ensure |
| 352 | + locals.each { |k, _| t2[k] = nil } |
| 353 | + end.join |
| 354 | + end |
| 355 | + _(log_stream.string).must_be_empty |
| 356 | + end |
| 357 | + end |
| 358 | + |
293 | 359 | it 'unwinds the stack on each thread' do |
294 | 360 | ctx = new_context |
295 | 361 | t1_ctx_before = Context.current |
|
0 commit comments