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!void

This method returns an undefined value.

Kafka test config with avro schema registry



36
37
38
39
40
41
# File 'lib/deimos/test_helpers.rb', line 36

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!void

This method returns an undefined value.

Set the config to the right settings for a kafka test



45
46
47
48
49
50
# File 'lib/deimos/test_helpers.rb', line 45

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!void

This method returns an undefined value.

Set the config to the right settings for a unit test



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

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

#clear_kafka_messages!void

This method returns an undefined value.

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



174
175
176
# File 'lib/deimos/test_helpers.rb', line 174

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

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

This method returns an undefined value.

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

  • keys (Array<Hash,String>) (defaults to: [])
  • partition_keys (Array<Integer>) (defaults to: [])
  • call_original (Boolean) (defaults to: false)
  • skip_expectation (Boolean) (defaults to: false)


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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/deimos/test_helpers.rb', line 258

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) ⇒ void

This method returns an undefined value.

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

Parameters:

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


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/deimos/test_helpers.rb', line 317

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) ⇒ void

This method returns an undefined value.

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

Parameters:

  • handler_class (Class)
  • payload (Hash)


239
240
241
242
243
244
# File 'lib/deimos/test_helpers.rb', line 239

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) ⇒ void

This method returns an undefined value.

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

  • skip_expectation (Boolean) (defaults to: false)

    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.



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
224
225
226
227
228
229
230
231
232
233
# File 'lib/deimos/test_helpers.rb', line 192

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