Module: Deimos::TestHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/deimos/test_helpers.rb

Overview

Include this module in your RSpec spec_helper to stub out external dependencies and add methods to use to test encoding/decoding.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.full_integration_test!Object

Kafka test config with avro schema registry



34
35
36
37
38
39
# File 'lib/deimos/test_helpers.rb', line 34

def full_integration_test!
  Deimos.configure do |deimos_config|
    deimos_config.producers.backend = :kafka
    deimos_config.schema.backend = :avro_schema_registry
  end
end

.kafka_test!Object

Set the config to the right settings for a kafka test



42
43
44
45
46
47
# File 'lib/deimos/test_helpers.rb', line 42

def kafka_test!
  Deimos.configure do |deimos_config|
    deimos_config.producers.backend = :kafka
    deimos_config.schema.backend = :avro_validation
  end
end

.sent_messagesArray<Hash>

for backwards compatibility

Returns:

  • (Array<Hash>)


18
19
20
# File 'lib/deimos/test_helpers.rb', line 18

def sent_messages
  Deimos::Backends::Test.sent_messages
end

.unit_test!Object

Set the config to the right settings for a unit test



23
24
25
26
27
28
29
30
31
# File 'lib/deimos/test_helpers.rb', line 23

def unit_test!
  Deimos.configure do |deimos_config|
    deimos_config.logger = Logger.new(STDOUT)
    deimos_config.consumers.reraise_errors = true
    deimos_config.kafka.seed_brokers ||= ['test_broker']
    deimos_config.schema.backend = Deimos.schema_backend_class.mock_backend
    deimos_config.producers.backend = :test
  end
end

Instance Method Details

#_frk_failure_message(topic, message, key = nil, partition_key = nil, was_negated = false) ⇒ Object

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/deimos/test_helpers.rb', line 100

def _frk_failure_message(topic, message, key=nil, partition_key=nil, was_negated=false)
  messages = Deimos::Backends::Test.sent_messages.
    select { |m| m[:topic] == topic }.
    map { |m| m.except(:topic) }
  message_string = ''
  diff = nil
  min_hash_diff = nil
  if messages.any?
    message_string = messages.map(&:inspect).join("\n")
    min_hash_diff = messages.min_by { |m| _hash_diff(m, message).keys.size }
    diff = RSpec::Expectations.differ.
      diff_as_object(message, min_hash_diff[:payload])
  end
  description = if message.respond_to?(:description)
                  message.description
                elsif message.nil?
                  'nil'
                else
                  message
                end
  str = "Expected #{topic} #{'not ' if was_negated}to have sent #{description}"
  str += " with key #{key}" if key
  str += " with partition key #{partition_key}" if partition_key
  str += "\nClosest message received: #{min_hash_diff}" if min_hash_diff
  str += "\nDiff: #{diff}" if diff
  str + "\nAll Messages received:\n#{message_string}"
end

#_hash_diff(hash1, hash2) ⇒ Object

get the difference of 2 hashes.

Parameters:

  • hash1 (Hash)
  • hash2 (Hash)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/deimos/test_helpers.rb', line 87

def _hash_diff(hash1, hash2)
  if hash1.nil? || !hash1.is_a?(Hash)
    hash2
  elsif hash2.nil? || !hash2.is_a?(Hash)
    hash1
  else
    hash1.dup.
      delete_if { |k, v| hash2[k] == v }.
      merge!(hash2.dup.delete_if { |k, _v| hash1.key?(k) })
  end
end

#clear_kafka_messages!Object

Clear all sent messages - e.g. if we want to check that particular messages were sent or not sent after a point in time.



165
166
167
# File 'lib/deimos/test_helpers.rb', line 165

def clear_kafka_messages!
  Deimos::Backends::Test.sent_messages.clear
end

#stub_batch_consumer(_klass) ⇒ Object

Deprecated.


80
81
82
# File 'lib/deimos/test_helpers.rb', line 80

def stub_batch_consumer(_klass)
  warn('Stubbing batch consumers is no longer necessary and this method will be removed in 3.0')
end

#stub_consumer(_klass) ⇒ Object

Deprecated.


75
76
77
# File 'lib/deimos/test_helpers.rb', line 75

def stub_consumer(_klass)
  warn('Stubbing consumers is no longer necessary and this method will be removed in 3.0')
end

#stub_producer(_klass) ⇒ Object

Deprecated.


70
71
72
# File 'lib/deimos/test_helpers.rb', line 70

def stub_producer(_klass)
  warn('Stubbing producers is no longer necessary and this method will be removed in 3.0')
end

#stub_producers_and_consumers!Object

Deprecated.


65
66
67
# File 'lib/deimos/test_helpers.rb', line 65

def stub_producers_and_consumers!
  warn('stub_producers_and_consumers! is no longer necessary and this method will be removed in 3.0')
end

#test_consume_batch(handler_class_or_topic, payloads, keys: [], partition_keys: [], call_original: false, skip_expectation: false, &block) ⇒ Object

Test that a given handler will consume a given batch payload correctly, i.e. that the schema is correct. If a block is given, that block will be executed when `consume` is called. Otherwise it will just confirm that `consume` is called at all. Deimos::Consumer or the topic as a string

Parameters:

  • handler_class_or_topic (Class|String)

    Class which inherits from

  • payloads (Array<Hash>)

    the payload to consume



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/deimos/test_helpers.rb', line 242

def test_consume_batch(handler_class_or_topic,
                       payloads,
                       keys: [],
                       partition_keys: [],
                       call_original: false,
                       skip_expectation: false,
                       &block)
  if call_original && block_given?
    raise 'Cannot have both call_original and be given a block!'
  end

  topic_name = 'my-topic'
  handler_class = if handler_class_or_topic.is_a?(String)
                    _get_handler_class_from_topic(handler_class_or_topic)
                  else
                    handler_class_or_topic
                  end
  handler = handler_class.new
  allow(handler_class).to receive(:new).and_return(handler)
  listener = double('listener',
                    handler_class: handler_class,
                    encoding: nil)
  batch_messages = payloads.zip(keys, partition_keys).map do |payload, key, partition_key|
    key ||= _key_from_consumer(handler_class)

    double('message',
           'key' => key,
           'partition_key' => partition_key,
           'partition' => 1,
           'offset' => 1,
           'headers' => {},
           'value' => payload)
  end
  batch = double('fetched_batch',
                 'messages' => batch_messages,
                 'topic' => topic_name,
                 'partition' => 1,
                 'offset_lag' => 0)
  unless skip_expectation
    _handler_expectation(:consume_batch,
                         payloads,
                         handler,
                         call_original,
                         &block)
  end
  action = Phobos::Actions::ProcessBatchInline.new(
    listener: listener,
    batch: batch,
    metadata: { topic: topic_name }
  )
  allow(action).to receive(:backoff_interval).and_return(0)
  allow(action).to receive(:handle_error) { |e| raise e }
  action.send(:execute)
end

#test_consume_batch_invalid_message(handler_class, payloads) ⇒ Object

Check to see that a given message will fail due to validation errors.

Parameters:

  • handler_class (Class)
  • payloads (Array<Hash>)


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/deimos/test_helpers.rb', line 300

def test_consume_batch_invalid_message(handler_class, payloads)
  topic_name = 'my-topic'
  handler = handler_class.new
  allow(handler_class).to receive(:new).and_return(handler)
  listener = double('listener',
                    handler_class: handler_class,
                    encoding: nil)
  batch_messages = payloads.map do |payload|
    key ||= _key_from_consumer(handler_class)

    double('message',
           'key' => key,
           'partition' => 1,
           'offset' => 1,
           'value' => payload)
  end
  batch = double('fetched_batch',
                 'messages' => batch_messages,
                 'topic' => topic_name,
                 'partition' => 1,
                 'offset_lag' => 0)

  action = Phobos::Actions::ProcessBatchInline.new(
    listener: listener,
    batch: batch,
    metadata: { topic: topic_name }
  )
  allow(action).to receive(:backoff_interval).and_return(0)
  allow(action).to receive(:handle_error) { |e| raise e }

  expect { action.send(:execute) }.
    to raise_error
end

#test_consume_invalid_message(handler_class, payload) ⇒ Object

Check to see that a given message will fail due to validation errors.

Parameters:

  • handler_class (Class)
  • payload (Hash)


228
229
230
231
232
233
# File 'lib/deimos/test_helpers.rb', line 228

def test_consume_invalid_message(handler_class, payload)
  expect {
    handler_class.decoder.validate(payload,
                                   schema: handler_class.decoder.schema)
  }.to raise_error(Avro::SchemaValidator::ValidationError)
end

#test_consume_message(handler_class_or_topic, payload, call_original: false, key: nil, partition_key: nil, skip_expectation: false, &block) ⇒ Object

Test that a given handler will consume a given payload correctly, i.e. that the schema is correct. If a block is given, that block will be executed when `consume` is called. Otherwise it will just confirm that `consume` is called at all. Deimos::Consumer or the topic as a string to continue as normal. Not compatible with a block. expectations on the consumer. Primarily used internally to Deimos.

Parameters:

  • handler_class_or_topic (Class|String)

    Class which inherits from

  • payload (Hash)

    the payload to consume

  • call_original (Boolean) (defaults to: false)

    if true, allow the consume handler

  • ignore_expectation (Boolean)

    Set to true to not place any

  • key (Object) (defaults to: nil)

    the key to use.

  • partition_key (Object) (defaults to: nil)

    the partition key to use.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/deimos/test_helpers.rb', line 182

def test_consume_message(handler_class_or_topic,
                         payload,
                         call_original: false,
                         key: nil,
                         partition_key: nil,
                         skip_expectation: false,
                         &block)
  raise 'Cannot have both call_original and be given a block!' if call_original && block_given?

  payload.stringify_keys! if payload.respond_to?(:stringify_keys!)
  handler_class = if handler_class_or_topic.is_a?(String)
                    _get_handler_class_from_topic(handler_class_or_topic)
                  else
                    handler_class_or_topic
                  end
  handler = handler_class.new
  allow(handler_class).to receive(:new).and_return(handler)
  listener = double('listener',
                    handler_class: handler_class,
                    encoding: nil)
  key ||= _key_from_consumer(handler_class)
  message = double('message',
                   'key' => key,
                   'partition_key' => partition_key,
                   'partition' => 1,
                   'offset' => 1,
                   'headers' => {},
                   'value' => payload)

  unless skip_expectation
    _handler_expectation(:consume,
                         payload,
                         handler,
                         call_original,
                         &block)
  end
  Phobos::Actions::ProcessMessage.new(
    listener: listener,
    message: message,
    listener_metadata: { topic: 'my-topic' }
  ).send(:process_message, payload)
end