|
| 1 | +require 'spec_helper' |
| 2 | +require 'resque/pool/config_loaders/throttled' |
| 3 | + |
| 4 | +module Resque::Pool::ConfigLoaders |
| 5 | + |
| 6 | + describe Throttled do |
| 7 | + let(:fake_time) { FakeTime.new 1445898807 } |
| 8 | + |
| 9 | + it "returns the config returned by the wrapped config loader for given env" do |
| 10 | + wrapped_config = { |
| 11 | + "dev" => {"qA,qB" => 1}, |
| 12 | + "prd" => {"qA,qB" => 4} |
| 13 | + } |
| 14 | + wrapped_loader = lambda {|env| wrapped_config[env] } |
| 15 | + throttle = Throttled.new(wrapped_loader) |
| 16 | + |
| 17 | + throttle.call("prd").should eq({"qA,qB" => 4}) |
| 18 | + end |
| 19 | + |
| 20 | + it "does not call wrapped loader again until the default period of time has elapsed" do |
| 21 | + wrapped_loader = TestConfigLoader.new |
| 22 | + wrapped_loader.configuration = {"qA,qB" => 1} |
| 23 | + |
| 24 | + throttle = Throttled.new(wrapped_loader, time_source: fake_time) |
| 25 | + first_call = throttle.call("prd") |
| 26 | + |
| 27 | + new_config = {"qA,qB" => 22} |
| 28 | + wrapped_loader.configuration = new_config |
| 29 | + fake_time.advance_time 6 |
| 30 | + # config changed, but not enough time elapsed |
| 31 | + |
| 32 | + second_call = throttle.call("prd") |
| 33 | + |
| 34 | + second_call.should eq(first_call) |
| 35 | + wrapped_loader.times_called.should == 1 |
| 36 | + |
| 37 | + fake_time.advance_time 6 |
| 38 | + # now, enough time has elapsed to retrieve latest config |
| 39 | + |
| 40 | + third_call = throttle.call("prd") |
| 41 | + |
| 42 | + third_call.should_not eq(first_call) |
| 43 | + third_call.should eq(new_config) |
| 44 | + wrapped_loader.times_called.should == 2 |
| 45 | + |
| 46 | + # further calls continue to use cached value |
| 47 | + throttle.call("prd") |
| 48 | + throttle.call("prd") |
| 49 | + throttle.call("prd") |
| 50 | + wrapped_loader.times_called.should == 2 |
| 51 | + end |
| 52 | + |
| 53 | + it "can specify an alternate cache period" do |
| 54 | + config0 = {foo: 2, bar: 1} |
| 55 | + config1 = {bar: 3, baz: 9} |
| 56 | + config2 = {foo: 4, quux: 1} |
| 57 | + wrapped_loader = TestConfigLoader.new |
| 58 | + wrapped_loader.configuration = config0 |
| 59 | + throttle = Throttled.new( |
| 60 | + wrapped_loader, period: 60, time_source: fake_time |
| 61 | + ) |
| 62 | + throttle.call("prd").should eq(config0) |
| 63 | + wrapped_loader.configuration = config1 |
| 64 | + fake_time.advance_time 59 |
| 65 | + throttle.call("prd").should eq(config0) |
| 66 | + fake_time.advance_time 5 |
| 67 | + throttle.call("prd").should eq(config1) |
| 68 | + wrapped_loader.configuration = config2 |
| 69 | + fake_time.advance_time 59 |
| 70 | + throttle.call("prd").should eq(config1) |
| 71 | + fake_time.advance_time 2 |
| 72 | + throttle.call("prd").should eq(config2) |
| 73 | + end |
| 74 | + |
| 75 | + it "forces a call to the wrapperd loader after reset! called, even if required time hasn't elapsed" do |
| 76 | + wrapped_loader = TestConfigLoader.new |
| 77 | + wrapped_loader.configuration = {"qA,qB" => 1} |
| 78 | + |
| 79 | + throttle = Throttled.new(wrapped_loader, time_source: fake_time) |
| 80 | + throttle.call("prd") |
| 81 | + |
| 82 | + new_config = {"qA,qB" => 22} |
| 83 | + wrapped_loader.configuration = new_config |
| 84 | + fake_time.advance_time 6 |
| 85 | + # the 10 second period has *not* elapsed |
| 86 | + |
| 87 | + throttle.reset! |
| 88 | + |
| 89 | + second_call = throttle.call("prd") |
| 90 | + |
| 91 | + second_call.should eq(new_config) |
| 92 | + wrapped_loader.times_called.should == 2 |
| 93 | + end |
| 94 | + |
| 95 | + it "delegates reset! to the wrapped_loader, when supported" do |
| 96 | + wrapped_loader = TestConfigLoader.new |
| 97 | + throttle = Throttled.new(wrapped_loader) |
| 98 | + |
| 99 | + wrapped_loader.times_reset.should == 0 |
| 100 | + throttle.reset! |
| 101 | + wrapped_loader.times_reset.should == 1 |
| 102 | + end |
| 103 | + |
| 104 | + it "does not delegate reset! to the wrapped_loader when it is not supported" do |
| 105 | + wrapped_loader = lambda {|env| Hash.new } |
| 106 | + throttle = Throttled.new(wrapped_loader) |
| 107 | + |
| 108 | + expect { |
| 109 | + throttle.reset! |
| 110 | + }.to_not raise_error |
| 111 | + end |
| 112 | + |
| 113 | + class TestConfigLoader |
| 114 | + attr_accessor :configuration |
| 115 | + attr_reader :times_called |
| 116 | + attr_reader :times_reset |
| 117 | + |
| 118 | + def initialize |
| 119 | + @times_called = 0 |
| 120 | + @times_reset = 0 |
| 121 | + end |
| 122 | + |
| 123 | + def call(env) |
| 124 | + @times_called += 1 |
| 125 | + configuration |
| 126 | + end |
| 127 | + |
| 128 | + def reset! |
| 129 | + @times_reset += 1 |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + class FakeTime |
| 134 | + attr_reader :now |
| 135 | + |
| 136 | + def initialize(start_time) |
| 137 | + @now = start_time |
| 138 | + end |
| 139 | + |
| 140 | + def advance_time(seconds) |
| 141 | + @now += seconds |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + end |
| 146 | + |
| 147 | +end |
0 commit comments