Skip to content

Commit

Permalink
TECH-12593: add spec to show that global_context and current_context …
Browse files Browse the repository at this point in the history
…are distinct per logger
  • Loading branch information
ColinDKelley committed Aug 31, 2023
1 parent fff5444 commit 6c23619
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
22 changes: 9 additions & 13 deletions spec/lib/contextual_logger/context/handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class ContextualLoggerContextContainerForSpec

describe 'mixin methods' do
describe 'current_context/current_context=' do
it 'can be set with current_context=, per Fiber/Thread' do
previous_context = instance.current_context({})
it 'can be set with current_context=, separately per Fiber/Thread' do
instance.current_context = context

Thread.new do
Expand All @@ -31,21 +30,21 @@ class ContextualLoggerContextContainerForSpec
end.resume

expect(instance.current_context({})).to eq(context)
end

it 'the current_context= values are separately per containing instance' do
instance.current_context = context
instance2 = ContextualLoggerContextContainerForSpec.new
instance2.current_context = context2

ensure
instance.current_context = previous_context
expect(instance.current_context({})).to eq(context)
expect(instance2.current_context({})).to eq(context2)
end

it 'defaults to global_context when set to nil' do
previous_context = instance.current_context(nil)

instance.current_context = nil

expect(instance.current_context(context)).to_not eq(previous_context)
expect(instance.current_context(context)).to eq(context)

ensure
instance.current_context = previous_context
end
end
end
Expand All @@ -56,13 +55,10 @@ class ContextualLoggerContextContainerForSpec
it { is_expected.to respond_to(:reset!) }

it 'resets the thread context on reset!' do
initial_context = instance.current_context(nil)
instance.current_context = context2
handler.reset!

expect(instance.current_context(nil)).to eq(context)
ensure
instance.current_context = initial_context
end
end
end
6 changes: 0 additions & 6 deletions spec/lib/contextual_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def expect_log_line_to_be_written(log_line)

describe ContextualLogger do
before { Time.now_override = Time.now }
after do
logger.global_context = {}
logger.current_context = nil
end

let(:raw_logger) { Logger.new('spec/reports/test.log') }
subject(:logger) do
Expand Down Expand Up @@ -421,8 +417,6 @@ def expect_log_line_to_be_written(log_line)
logger.with_context(hash_context: { apple: 'orange', hello: 'world' }) do
expect(logger.info('this is a test', array_context: [3], hash_context: { pizza: 'bagel', hello: 'goodbye' })).to eq(true)
end
ensure
logger.global_context = {}
end
end

Expand Down

0 comments on commit 6c23619

Please sign in to comment.