Module: Rimless::RSpec::Helpers

Defined in:
lib/rimless/rspec/helpers.rb

Overview

A collection of Rimless/RSpec helpers.

Instance Method Summary collapse

Instance Method Details

#avro_parse(data) ⇒ Hash{String => Mixed}

A simple helper to parse a blob of Apache Avro data.

Parameters:

  • data (String)

    the Apache Avro blob

  • opts (Hash{Symbol => Mixed})

    additional options

Returns:

  • (Hash{String => Mixed})

    the parsed payload



13
14
15
# File 'lib/rimless/rspec/helpers.rb', line 13

def avro_parse(data, **)
  Rimless.avro_decode(data, **)
end

#capture_kafka_messages { ... } ⇒ Array<Hash{Symbol => Mixed}>

Capture all Apache Kafka messages of the given block.

Yields:

  • the given block to capture the messages

Returns:

  • (Array<Hash{Symbol => Mixed}>)

    the captured messages



57
58
59
# File 'lib/rimless/rspec/helpers.rb', line 57

def capture_kafka_messages(&)
  Rimless::RSpec::Matchers::HaveSentKafkaMessage.new(nil).capture(&)
end

#kafka_consumer_for(topic) ⇒ Karafka::BaseConsumer

An augmented helper for karafka.consumer_for, provided by the karafka-testing gem to locate and instantiate a consumer. When the found consumer features the Rimless job bridge consumer logic, the enqueue_job is replaced to not enqueue the job, but perform it inline. Otherwise the end-user consumer logic is not executed, which is clearly the user expectation.

Parameters:

  • topic (String)

    the full topic name, use Rimless.topic if needed

Returns:

  • (Karafka::BaseConsumer)

    the found consumer



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rimless/rspec/helpers.rb', line 71

def kafka_consumer_for(topic)
  # The +karafka+ helper is provided by the +karafka-testing+ gem
  karafka.consumer_for(topic).tap do |consumer|
    # When we're not dealing with a regular Rimless job bridge consumer,
    # we skip further processing
    next unless consumer.respond_to? :enqueue_job

    # Otherwise rig the job bridging and run the wrapped consumer instead
    allow(consumer).to receive(:enqueue_job) do |message|
      Rimless.configuration.consumer_job_class.perform_now(
        **consumer.message_to_job_args(message)
      )
    end
  end
end

#kafka_message(topic: nil, headers: {}, metadata: {}, **payload) ⇒ RSpec::Mocks::InstanceVerifyingDouble

A simple helper to generate Apache Kafka message doubles for consuming.

rubocop:disable Metrics/MethodLength – because of the metadata handling

Parameters:

  • payload (Hash{Symbol => Mixed})

    the message payload

  • topic (String, Hash{Symbol => Mixed}) (defaults to: nil)

    the actual message topic (full as string, or parts via hash)

  • metadata (Hash{Symbol => Mixed}) (defaults to: {})

    the message metadata

Returns:

  • (RSpec::Mocks::InstanceVerifyingDouble)

    the Kafka message double



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
# File 'lib/rimless/rspec/helpers.rb', line 26

def kafka_message(topic: nil, headers: {}, metadata: {}, **payload)
   = {
    topic: topic ? Rimless.topic(topic) : nil,
    partition: 0,
    offset: 206,
    key: nil,
    headers: headers,
    timestamp: Time.current,
    received_at: Time.current,
    **
  }

  instance_double(
    Karafka::Messages::Message,
    deserialized?: true,
    tombstone?: false,
    payload: payload,
    metadata: instance_double(
      Karafka::Messages::Metadata,
      **,
      to_h: 
    ),
    **
  )
end