|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe BigRails::Redis::Registry do |
| 4 | + subject(:instance) { described_class.new } |
| 5 | + |
| 6 | + before { load_config("simple") } |
| 7 | + |
| 8 | + describe "#for" do |
| 9 | + context "when name is not valid" do |
| 10 | + it "raises error" do |
| 11 | + expect { |
| 12 | + instance.for("foo") |
| 13 | + }.to raise_error(described_class::UnknownConnection, "connection for 'foo' is not registered") |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + it "stores connection" do |
| 18 | + expect(instance.builder).to receive(:call).and_call_original.once |
| 19 | + expect(instance.for("default")).to be_a(::Redis) |
| 20 | + # Second call should not build connection again |
| 21 | + instance.for("default") |
| 22 | + end |
| 23 | + |
| 24 | + context "with pool options" do |
| 25 | + it "returns connection pool" do |
| 26 | + expect(instance.for("pooled")).to be_a(::ConnectionPool) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context "when wrapped" do |
| 31 | + it "stores wrapped connection" do |
| 32 | + expect(instance.for("pooled", wrapped: true).wrapped_pool).to be_a(::ConnectionPool) |
| 33 | + end |
| 34 | + |
| 35 | + context "when has no pool" do |
| 36 | + it "returns redis instance" do |
| 37 | + expect(instance.for("default", wrapped: true)).to be_a(::Redis) |
| 38 | + expect(instance.for("default", wrapped: true)).not_to respond_to(:wrapped_pool) |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe "#config_for" do |
| 45 | + it "returns config for specific connection" do |
| 46 | + expect(instance.config_for("default").redis_options).to eq( |
| 47 | + url: "redis://localhost" |
| 48 | + ) |
| 49 | + |
| 50 | + expect(instance.config_for("pooled").redis_options).to eq( |
| 51 | + url: "redis://localhost/2" |
| 52 | + ) |
| 53 | + |
| 54 | + expect(instance.config_for("pooled").pool_options).to eq( |
| 55 | + timeout: 5, |
| 56 | + size: 5 |
| 57 | + ) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + describe "#each" do |
| 62 | + it "iterates through all connections" do |
| 63 | + instance.each do |conn| |
| 64 | + if conn.is_a? ::Redis |
| 65 | + expect(conn).to eq(instance.for("default")) |
| 66 | + elsif conn.is_a? ::ConnectionPool |
| 67 | + expect(conn).to eq(instance.for("pooled")) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe "#verify!" do |
| 74 | + context "with name" do |
| 75 | + it "verifies specified connection" do |
| 76 | + conn = instance.for("default") |
| 77 | + conn2 = instance.for("pooled") |
| 78 | + expect(conn).to receive(:ping).and_call_original |
| 79 | + conn2.with do |redis| |
| 80 | + expect(redis).to receive(:ping).and_call_original |
| 81 | + end |
| 82 | + |
| 83 | + instance.verify!("default") |
| 84 | + instance.verify!("pooled") |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "without" do |
| 89 | + it "verifies all connections" do |
| 90 | + conn = instance.for("default") |
| 91 | + conn2 = instance.for("pooled") |
| 92 | + expect(conn).to receive(:ping).and_call_original |
| 93 | + conn2.with do |redis| |
| 94 | + expect(redis).to receive(:ping).and_call_original |
| 95 | + end |
| 96 | + |
| 97 | + instance.verify! |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments