Permalink
Browse files

Merge pull request #685 from FeeFighters/master

Fixed SQS :receive_message mock bug
  • Loading branch information...
2 parents a1b8159 + 77d0a1b commit 00ce6bcdd2c28437cd29a6067bce1cd29addd8c6 @geemus geemus committed Jan 10, 2012
Showing with 13 additions and 14 deletions.
  1. +13 −14 lib/fog/aws/requests/sqs/receive_message.rb
@@ -37,37 +37,36 @@ def receive_message(queue_url, options = {})
max_number_of_messages = options['MaxNumberOfMessages'] || 1
now = Time.now
- keys = queue[:messages].keys[0, max_number_of_messages]
-
- messages = queue[:messages].values_at(*keys).map do |m|
+ messages = []
+
+ queue[:messages].values.each do |m|
message_id = m['MessageId']
-
+
invisible = if (received_handles = queue[:receipt_handles][message_id])
visibility_timeout = m['Attributes']['VisibilityTimeout'] || queue['Attributes']['VisibilityTimeout']
received_handles.any? { |handle, time| now < time + visibility_timeout }
else
false
end
-
- if invisible
- nil
- else
+
+ unless invisible
receipt_handle = Fog::Mock.random_base64(300)
-
+
queue[:receipt_handles][message_id] ||= {}
queue[:receipt_handles][message_id][receipt_handle] = now
-
+
m['Attributes'].tap do |attrs|
attrs['ApproximateFirstReceiveTimestamp'] ||= now
attrs['ApproximateReceiveCount'] = (attrs['ApproximateReceiveCount'] || 0) + 1
end
-
- m.merge({
+
+ messages << m.merge({
'ReceiptHandle' => receipt_handle
})
+ break if messages.size >= max_number_of_messages
end
- end.compact
-
+ end
+
response.body = {
'ResponseMetadata' => {
'RequestId' => Fog::AWS::Mock.request_id

0 comments on commit 00ce6bc

Please sign in to comment.