From b0bce0b8ce807e4d92a8a01fa0235727d709c21d Mon Sep 17 00:00:00 2001 From: Blake Mesdag Date: Tue, 12 Apr 2016 12:07:56 -0400 Subject: Handle max_time edge cases for epoch times and add test --- activesupport/lib/active_support/file_update_checker.rb | 15 ++++++++++++--- activesupport/test/file_update_checker_shared_tests.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 4f0a2dedc5..d1eb410c85 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -111,13 +111,22 @@ module ActiveSupport # healthy to consider this edge case because with mtimes in the future # reloading is not triggered. def max_mtime(paths) + return nil if paths.empty? + time_now = Time.now - time_at_zero = Time.at(0) - max_time = time_at_zero + max_time = nil paths.each do |path| time = File.mtime(path) + if max_time.nil? + if time.compare_without_coercion(time_now) < 0 + max_time = time + end + + next + end + # This avoids ActiveSupport::CoreExt::Time#time_with_coercion # which is super slow when comparing two Time objects # @@ -128,7 +137,7 @@ module ActiveSupport end end - max_time.object_id == time_at_zero.object_id ? nil : max_time + max_time end def compile_glob(hash) diff --git a/activesupport/test/file_update_checker_shared_tests.rb b/activesupport/test/file_update_checker_shared_tests.rb index 5207860a0e..a78c3a67b6 100644 --- a/activesupport/test/file_update_checker_shared_tests.rb +++ b/activesupport/test/file_update_checker_shared_tests.rb @@ -134,6 +134,23 @@ module FileUpdateCheckerSharedTests assert_equal 1, i end + test 'should return max_time for files with mtime = Time.at(0)' do + i = 0 + + FileUtils.touch(tmpfiles) + + now = Time.now + time = Time.at(0) # wrong mtime from the future + File.utime(time, time, tmpfiles[0]) + + checker = new_checker(tmpfiles) { i += 1 } + + touch(tmpfiles[1..-1]) + + assert checker.execute_if_updated + assert_equal 1, i + end + test 'should cache updated result until execute' do i = 0 -- cgit v1.2.3 From fad2428e357f5e88e745aa402e21cdfcb700fbc0 Mon Sep 17 00:00:00 2001 From: Blake Mesdag Date: Tue, 12 Apr 2016 13:29:56 -0400 Subject: More readable version --- .../lib/active_support/file_update_checker.rb | 23 ++++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index d1eb410c85..2a1a8f9797 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -114,30 +114,27 @@ module ActiveSupport return nil if paths.empty? time_now = Time.now - max_time = nil + max_mtime = nil paths.each do |path| - time = File.mtime(path) + mtime = File.mtime(path) - if max_time.nil? - if time.compare_without_coercion(time_now) < 0 - max_time = time - end - - next - end + # Prevent dates in the future being considered + # Equivalent ruby: + # time.now < mtime + next if time_now.compare_without_coercion(mtime) < 0 # This avoids ActiveSupport::CoreExt::Time#time_with_coercion # which is super slow when comparing two Time objects # # Equivalent Ruby: - # time < time_now && time > max_time - if time.compare_without_coercion(time_now) < 0 && time.compare_without_coercion(max_time) > 0 - max_time = time + # max_mtime.nil? || max_mtime < mtime + if max_mtime.nil? || max_mtime.compare_without_coercion(mtime) < 0 + max_mtime = mtime end end - max_time + max_mtime end def compile_glob(hash) -- cgit v1.2.3 From f05704fc1aec5f859c78a75d26dc694c51a729af Mon Sep 17 00:00:00 2001 From: Blake Mesdag Date: Tue, 12 Apr 2016 13:49:41 -0400 Subject: No more need for an early return --- activesupport/lib/active_support/file_update_checker.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 2a1a8f9797..fa0b1a4bcf 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -111,8 +111,6 @@ module ActiveSupport # healthy to consider this edge case because with mtimes in the future # reloading is not triggered. def max_mtime(paths) - return nil if paths.empty? - time_now = Time.now max_mtime = nil -- cgit v1.2.3