Sha256: 18ff612af64bb319860bdd18fc64526a34a2e6da51d1be8f2fc88bc0534baba6

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require 'ostruct'

module RedisFailover
  # Test stub for Redis.
  class RedisStub
    class Proxy
      def initialize(queue, opts = {})
        @info = {:role => 'master'}
        @queue = queue
      end

      def blpop(*args)
        @queue.pop.tap do |value|
          raise value if value
        end
      end

      def del(*args)
      end

      def lpush(*args)
        @queue << nil
      end

      def slaveof(host, port)
        if host == 'no' && port == 'one'
          @info[:role] = 'master'
        else
          @info[:role] = 'slave'
        end
      end

      def info
        @info.dup
      end

      def ping
        'pong'
      end
    end

    attr_reader :host, :port, :reachable
    def initialize(opts = {})
      @host = opts[:host]
      @port = Integer(opts[:port])
      @queue = Queue.new
      @proxy = Proxy.new(@queue, opts)
      @reachable = true
    end

    def method_missing(method, *args, &block)
      if @reachable
        @proxy.send(method, *args, &block)
      else
        raise Errno::ECONNREFUSED
      end
    end

    def make_reachable!
      @reachable = true
    end

    def make_unreachable!
      @queue << Errno::ECONNREFUSED
      @reachable = false
    end

    def to_s
      "#{@host}:#{@port}"
    end

    def client
      OpenStruct.new(:host => @host, :port => @port)
    end
  end

  module RedisStubSupport
    def redis
      @redis ||= RedisStub.new(:host => @host, :port => @port)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis_failover-0.1.1 spec/support/redis_stub.rb
redis_failover-0.1.0 spec/support/redis_stub.rb