Sha256: 4fbefdfb6d4f08d201f1108b0c6eeccf6f5550f5f15d264be94e5b7a204769b2

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require 'securerandom'

module FakeSQS

  ReadCountOutOfRange = Class.new(RuntimeError)

  class Queue

    attr_reader :name, :messages, :message_factory, :messages_in_flight

    def initialize(options = {})
      @name = options.fetch("QueueName")
      @message_factory = options.fetch(:message_factory)
      reset
    end

    def send_message(options = {})
      message = message_factory.new(options)
      messages << message
      message
    end

    def receive_message(options = {})
      amount = Integer options.fetch("MaxNumberOfMessages") { "1" }

      fail ReadCountOutOfRange, amount if amount > 10

      return {} if messages.empty?

      result = {}

      actual_amount = amount > size ? size : amount

      actual_amount.times do
        message = messages.delete_at(rand(size))
        receipt = generate_receipt
        messages_in_flight[receipt] = message
        result[receipt] = message
      end

      result
    end

    def delete_message(receipt)
      message = messages_in_flight.delete(receipt)
    end

    def reset
      @messages = []
      @messages_in_flight = {}
    end

    def expire
      @messages += messages_in_flight.values
      @messages_in_flight = {}
    end

    def size
      messages.size
    end

    def generate_receipt
      SecureRandom.hex
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fake_sqs-0.0.7 lib/fake_sqs/queue.rb
fake_sqs-0.0.6 lib/fake_sqs/queue.rb
fake_sqs-0.0.5 lib/fake_sqs/queue.rb
fake_sqs-0.0.4 lib/fake_sqs/queue.rb
fake_sqs-0.0.3 lib/fake_sqs/queue.rb
fake_sqs-0.0.2 lib/fake_sqs/queue.rb