|
2 | 2 |
|
3 | 3 | RSpec.describe ActiveSupport::Cache::AtomicDalliStore do |
4 | 4 | let(:options) do |
5 | | - { namespace: "epets_test", expires_in: 5.minutes } |
| 5 | + { namespace: "epets_test", expires_in: 2.seconds } |
6 | 6 | end |
7 | 7 |
|
| 8 | + let(:client) { subject.dalli } |
| 9 | + let(:exception) { Dalli::DalliError.new } |
| 10 | + |
| 11 | + let(:ttl_key) { "epets_test:foo.ttl" } |
| 12 | + let(:ttl_set_args) { [ttl_key, "", 2.seconds, raw: true] } |
| 13 | + let(:ttl_get_args) { [ttl_key, raw: true] } |
| 14 | + let(:ttl_add_args) { [ttl_key, "", 10, raw: true] } |
| 15 | + |
8 | 16 | around do |example| |
9 | | - subject.with do |client| |
10 | | - @client = client |
11 | | - @client.delete("epets_test:foo") |
12 | | - @client.delete("epets_test:foo.ttl") |
| 17 | + client.delete("epets_test:foo") |
| 18 | + client.delete("epets_test:foo.ttl") |
| 19 | + example.run |
| 20 | + end |
13 | 21 |
|
14 | | - example.run |
15 | | - end |
| 22 | + before do |
| 23 | + allow(client).to receive(:get).and_call_original |
| 24 | + allow(client).to receive(:set).and_call_original |
| 25 | + allow(client).to receive(:add).and_call_original |
| 26 | + allow(client).to receive(:delete).and_call_original |
16 | 27 | end |
17 | 28 |
|
18 | 29 | describe "#fetch" do |
|
27 | 38 | expect { |
28 | 39 | subject.fetch("foo", options) { "bar" } |
29 | 40 | }.to change { |
30 | | - @client.get("epets_test:foo") |
| 41 | + client.get("epets_test:foo") |
31 | 42 | }.from(nil).to("bar") |
32 | 43 | end |
33 | 44 |
|
34 | 45 | it "writes the TTL value to the cache" do |
35 | 46 | expect { |
36 | 47 | subject.fetch("foo", options) { "bar" } |
37 | 48 | }.to change { |
38 | | - @client.get("epets_test:foo.ttl") |
| 49 | + client.get("epets_test:foo.ttl") |
39 | 50 | }.from(nil).to("") |
40 | 51 | end |
41 | 52 |
|
42 | 53 | it "returns the value" do |
43 | 54 | expect(subject.fetch("foo", options) { "bar" }).to eq("bar") |
44 | 55 | end |
| 56 | + |
| 57 | + it "handles exceptions" do |
| 58 | + expect(subject.dalli).to receive(:set).with(*ttl_set_args).and_raise(exception) |
| 59 | + expect(subject.fetch("foo", options) { "bar" }).to eq("bar") |
| 60 | + end |
45 | 61 | end |
46 | 62 |
|
47 | 63 | context "when the cache is set" do |
|
58 | 74 | it "returns the value" do |
59 | 75 | expect(subject.fetch("foo", options) { "bar" }).to eq("bar") |
60 | 76 | end |
| 77 | + |
| 78 | + it "handles exceptions when reading the lock" do |
| 79 | + expect(subject.dalli).to receive(:get).with(*ttl_get_args).and_raise(exception) |
| 80 | + expect(subject.fetch("foo", options) { "bar" }).to eq("bar") |
| 81 | + end |
| 82 | + |
| 83 | + it "handles exceptions when setting the lock" do |
| 84 | + client.delete(ttl_key) |
| 85 | + expect(subject.dalli).to receive(:add).with(*ttl_add_args).and_raise(exception) |
| 86 | + expect(subject.fetch("foo", options) { "bar" }).to eq("bar") |
| 87 | + end |
61 | 88 | end |
62 | 89 | end |
63 | 90 |
|
64 | 91 | describe "#read" do |
65 | 92 | context "when the cache is not set" do |
66 | 93 | it "returns nil" do |
67 | | - expect(subject.read("foo")).to be_nil |
| 94 | + expect(subject.read("foo", options)).to be_nil |
68 | 95 | end |
69 | 96 | end |
70 | 97 |
|
|
76 | 103 | it "returns the value" do |
77 | 104 | expect(subject.read("foo", options)).to eq("bar") |
78 | 105 | end |
| 106 | + |
| 107 | + it "handles exceptions when reading the lock" do |
| 108 | + expect(subject.dalli).to receive(:get).with(*ttl_get_args).and_raise(exception) |
| 109 | + expect(subject.read("foo", options)).to eq("bar") |
| 110 | + end |
| 111 | + |
| 112 | + it "handles exceptions when setting the lock" do |
| 113 | + client.delete(ttl_key) |
| 114 | + expect(subject.dalli).to receive(:add).with(*ttl_add_args).and_raise(exception) |
| 115 | + expect(subject.read("foo", options)).to eq("bar") |
| 116 | + end |
79 | 117 | end |
80 | 118 | end |
81 | 119 |
|
|
84 | 122 | expect { |
85 | 123 | subject.write("foo", "bar", options) |
86 | 124 | }.to change { |
87 | | - @client.get("epets_test:foo") |
| 125 | + client.get("epets_test:foo") |
88 | 126 | }.from(nil).to("bar") |
89 | 127 | end |
90 | 128 |
|
91 | 129 | it "writes the TTL value to the cache" do |
92 | 130 | expect { |
93 | 131 | subject.write("foo", "bar", options) |
94 | 132 | }.to change { |
95 | | - @client.get("epets_test:foo.ttl") |
| 133 | + client.get("epets_test:foo.ttl") |
96 | 134 | }.from(nil).to("") |
97 | 135 | end |
| 136 | + |
| 137 | + it "handles exceptions when setting the TTL" do |
| 138 | + expect(subject.dalli).to receive(:set).with(*ttl_set_args).and_raise(exception) |
| 139 | + expect(subject.write("foo", "bar", options)).to be_falsey |
| 140 | + end |
98 | 141 | end |
99 | 142 |
|
100 | 143 | describe "#delete" do |
|
106 | 149 | expect { |
107 | 150 | subject.delete("foo", options) |
108 | 151 | }.to change { |
109 | | - @client.get("epets_test:foo") |
| 152 | + client.get("epets_test:foo") |
110 | 153 | }.from("bar").to(nil) |
111 | 154 | end |
112 | 155 |
|
113 | 156 | it "deletes the TTL value from the cache" do |
114 | 157 | expect { |
115 | 158 | subject.delete("foo", options) |
116 | 159 | }.to change { |
117 | | - @client.get("epets_test:foo.ttl") |
| 160 | + client.get("epets_test:foo.ttl") |
118 | 161 | }.from("").to(nil) |
119 | 162 | end |
| 163 | + |
| 164 | + it "handles exceptions when deleting the TTL" do |
| 165 | + expect(subject.dalli).to receive(:delete).with(ttl_key).and_raise(exception) |
| 166 | + expect(subject.delete("foo", options)).to be_falsey |
| 167 | + end |
120 | 168 | end |
121 | 169 | end |
0 commit comments