Sha256: 1decccbebc287c30c7a3390feefc23c46c222ad435fc7ba61b36be624cf82601

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'

require_relative '../../lib/routemaster/redis_broker'

describe Routemaster::RedisBroker do
  subject { Class.new(Routemaster::RedisBroker).instance }

  describe "#get" do
    let(:urls)   { ['redis://localhost/12'] }

    context "setting up a redis namespace" do
      let(:redis)           { instance_double(Redis, id: 1) }
      let(:redis_namespace) { instance_double(Redis::Namespace) }

      before do
        allow(Redis::Distributed).to receive(:new) { redis }
        allow(Redis::Namespace).to receive(:new) { redis_namespace }
      end

      it 'returns a namespaced redis connection' do
        expect(subject.get(:name, urls: urls)).to eq(redis_namespace)
      end

      it 'uses the url to initialise redis' do
        expect(Redis::Distributed).to receive(:new).with(urls)
        subject.get(:name, urls: urls)
      end

      it 'namespaces with rm by default' do
        expect(Redis::Namespace).to receive(:new).with('rm', redis: redis)
        subject.get(:name, urls: urls)
      end

      it 'can use a namespace based on the url' do
        expect(Redis::Namespace).to receive(:new).with('other', redis: redis)
        subject.get(:name, urls: ['redis://localhost/12/other'])
      end
    end

    context "when we are in the same process" do
      it 'is a single connection for each url' do
        expect(subject.get(:name, urls: urls)).to eql(subject.get(:name, urls: urls))
      end
    end

    context "when we have forked" do
      let!(:connection) { subject.get(:name, urls: urls) }

      it 'is a new connection for the newly forked process' do
        # this is tied to implementation, but tests a 'fork' well enough
        allow(Process).to receive(:pid) { -1 }
        expect(subject.get(:name, urls: urls)).to_not eql(connection)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
routemaster-drain-2.5.2 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.5.1 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.5.0 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.4.4 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.4.3 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.4.2 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.4.1 spec/routemaster/redis_broker_spec.rb
routemaster-drain-2.4.0 spec/routemaster/redis_broker_spec.rb