|
3 | 3 | require "rails_helper"
|
4 | 4 |
|
5 | 5 | describe Sbmt::KafkaConsumer::Instrumentation::LoggerListener do
|
6 |
| - let(:event) { double("event") } |
7 |
| - |
8 |
| - let(:message) { double("message") } |
9 |
| - let(:metadata) { OpenStruct.new(topic: "topic", partition: 0) } |
| 6 | + let(:metadata) { OpenStruct.new(topic: "topic", partition: 0, key: "key", offset: 42) } |
| 7 | + let(:message) { double("message", metadata: metadata) } |
| 8 | + let(:caller) { double(topic: double(consumer_group: double(id: "group_id"))) } |
| 9 | + let(:event) { double("event", payload: {message: message, time: time, caller: caller}) } |
10 | 10 |
|
11 | 11 | let(:inbox_name) { "inbox" }
|
12 | 12 | let(:event_name) { "event" }
|
13 | 13 | let(:status) { "status" }
|
14 | 14 | let(:message_uuid) { "uuid" }
|
15 | 15 | let(:time) { 10.20 }
|
| 16 | + let(:logger) { double("Logger") } |
| 17 | + let(:error) { StandardError.new("some error") } |
| 18 | + let(:error_message) { "some error message" } |
| 19 | + |
| 20 | + before do |
| 21 | + allow(Sbmt::KafkaConsumer).to receive(:logger).and_return(logger) |
| 22 | + |
| 23 | + allow_any_instance_of(described_class).to receive(:log_backtrace).and_return("some backtrace") |
| 24 | + allow_any_instance_of(described_class).to receive(:error_message).and_return(error_message) |
| 25 | + end |
16 | 26 |
|
17 | 27 | describe ".on_error_occurred" do
|
18 | 28 | it "logs error when consumer.base.consume_one event occurred" do
|
19 |
| - expect(event).to receive(:[]).with(:error).and_return("some error") |
20 |
| - expect(event).to receive(:[]).with(:type).and_return("consumer.base.consume_one") |
21 |
| - expect(Rails.logger).to receive(:error) |
| 29 | + allow(event).to receive(:[]).with(:error).and_return(error) |
| 30 | + allow(event).to receive(:[]).with(:type).and_return("consumer.base.consume_one") |
| 31 | + |
| 32 | + expect(logger).to receive(:tagged).with(hash_including( |
| 33 | + type: "consumer.base.consume_one", |
| 34 | + stacktrace: "some backtrace" |
| 35 | + )).and_yield |
| 36 | + |
| 37 | + expect(logger).to receive(:error).with(error_message) |
22 | 38 |
|
23 | 39 | described_class.new.on_error_occurred(event)
|
24 | 40 | end
|
25 | 41 |
|
26 | 42 | it "logs error when consumer.inbox.consume_one event occurred" do
|
27 |
| - expect(event).to receive(:[]).with(:error).and_return("some error") |
28 |
| - expect(event).to receive(:[]).with(:type).and_return("consumer.inbox.consume_one") |
29 |
| - expect(event).to receive(:[]).with(:status).and_return(status) |
30 |
| - expect(Rails.logger).to receive(:error) |
| 43 | + allow(event).to receive(:[]).with(:error).and_return(error) |
| 44 | + allow(event).to receive(:[]).with(:type).and_return("consumer.inbox.consume_one") |
| 45 | + allow(event).to receive(:[]).with(:status).and_return(status) |
| 46 | + |
| 47 | + expect(logger).to receive(:tagged).with(hash_including( |
| 48 | + type: "consumer.inbox.consume_one", |
| 49 | + status: "status", |
| 50 | + stacktrace: "some backtrace" |
| 51 | + )).and_yield |
| 52 | + |
| 53 | + expect(logger).to receive(:error).with(error_message) |
31 | 54 |
|
32 | 55 | described_class.new.on_error_occurred(event)
|
33 | 56 | end
|
34 | 57 | end
|
35 | 58 |
|
36 | 59 | describe ".on_consumer_consumed_one" do
|
37 | 60 | it "logs info message" do
|
38 |
| - expect(event).to receive(:payload).and_return(time: time) |
39 |
| - expect(Rails.logger).to receive(:info) |
| 61 | + expect(logger).to receive(:tagged).with(hash_including( |
| 62 | + kafka: hash_including( |
| 63 | + topic: "topic", |
| 64 | + partition: 0, |
| 65 | + key: "key", |
| 66 | + offset: 42, |
| 67 | + consumer_group: "group_id", |
| 68 | + consume_duration_ms: time |
| 69 | + ) |
| 70 | + )).and_yield |
| 71 | + |
| 72 | + expect(logger).to receive(:info).with("Successfully consumed message") |
40 | 73 |
|
41 | 74 | described_class.new.on_consumer_consumed_one(event)
|
42 | 75 | end
|
43 | 76 | end
|
44 | 77 |
|
45 | 78 | describe ".on_consumer_inbox_consumed_one" do
|
46 | 79 | it "logs info message" do
|
47 |
| - expect(event).to receive(:payload).and_return(time: time) |
48 | 80 | expect(event).to receive(:[]).with(:status).and_return(status)
|
49 | 81 | expect(event).to receive(:[]).with(:message_uuid).and_return(message_uuid)
|
50 |
| - expect(Rails.logger).to receive(:info) |
| 82 | + |
| 83 | + expect(logger).to receive(:tagged).with(hash_including( |
| 84 | + kafka: hash_including( |
| 85 | + topic: "topic", |
| 86 | + partition: 0, |
| 87 | + key: "key", |
| 88 | + offset: 42, |
| 89 | + consumer_group: "group_id", |
| 90 | + consume_duration_ms: time |
| 91 | + ) |
| 92 | + )).and_yield |
| 93 | + |
| 94 | + expect(logger).to receive(:info).with("Successfully consumed message with uuid: uuid") |
51 | 95 |
|
52 | 96 | described_class.new.on_consumer_inbox_consumed_one(event)
|
53 | 97 | end
|
|
0 commit comments