class DeliveryBoy::Fake

A fake implementation that is useful for testing.

Constants

FakeMessage

Public Class Methods

new() click to toggle source
# File lib/delivery_boy/fake.rb, line 11
def initialize
  @messages = Hash.new {|h, k| h[k] = [] }
  @buffer = Hash.new {|h, k| h[k] = [] }
  @delivery_lock = Mutex.new
end

Public Instance Methods

buffer_size() click to toggle source
# File lib/delivery_boy/fake.rb, line 60
def buffer_size
  @delivery_lock.synchronize do
    @buffer.values.flatten.size
  end
end
clear() click to toggle source

Clear all messages stored in memory.

# File lib/delivery_boy/fake.rb, line 67
def clear
  @delivery_lock.synchronize do
    @messages.clear
    @buffer.clear
  end
end
clear_buffer() click to toggle source
# File lib/delivery_boy/fake.rb, line 54
def clear_buffer
  @delivery_lock.synchronize do
    @buffer.clear
  end
end
deliver(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now) click to toggle source
# File lib/delivery_boy/fake.rb, line 17
def deliver(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now)
  @delivery_lock.synchronize do
    offset = @messages[topic].count
    message = FakeMessage.new(value, topic, key, headers, offset, partition, partition_key, create_time)

    @messages[topic] << message
  end

  nil
end
Also aliased as: deliver_async!
deliver_async!(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now)
Alias for: deliver
deliver_messages() click to toggle source
# File lib/delivery_boy/fake.rb, line 41
def deliver_messages
  @delivery_lock.synchronize do
    @buffer.each do |topic, messages|
      @messages[topic].push(*messages)
    end
    @buffer.clear
  end
end
messages_for(topic) click to toggle source

Return all messages written to the specified topic.

# File lib/delivery_boy/fake.rb, line 75
def messages_for(topic)
  @delivery_lock.synchronize do
    # Return a clone so that the list of messages can be traversed
    # without worrying about a concurrent modification
    @messages[topic].clone
  end
end
produce(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now) click to toggle source
# File lib/delivery_boy/fake.rb, line 30
def produce(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now)
  @delivery_lock.synchronize do
    offset = @buffer[topic].count
    message = FakeMessage.new(value, topic, key, headers, offset, partition, partition_key, create_time)

    @buffer[topic] << message
  end

  nil
end
shutdown() click to toggle source
# File lib/delivery_boy/fake.rb, line 50
def shutdown
  clear
end