|
| 1 | +RSpec.describe DontRepeatFor do |
| 2 | + let(:fake_redis) { FakeRedis.new } |
| 3 | + |
| 4 | + around do |example| |
| 5 | + old_redis = $redis if defined?($redis) |
| 6 | + was_defined = defined?($redis) |
| 7 | + $redis = fake_redis |
| 8 | + fake_redis.reset! |
| 9 | + example.run |
| 10 | + ensure |
| 11 | + if was_defined |
| 12 | + $redis = old_redis |
| 13 | + else |
| 14 | + $redis = nil |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + describe "VERSION" do |
| 19 | + it "is defined" do |
| 20 | + expect(DontRepeatFor::VERSION).to be_a(String) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe "#initialize" do |
| 25 | + it "executes the block the first time" do |
| 26 | + executed = false |
| 27 | + DontRepeatFor.new(300, "test/first_run") { executed = true } |
| 28 | + expect(executed).to be true |
| 29 | + end |
| 30 | + |
| 31 | + it "does not execute the block on a second call with the same key" do |
| 32 | + call_count = 0 |
| 33 | + 2.times { DontRepeatFor.new(300, "test/duplicate") { call_count += 1 } } |
| 34 | + expect(call_count).to eq(1) |
| 35 | + end |
| 36 | + |
| 37 | + it "does not execute the block on any subsequent calls with the same key" do |
| 38 | + call_count = 0 |
| 39 | + 5.times { DontRepeatFor.new(300, "test/many") { call_count += 1 } } |
| 40 | + expect(call_count).to eq(1) |
| 41 | + end |
| 42 | + |
| 43 | + it "executes blocks with different keys independently" do |
| 44 | + results = [] |
| 45 | + DontRepeatFor.new(300, "test/a") { results << :a } |
| 46 | + DontRepeatFor.new(300, "test/b") { results << :b } |
| 47 | + expect(results).to eq([:a, :b]) |
| 48 | + end |
| 49 | + |
| 50 | + it "converts time to integer" do |
| 51 | + instance = DontRepeatFor.new(300.5, "test/float") {} |
| 52 | + expect(instance.time).to eq(300) |
| 53 | + end |
| 54 | + |
| 55 | + it "exposes key and time as accessors" do |
| 56 | + instance = DontRepeatFor.new(600, "test/accessors") {} |
| 57 | + expect(instance.key).to eq("test/accessors") |
| 58 | + expect(instance.time).to eq(600) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe "redis key format" do |
| 63 | + it "namespaces keys under DontRepeatFor/v1/Keys/" do |
| 64 | + DontRepeatFor.new(300, "my/custom/key") {} |
| 65 | + expected_key = "DontRepeatFor/v1/Keys/my/custom/key" |
| 66 | + expect(fake_redis.expirations).to have_key(expected_key) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "expiry" do |
| 71 | + it "sets the expiry to the given time in seconds" do |
| 72 | + DontRepeatFor.new(600, "test/expiry") {} |
| 73 | + expected_key = "DontRepeatFor/v1/Keys/test/expiry" |
| 74 | + expect(fake_redis.expirations[expected_key]).to eq(600) |
| 75 | + end |
| 76 | + |
| 77 | + it "does not set expiry on duplicate calls" do |
| 78 | + DontRepeatFor.new(600, "test/expiry_once") {} |
| 79 | + fake_redis.expirations.clear |
| 80 | + DontRepeatFor.new(600, "test/expiry_once") {} |
| 81 | + expect(fake_redis.expirations).to be_empty |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "redis connection" do |
| 86 | + it "uses $redis global when defined" do |
| 87 | + executed = false |
| 88 | + DontRepeatFor.new(300, "test/global_redis") { executed = true } |
| 89 | + expect(executed).to be true |
| 90 | + expect(fake_redis.expirations).to have_key("DontRepeatFor/v1/Keys/test/global_redis") |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments