Sha256: 8627cb8e7ded51a1d75efd5bdbe8aa5a0599525847b6075c55d93d2643670026

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 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

      def change_role_to(role)
        @info['role'] = role
      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 change_role_to(role)
      @proxy.change_role_to(role)
    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.3.0 spec/support/redis_stub.rb
redis_failover-0.2.0 spec/support/redis_stub.rb