|
69 | 69 | end
|
70 | 70 |
|
71 | 71 | describe '#handle_message' do
|
| 72 | + subject { worker.handle_message(consumer, delivery_info, properties, payload) } |
72 | 73 | let(:payload) { '{}' }
|
73 | 74 | let(:consumer_instance) { double('Consumer instance') }
|
74 | 75 | let(:delivery_info) { double('Delivery Info', routing_key: '',
|
75 | 76 | delivery_tag: 'dt') }
|
76 | 77 | let(:properties) { double('Properties', message_id: nil, content_type: "application/json") }
|
| 78 | + let(:log) { StringIO.new } |
77 | 79 | before { allow(consumer).to receive_messages(new: consumer_instance) }
|
78 | 80 | before { allow(broker).to receive(:ack) }
|
79 | 81 | before { allow(broker).to receive(:nack) }
|
80 | 82 | before { allow(consumer_instance).to receive(:broker=) }
|
81 | 83 | before { allow(consumer_instance).to receive(:delivery_info=) }
|
| 84 | + before { allow(Hutch::Logging).to receive(:logger).and_return(Logger.new(log)) } |
82 | 85 |
|
83 | 86 | it 'passes the message to the consumer' do
|
84 | 87 | expect(consumer_instance).to receive(:process).
|
85 | 88 | with(an_instance_of(Hutch::Message))
|
86 | 89 | expect(consumer_instance).to receive(:message_rejected?).and_return(false)
|
87 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 90 | + subject |
88 | 91 | end
|
89 | 92 |
|
90 | 93 | it 'acknowledges the message' do
|
91 | 94 | allow(consumer_instance).to receive(:process)
|
92 | 95 | expect(broker).to receive(:ack).with(delivery_info.delivery_tag)
|
93 | 96 | expect(consumer_instance).to receive(:message_rejected?).and_return(false)
|
94 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 97 | + subject |
95 | 98 | end
|
96 | 99 |
|
97 | 100 | context 'when the consumer fails and a requeue is configured' do
|
|
108 | 111 | expect(broker).to_not receive(:nack)
|
109 | 112 | expect(broker).to receive(:requeue)
|
110 | 113 |
|
111 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 114 | + subject |
112 | 115 | end
|
113 | 116 | end
|
114 | 117 |
|
115 | 118 |
|
116 | 119 | context 'when the consumer raises an exception' do
|
| 120 | + let(:expected_log) { /ERROR .+ error in consumer .+ RuntimeError .+ backtrace:/m } |
117 | 121 | before { allow(consumer_instance).to receive(:process).and_raise('a consumer error') }
|
118 | 122 |
|
119 | 123 | it 'logs the error' do
|
120 |
| - Hutch::Config[:error_handlers].each do |backend| |
121 |
| - expect(backend).to receive(:handle) |
122 |
| - end |
123 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 124 | + expect { subject }.to change { log.tap(&:rewind).read }.from("").to(expected_log) |
124 | 125 | end
|
125 | 126 |
|
126 | 127 | it 'rejects the message' do
|
127 | 128 | expect(broker).to receive(:nack).with(delivery_info.delivery_tag)
|
128 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 129 | + subject |
| 130 | + end |
| 131 | + |
| 132 | + context 'when a custom error handler supports delivery info' do |
| 133 | + let(:error_handler) do |
| 134 | + Class.new(Hutch::ErrorHandlers::Base) do |
| 135 | + def handle(_properties, _payload, _consumer, _ex, delivery_info) |
| 136 | + raise unless delivery_info.delivery_tag == 'dt' |
| 137 | + puts 'handled!' |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + around do |example| |
| 143 | + original = Hutch::Config[:error_handlers] |
| 144 | + Hutch::Config[:error_handlers] = [error_handler.new] |
| 145 | + example.run |
| 146 | + Hutch::Config[:error_handlers] = original |
| 147 | + end |
| 148 | + |
| 149 | + it 'calls the custom handler with delivery info' do |
| 150 | + expect { subject }.to output("handled!\n").to_stdout |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context 'when a custom error handler does not support delivery info' do |
| 155 | + let(:error_handler) do |
| 156 | + Class.new(Hutch::ErrorHandlers::Base) do |
| 157 | + def handle(_properties, _payload, _consumer, _ex) |
| 158 | + puts 'handled!' |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + around do |example| |
| 164 | + original = Hutch::Config[:error_handlers] |
| 165 | + Hutch::Config[:error_handlers] = [error_handler.new] |
| 166 | + example.run |
| 167 | + Hutch::Config[:error_handlers] = original |
| 168 | + end |
| 169 | + |
| 170 | + it 'calls the custom handler with delivery info' do |
| 171 | + expect { subject }.to output("handled!\n").to_stdout |
| 172 | + end |
129 | 173 | end
|
130 | 174 | end
|
131 | 175 |
|
132 | 176 | context "when the payload is not valid json" do
|
133 | 177 | let(:payload) { "Not Valid JSON" }
|
| 178 | + let(:expected_log) { /ERROR .+ error in consumer .+ MultiJson::ParseError .+ backtrace:/m } |
134 | 179 |
|
135 | 180 | it 'logs the error' do
|
136 |
| - Hutch::Config[:error_handlers].each do |backend| |
137 |
| - expect(backend).to receive(:handle) |
138 |
| - end |
139 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 181 | + expect { subject }.to change { log.tap(&:rewind).read }.from("").to(expected_log) |
140 | 182 | end
|
141 | 183 |
|
142 | 184 | it 'rejects the message' do
|
143 | 185 | expect(broker).to receive(:nack).with(delivery_info.delivery_tag)
|
144 |
| - worker.handle_message(consumer, delivery_info, properties, payload) |
| 186 | + subject |
145 | 187 | end
|
146 | 188 | end
|
147 | 189 | end
|
|
0 commit comments