-
Notifications
You must be signed in to change notification settings - Fork 2
/
lograge_activejob_log_subscriber_spec.rb
84 lines (68 loc) · 2.13 KB
/
lograge_activejob_log_subscriber_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'spec_helper'
require 'logger'
require 'active_job'
require 'lograge'
RSpec.describe LogrageActivejob::LogSubscriber do
class TestJob < ActiveJob::Base; end
let(:log_output) { StringIO.new }
let(:logger) do
Logger.new(log_output).tap { |logger| logger.formatter = ->(_, _, _, msg) { msg } }
end
let(:subscriber) { LogrageActivejob::LogSubscriber.new }
let(:event) do
ActiveSupport::Notifications::Event.new(
'perform.active_job',
Time.now,
Time.now + 1.second,
1,
adapter: ActiveJob::QueueAdapters::AsyncAdapter.new,
job: TestJob.new
)
end
before do
Lograge.logger = logger
end
context 'when perform an action with lograge output' do
before do
Lograge.formatter = Lograge::Formatters::KeyValue.new
end
it 'includes the job_class' do
subscriber.perform(event)
expect(log_output.string).to include('job_class=TestJob ')
end
it 'includes the event_name' do
subscriber.perform(event)
expect(log_output.string).to include('event_name=perform.active_job ')
end
it 'includes the duration' do
subscriber.perform(event)
expect(log_output.string).to match(/duration=[\d\.]/)
end
it 'includes the job_id' do
subscriber.perform(event)
expect(log_output.string).to match(/job_id=[a-z0-9\-]{1,} /)
end
it 'includes the adapter_class' do
subscriber.perform(event)
expect(log_output.string).to include('adapter_class=AsyncAdapter ')
end
it 'includes the queue_name' do
subscriber.perform(event)
expect(log_output.string).to include('queue_name=default ')
end
it 'includes the args' do
subscriber.perform(event)
expect(log_output.string).to match(/args=\[.*\] /)
end
end
context 'with custom_options configured for lograge output' do
before do
Lograge.formatter = Lograge::Formatters::KeyValue.new
end
it 'includes the event_time' do
LogrageActivejob.custom_options = ->(_event) { { data: 'value' } }
subscriber.perform(event)
expect(log_output.string).to include('data=value')
end
end
end