|
| 1 | +require 'flipper/adapters/fallback_to_cached' |
| 2 | + |
| 3 | +RSpec.describe Flipper::Adapters::FallbackToCached do |
| 4 | + let(:adapter) { Flipper::Adapters::Memory.new } |
| 5 | + let(:flipper) { Flipper.new(subject, memoize: false) } |
| 6 | + let(:feature_a) { flipper[:malware_rule] } |
| 7 | + let(:feature_b) { flipper[:spam_rule] } |
| 8 | + |
| 9 | + subject { described_class.new(adapter) } |
| 10 | + |
| 11 | + before do |
| 12 | + feature_a.enable |
| 13 | + feature_b.disable |
| 14 | + end |
| 15 | + |
| 16 | + describe "#features" do |
| 17 | + it "uses primary adapter by default and caches value" do |
| 18 | + expect(adapter).to receive(:features).and_call_original |
| 19 | + expect(subject.features).to_not be_empty |
| 20 | + end |
| 21 | + |
| 22 | + it "falls back to cached value if primary adapter raises an error" do |
| 23 | + subject.features |
| 24 | + expect(adapter).to receive(:features).and_raise(StandardError) |
| 25 | + expect(subject.features).to_not be_empty |
| 26 | + end |
| 27 | + |
| 28 | + it "raises an error if primary adapter fails and cache is empty" do |
| 29 | + expect(adapter).to receive(:features).and_raise(StandardError) |
| 30 | + expect { subject.features }.to raise_error StandardError |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe "#get" do |
| 35 | + it "uses primary adapter by default and caches value" do |
| 36 | + expect(adapter).to receive(:get).with(feature_a).and_call_original |
| 37 | + expect(subject.get(feature_a)).to_not be_nil |
| 38 | + end |
| 39 | + |
| 40 | + it "falls back to cached value if primary adapter raises an error" do |
| 41 | + subject.get(feature_a) |
| 42 | + expect(adapter).to receive(:get).with(feature_a).and_raise(StandardError) |
| 43 | + expect(subject.get(feature_a)).to_not be_nil |
| 44 | + end |
| 45 | + |
| 46 | + it "raises an error if primary adapter fails and cache is empty" do |
| 47 | + expect(adapter).to receive(:get).with(feature_a).and_raise(StandardError) |
| 48 | + expect { subject.get(feature_a) }.to raise_error StandardError |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe "#get_multi" do |
| 53 | + it "uses primary adapter by default and caches value" do |
| 54 | + expect(adapter).to receive(:get_multi).with([feature_a, feature_b]).and_call_original |
| 55 | + expect(subject.get_multi([feature_a, feature_b])).to_not be_empty |
| 56 | + end |
| 57 | + |
| 58 | + it "falls back to cached value if primary adapter raises an error" do |
| 59 | + subject.get_multi([feature_a, feature_b]) |
| 60 | + expect(adapter).to receive(:get_multi).with([feature_a, feature_b]).and_raise(StandardError) |
| 61 | + expect(subject.get_multi([feature_a, feature_b])).to_not be_empty |
| 62 | + end |
| 63 | + |
| 64 | + it "raises an error if primary adapter fails and cache is empty" do |
| 65 | + expect(adapter).to receive(:get_multi).with([feature_a, feature_b]).and_raise(StandardError) |
| 66 | + expect { subject.get_multi([feature_a, feature_b]) }.to raise_error StandardError |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "#get_all" do |
| 71 | + it "uses primary adapter by default and caches value" do |
| 72 | + expect(adapter).to receive(:get_all).and_call_original |
| 73 | + expect(subject.get_all).to_not be_empty |
| 74 | + end |
| 75 | + |
| 76 | + it "falls back to cached value if primary adapter raises an error" do |
| 77 | + subject.get_all |
| 78 | + expect(adapter).to receive(:get_all).and_raise(StandardError) |
| 79 | + expect(subject.get_all).to_not be_empty |
| 80 | + end |
| 81 | + |
| 82 | + it "raises an error if primary adapter fails and cache is empty" do |
| 83 | + subject.cache.clear |
| 84 | + expect(adapter).to receive(:get_all).and_raise(StandardError) |
| 85 | + expect { subject.get_all }.to raise_error StandardError |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments