Skip to content

Commit

Permalink
issue guard#510: switch to Listen::MonotonicTime.new except for :mtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Dec 31, 2020
1 parent 366bfa6 commit be30e02
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/listen/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def _stop
end

def _timed(title)
start = Time.now.to_f
start = MonotonicTime.now
yield
diff = Time.now.to_f - start
diff = MonotonicTime.now - start
Listen.logger.info format('%s: %.05f seconds', title, diff)
rescue
Listen.logger.warn "#{title} crashed: #{$ERROR_INFO.inspect}"
Expand Down
9 changes: 5 additions & 4 deletions lib/listen/adapter/polling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def _configure(_, &callback)

def _run
loop do
start = Time.now.to_f
start = MonotonicTime.now
@polling_callbacks.each do |callback|
callback.call(nil)
nap_time = options.latency - (Time.now.to_f - start)
# TODO: warn if nap_time is negative (polling too slow)
sleep(nap_time) if nap_time > 0
if (nap_time = options.latency - (MonotonicTime.now - start)) > 0
# TODO: warn if nap_time is negative (polling too slow)
sleep(nap_time)
end
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/listen/event/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def call(*args)
@block&.call(*args)
end

def timestamp
Time.now.to_f
end

def callable?
@block
end
Expand Down
16 changes: 7 additions & 9 deletions lib/listen/event/processor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'listen/monotonic_time'

module Listen
module Event
class Processor
Expand Down Expand Up @@ -33,7 +35,7 @@ class Stopped < RuntimeError

def _wait_until_events_calm_down
loop do
now = _timestamp
now = MonotonicTime.now

# Assure there's at least latency between callbacks to allow
# for accumulating changes
Expand Down Expand Up @@ -70,7 +72,7 @@ def _sleep(seconds)
end

def _remember_time_of_first_unprocessed_event
@_remember_time_of_first_unprocessed_event ||= _timestamp
@_remember_time_of_first_unprocessed_event ||= MonotonicTime.now
end

def _reset_no_unprocessed_events
Expand All @@ -85,7 +87,7 @@ def _deadline
# returns the event or `nil` when the event_queue is closed
def _wait_until_events
config.event_queue.pop.tap do |_event|
@_remember_time_of_first_unprocessed_event ||= _timestamp
@_remember_time_of_first_unprocessed_event ||= MonotonicTime.now
end
end

Expand All @@ -96,10 +98,6 @@ def _flush_wakeup_reasons
end
end

def _timestamp
config.timestamp
end

# for easier testing without sleep loop
def _process_changes(event)
_reset_no_unprocessed_events
Expand All @@ -113,13 +111,13 @@ def _process_changes(event)
result = [hash[:modified], hash[:added], hash[:removed]]
return if result.all?(&:empty?)

block_start = _timestamp
block_start = MonotonicTime.now
exception_note = " (exception)"
::Listen::Thread.rescue_and_log('_process_changes') do
config.call(*result)
exception_note = nil
end
Listen.logger.debug "Callback#{exception_note} took #{_timestamp - block_start} sec"
Listen.logger.debug "Callback#{exception_note} took #{MonotonicTime.now - block_start} sec"
end

attr_reader :config
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/listen/event/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

let(:state) do
{ time: 0 }
{ time: 0.0 }
end

def status_for_time(time)
Expand All @@ -39,7 +39,7 @@ def status_for_time(time)
status_for_time(state[:time]) == :paused
end

allow(config).to receive(:timestamp) do
allow(Time).to receive(:now) do
state[:time]
end
end
Expand Down Expand Up @@ -67,9 +67,9 @@ def status_for_time(time)
it 'does not sleep' do
expect(config).to_not receive(:sleep)
expect(event_queue).to receive(:pop).and_return(event)
t = Time.now.to_f
t = Listen::MonotonicTime.now
subject.loop_for(1)
diff = Time.now.to_f - t
diff = Listen::MonotonicTime.now - t
expect(diff).to be < 0.02
end
end
Expand Down

0 comments on commit be30e02

Please sign in to comment.