From d67ea28693088e4d7039069b52541777eb7aa989 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Thu, 7 Jun 2012 15:26:53 +0100 Subject: Updated DuringRunner to cache calls to the calendar more. --- lib/mauve/notification.rb | 16 +++++--- test/tc_mauve_notification.rb | 96 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index 57d82d2..745660a 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -124,14 +124,20 @@ module Mauve def no_one_in(people_list) return true unless Configuration.current.people[people_list].respond_to?(:people) + @test_time = @time if @test_time.nil? + # - # Cache the results to prevent hitting the calendar too many times. + # This is cached by itself, since calendar calls are rounded to the + # nearest minute. However this cache expires with the during runner, + # which isn't so insane.. # - @no_one_in_cache ||= Hash.new + test_time = @test_time - @test_time.sec - return @no_one_in_cache[people_list] if @no_one_in_cache.has_key?(people_list) + @no_one_in_cache ||= Hash.new{|h,k| h[k] = Hash.new} - @no_one_in_cache[people_list] = Configuration.current.people[people_list].people(@time).empty? + return @no_one_in_cache[people_list][test_time] if @no_one_in_cache[people_list].has_key?(test_time) + + @no_one_in_cache[people_list][test_time] = Configuration.current.people[people_list].people(test_time).empty? end # Returns true if the current hour is in the list of hours given. @@ -248,7 +254,7 @@ module Mauve @during = nil @every = nil @level = nil - end + end # @return [String] def to_s diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb index f78ec27..b8fb904 100644 --- a/test/tc_mauve_notification.rb +++ b/test/tc_mauve_notification.rb @@ -214,25 +214,97 @@ EOF Configuration.current = ConfigurationBuilder.parse(config) - stub_request(:get, "http://localhost/api/attendees/support_shift/2011-08-01T00:00:00"). - to_return(:status => 200, :body => YAML.dump(%w(test1 test2))) + # +# +# 5.times.each do |hr| +# end - stub_request(:get, "http://localhost/api/attendees/support_shift/2011-08-01T00:05:00"). - to_return(:status => 200, :body => YAML.dump([])) - dr = DuringRunner.new(Time.now) - assert(!dr.send(:no_one_in, "support")) + dr = DuringRunner.new(Time.now, nil) { + no_one_in("support") + } + + # + # Stub the URL + # + url ="http://localhost/api/attendees/support_shift/#{Time.now.strftime("%Y-%m-%dT%H:%M:00")}" + stub_request(:get, url).to_return(:status => 200, :body => YAML.dump(%w(test1 test2))) + + assert(!dr.now?) - # advance by 5 minutes, and try again -- we should get the same answer. + # + # Advance by 5 minutes, and try again -- we should get the same answer, + # since the DuringRunner's time does not change. + # Timecop.freeze(Time.now + 5.minutes) - assert(!dr.send(:no_one_in, "support")) - - # However a new runner should return true. - dr = DuringRunner.new(Time.now) - assert(dr.send(:no_one_in, "support")) + assert(!dr.now?) + # + # Roll back 5 minutes. + # + Timecop.freeze(Time.now - 5.minutes) + + # + # That last round of requests should only have hit the midnight URL once. + # + assert_requested(:get, url, :times => 1) + WebMock.reset! + + # + # Set up a new runner. + # + dr = DuringRunner.new(Time.now, nil) { + no_one_in("support") + } + + # + # This time our URL returns an empty list. + # + stub_request(:get, url).to_return(:status => 200, :body => YAML.dump([])) + assert(dr.now?) + # # We expect a warning about an empty list. + # logger_pop + + # + # This should make a new request to the 00:05 URL + # + assert_requested(:get, url, :times => 1) + WebMock.reset! + + # + # Set up a new runner. + # + dr = DuringRunner.new(Time.now, nil) { + no_one_in("support") + } + + # + # Set up our stubs. The first is to catch any iterations done by find_next. + # + stub_request(:get, /http:\/\/localhost\/api\/attendees\/support_shift\/(.*)/). + to_return do |request| + t = Time.parse(request.uri.path.split("/").last) + if t >= Time.now and t < Time.now + 5.minutes + {:status => 200, :body => YAML.dump(%w(test1 test2))} + else + {:status => 200, :body => YAML.dump([])} + end + end + + assert(!dr.now?) + assert_equal(Time.now + 5.minutes, dr.find_next(0)) + logger_pop + logger_pop + + # + # This should make 5 requests, one in an hours time (the initial step) + + # + WebMock::RequestRegistry.instance.requested_signatures.each do |request, count| + assert_equal(1, count, "URI #{request.uri.to_s} requested more than once during find_next()") + end + end def test_bank_holiday -- cgit v1.2.1