Skip to content

Watcher: No need to keep polling mtimes if already stale #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/spring/watcher/polling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def start
Thread.current.abort_on_exception = true

begin
loop do
until stale?
Kernel.sleep latency
check_stale
end
Expand All @@ -42,6 +42,8 @@ def start
"poller: aborted: #{e.class}: #{e}\n #{e.backtrace.join("\n ")}"
end
raise
ensure
@poller = nil
end
}
end
Expand All @@ -55,6 +57,10 @@ def stop
end
end

def running?
@poller && @poller.alive?
end

def subjects_changed
computed = compute_mtime
debug { "subjects_changed: mtime #{@mtime} -> #{computed}" }
Expand Down
51 changes: 51 additions & 0 deletions test/unit/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,55 @@ class PollingWatcherTest < Spring::Test::WatcherTest
def watcher_class
Spring::Watcher::Polling
end

test "skips staleness checks if already stale" do
class << watcher
attr_reader :checked_when_stale_count
attr_reader :checked_when_not_stale_count

def check_stale
@checked_when_stale_count = 0 unless defined? @checked_when_stale_count
@checked_when_not_stale_count = 0 unless defined? @checked_when_not_stale_count

if stale?
@checked_when_stale_count += 1
else
@checked_when_not_stale_count += 1
end

super
end

# Wait for the poller thread to finish.
def join
@poller.join if @poller
end
end

# Track when we're marked as stale.
on_stale_count = 0
watcher.on_stale { on_stale_count += 1 }

# Add a file to watch and start polling.
file = "#{@dir}/omg"
touch file, Time.now - 2.seconds
watcher.add file
watcher.start
assert watcher.running?

# First touch bumps mtime and marks as stale.
touch file, Time.now - 1.second
Timeout.timeout(1) { watcher.join }
assert !watcher.running?
assert_equal 0, watcher.checked_when_stale_count
assert_equal 1, watcher.checked_when_not_stale_count
assert_equal 1, on_stale_count

# Second touch skips mtime check because it's already stale.
touch file, Time.now
sleep 1
assert_equal 0, watcher.checked_when_stale_count
assert_equal 1, watcher.checked_when_not_stale_count
assert_equal 1, on_stale_count
end
end