|
3 | 3 | require "sidekiq/throttled/expirable_set" |
4 | 4 |
|
5 | 5 | RSpec.describe Sidekiq::Throttled::ExpirableSet do |
6 | | - subject(:expirable_set) { described_class.new(2.0) } |
| 6 | + subject(:expirable_set) { described_class.new } |
7 | 7 |
|
8 | 8 | it { is_expected.to be_an Enumerable } |
9 | 9 |
|
10 | | - describe ".new" do |
| 10 | + describe "#add" do |
11 | 11 | it "raises ArgumentError if given TTL is not Float" do |
12 | | - expect { described_class.new(42) }.to raise_error(ArgumentError) |
| 12 | + expect { expirable_set.add("a", ttl: 42) }.to raise_error(ArgumentError) |
13 | 13 | end |
14 | 14 |
|
15 | 15 | it "raises ArgumentError if given TTL is not positive" do |
16 | | - expect { described_class.new(0.0) }.to raise_error(ArgumentError) |
| 16 | + expect { expirable_set.add("a", ttl: 0.0) }.to raise_error(ArgumentError) |
17 | 17 | end |
18 | | - end |
19 | 18 |
|
20 | | - describe "#add" do |
21 | 19 | it "returns self" do |
22 | | - expect(expirable_set.add("a")).to be expirable_set |
| 20 | + expect(expirable_set.add("a", ttl: 1.0)).to be expirable_set |
23 | 21 | end |
24 | 22 |
|
25 | 23 | it "adds uniq elements to the set" do |
26 | | - expirable_set.add("a").add("b").add("b").add("a") |
| 24 | + expirable_set.add("a", ttl: 1.0).add("b", ttl: 1.0).add("b", ttl: 1.0).add("a", ttl: 1.0) |
27 | 25 |
|
28 | 26 | expect(expirable_set).to contain_exactly("a", "b") |
29 | 27 | end |
| 28 | + |
| 29 | + it "uses longest sunset" do |
| 30 | + monotonic_time = 0.0 |
| 31 | + allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC) { monotonic_time } |
| 32 | + |
| 33 | + expirable_set.add("a", ttl: 1.0).add("b", ttl: 42.0).add("b", ttl: 1.0).add("a", ttl: 2.0) |
| 34 | + |
| 35 | + monotonic_time += 0.5 |
| 36 | + expect(expirable_set).to contain_exactly("a", "b") |
| 37 | + |
| 38 | + monotonic_time += 1.0 |
| 39 | + expect(expirable_set).to contain_exactly("a", "b") |
| 40 | + |
| 41 | + monotonic_time += 0.5 |
| 42 | + expect(expirable_set).to contain_exactly("b") |
| 43 | + end |
30 | 44 | end |
31 | 45 |
|
32 | 46 | describe "#each" do |
|
37 | 51 |
|
38 | 52 | allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC) { monotonic_time } |
39 | 53 |
|
40 | | - expirable_set.add("lorem") |
41 | | - expirable_set.add("ipsum") |
| 54 | + expirable_set.add("lorem", ttl: 1.0) |
| 55 | + expirable_set.add("ipsum", ttl: 1.0) |
42 | 56 |
|
43 | | - monotonic_time += 1 |
| 57 | + monotonic_time += 0.5 |
44 | 58 |
|
45 | | - expirable_set.add("ipsum") |
| 59 | + expirable_set.add("ipsum", ttl: 1.0) |
46 | 60 |
|
47 | | - monotonic_time += 1 |
| 61 | + monotonic_time += 0.5 |
48 | 62 |
|
49 | | - expirable_set.add("dolor") |
| 63 | + expirable_set.add("dolor", ttl: 1.0) |
50 | 64 | end |
51 | 65 |
|
52 | 66 | it { is_expected.to be_an(Enumerator) } |
|
0 commit comments