Class: DeliveryBoy::Fake
- Inherits:
-
Object
- Object
- DeliveryBoy::Fake
- Defined in:
- lib/delivery_boy/fake.rb
Overview
A fake implementation that is useful for testing.
Defined Under Namespace
Classes: FakeMessage
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all messages stored in memory.
- #deliver(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now) ⇒ Object (also: #deliver_async!)
- #deliver_messages ⇒ Object
-
#initialize ⇒ Fake
constructor
A new instance of Fake.
-
#messages_for(topic) ⇒ Object
Return all messages written to the specified topic.
- #produce(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ Fake
Returns a new instance of Fake.
11 12 13 14 15 |
# 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 |
Instance Method Details
#clear ⇒ Object
Clear all messages stored in memory.
55 56 57 58 59 60 |
# File 'lib/delivery_boy/fake.rb', line 55 def clear @delivery_lock.synchronize do @messages.clear @buffer.clear end end |
#deliver(value, topic:, key: nil, headers: {}, partition: nil, partition_key: nil, create_time: Time.now) ⇒ Object Also known as: deliver_async!
17 18 19 20 21 22 23 24 25 26 |
# 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 = FakeMessage.new(value, topic, key, headers, offset, partition, partition_key, create_time) @messages[topic] << end nil end |
#deliver_messages ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/delivery_boy/fake.rb', line 41 def @delivery_lock.synchronize do @buffer.each do |topic, | @messages[topic].push(*) end @buffer.clear end end |
#messages_for(topic) ⇒ Object
Return all messages written to the specified topic.
63 64 65 66 67 68 69 |
# File 'lib/delivery_boy/fake.rb', line 63 def (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) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# 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 = FakeMessage.new(value, topic, key, headers, offset, partition, partition_key, create_time) @buffer[topic] << end nil end |
#shutdown ⇒ Object
50 51 52 |
# File 'lib/delivery_boy/fake.rb', line 50 def shutdown clear end |