mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
get_message and list_messages mocks.
This commit is contained in:
parent
f2829cf330
commit
625ca8e255
4 changed files with 83 additions and 12 deletions
|
@ -92,14 +92,13 @@ module Fog
|
|||
|
||||
# An in-memory Queue implementation.
|
||||
class MockQueue
|
||||
attr_accessor :messages
|
||||
attr_accessor :metadata
|
||||
attr_accessor :name, :metadata, :messages
|
||||
attr_accessor :claimed, :free
|
||||
|
||||
def initialize
|
||||
@messages = []
|
||||
@metadata = {}
|
||||
@claimed, @free = 0, 0, 0
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@messages, @metadata = [], {}
|
||||
@claimed, @free = 0, 0
|
||||
@id_counter = Fog::Mock.random_hex(24).to_i(16)
|
||||
end
|
||||
|
||||
|
@ -119,7 +118,7 @@ module Fog
|
|||
def add_message(client_id, data, ttl)
|
||||
id = @id_counter.to_s(16)
|
||||
@id_counter += 1
|
||||
message = MockMessage.new(id, client_id, data, ttl)
|
||||
message = MockMessage.new(id, self, client_id, data, ttl)
|
||||
@messages << message
|
||||
message
|
||||
end
|
||||
|
@ -127,12 +126,33 @@ module Fog
|
|||
|
||||
# A single message posted to an in-memory MockQueue.
|
||||
class MockMessage
|
||||
attr_accessor :id, :data, :ttl, :producer_id
|
||||
attr_accessor :id, :queue, :data, :ttl, :producer_id, :claimant_id
|
||||
|
||||
# Create a new message. Use {MockQueue#add_message} instead.
|
||||
def initialize(id, client_id, data, ttl)
|
||||
@id, @producer_id = id, client_id
|
||||
@data, @ttl = client_id, data, ttl
|
||||
def initialize(id, queue, client_id, data, ttl)
|
||||
@id, @queue, @producer_id = id, queue, client_id
|
||||
@data, @ttl = data, ttl
|
||||
@created = Time.now.to_i
|
||||
@claimant_id = nil
|
||||
end
|
||||
|
||||
# Determine how long ago this message was created, in seconds.
|
||||
#
|
||||
# @return [Integer]
|
||||
def age
|
||||
Time.now.to_i - @created
|
||||
end
|
||||
|
||||
# Convert this message to a GET payload.
|
||||
#
|
||||
# @return [Hash]
|
||||
def to_h
|
||||
{
|
||||
"body" => @data,
|
||||
"age" => age,
|
||||
"ttl" => @ttl,
|
||||
"href" => "/v1/queues/#{@queue.name}/messages/#{@id}"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ module Fog
|
|||
existed = ! data[queue_name].nil?
|
||||
|
||||
unless existed
|
||||
data[queue_name] = MockQueue.new
|
||||
data[queue_name] = MockQueue.new(queue_name)
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Queues
|
||||
|
||||
class Real
|
||||
|
||||
# This operation gets the specified message from the specified queue.
|
||||
|
@ -22,7 +23,24 @@ module Fog
|
|||
:headers => { 'Client-ID' => client_id }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_message(client_id, queue_name, message_id)
|
||||
queue = data[queue_name]
|
||||
raise NotFound.new unless queue
|
||||
|
||||
message = queue.messages.find { |msg| msg.id == message_id }
|
||||
raise NotFound.new unless message
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = message.to_h
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Queues
|
||||
|
||||
class Real
|
||||
|
||||
# This operation gets the message or messages in the specified queue.
|
||||
|
@ -38,6 +39,38 @@ module Fog
|
|||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def list_messages(client_id, queue_name, options = {})
|
||||
queue = data[queue_name]
|
||||
raise NotFound.new unless queue
|
||||
|
||||
marker = (options[:marker] || "0").to_i
|
||||
limit = options[:limit] || 10
|
||||
echo = options[:echo] || false
|
||||
include_claimed = options[:include_claimed] || false
|
||||
|
||||
next_marker = marker + limit + 1
|
||||
messages = queue.messages[marker...next_marker]
|
||||
messages.reject! { |m| m.producer_id == client_id } unless echo
|
||||
messages.select! { |m| m.claimant_id.nil? } unless include_claimed
|
||||
|
||||
response = Excon::Response.new
|
||||
if queue.messages.empty?
|
||||
response.status = 204
|
||||
else
|
||||
response.body = {
|
||||
"messages" => messages.map(&:to_h),
|
||||
"links" => [{
|
||||
"href" => "/v1/queues/#{queue_name}/messages?marker=#{next_marker}",
|
||||
"rel" => "next"
|
||||
}]
|
||||
}
|
||||
end
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue