Skip to content

Commit

Permalink
issue #504: address many rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Nov 23, 2020
1 parent ab870f5 commit 870e8f0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 39 deletions.
8 changes: 4 additions & 4 deletions lib/listen/event/processor.rb
Expand Up @@ -70,22 +70,22 @@ def _sleep(seconds)
end

def _remember_time_of_first_unprocessed_event
@first_unprocessed_event_time ||= _timestamp
@_remember_time_of_first_unprocessed_event ||= _timestamp
end

def _reset_no_unprocessed_events
@first_unprocessed_event_time = nil
@_remember_time_of_first_unprocessed_event = nil
end

def _deadline
@first_unprocessed_event_time + @latency
@_remember_time_of_first_unprocessed_event + @latency
end

# blocks until event is popped
# returns the event or `nil` when the event_queue is closed
def _wait_until_events
config.event_queue.pop.tap do |_event|
@first_unprocessed_event_time ||= _timestamp
@_remember_time_of_first_unprocessed_event ||= _timestamp
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/listen/thread.rb
Expand Up @@ -38,12 +38,12 @@ def _log_exception(ex, thread_name, caller_stack: nil)
Listen.logger.error(message)
end

def _exception_with_causes(ex)
result = +"#{ex.class}: #{ex}"
if ex.cause
def _exception_with_causes(exception)
result = +"#{exception.class}: #{exception}"
if exception.cause
result << "\n"
result << "--- Caused by: ---\n"
result << _exception_with_causes(ex.cause)
result << _exception_with_causes(exception.cause)
end
result
end
Expand Down
18 changes: 9 additions & 9 deletions spec/acceptance/listen_spec.rb
Expand Up @@ -18,15 +18,15 @@
let(:paths) { Pathname.new(Dir.pwd) }
around { |example| fixtures { example.run } }

modes =
case ENV['TEST_LISTEN_ADAPTER_MODES'] || 'both'
when 'polling'
[true]
when 'native'
[false]
else
[false, true]
end
modes =
case ENV['TEST_LISTEN_ADAPTER_MODES']
when 'polling'
[true]
when 'native'
[false]
else
[false, true]
end

# TODO: make it configurable
# TODO: restore
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/listen/directory_spec.rb
Expand Up @@ -13,14 +13,14 @@ def fake_dir_stat(name, options = {})
instance_double(::File::Stat, name, defaults.merge(options))
end

def fake_children(ex, dir, *args, &block)
def fake_children(exception, dir, *args, &block)
if block_given?
ex.send(:allow, dir).to receive(:children, &block)
exception.send(:allow, dir).to receive(:children, &block)
else
ex.send(:allow, dir).to receive(:children).and_return(*args)
exception.send(:allow, dir).to receive(:children).and_return(*args)
end
ex.send(:allow, dir).to receive(:exist?).and_return(true)
ex.send(:allow, dir).to receive(:directory?).and_return(true)
exception.send(:allow, dir).to receive(:exist?).and_return(true)
exception.send(:allow, dir).to receive(:directory?).and_return(true)
end

let(:dir) { double(:dir) }
Expand Down
51 changes: 35 additions & 16 deletions spec/lib/listen/thread_spec.rb
Expand Up @@ -42,25 +42,39 @@
-> { raise ArgumentError, 'boom!' }
end

it "rescues and logs exceptions" do
expect(Listen.logger).to receive(:error).
with(/Exception rescued in listen-worker_thread:\nArgumentError: boom!\n.*\/listen\/thread_spec\.rb/)
subject.join
end
it "rescues and logs exceptions" do
pattern = <<~EOS.strip
Exception rescued in listen-worker_thread:
ArgumentError: boom!
.*\\/listen\\/thread_spec\\.rb
EOS
expect(Listen.logger).to receive(:error).with(/#{pattern}/)
subject.join
end

it "rescues and logs backtrace + exception backtrace" do
expect(Listen.logger).to receive(:error).
with(/Exception rescued in listen-worker_thread:\nArgumentError: boom!\n.*\/listen\/thread\.rb.*--- Thread.new ---.*\/listen\/thread_spec\.rb/m)
subject.join
end
it "rescues and logs backtrace + exception backtrace" do
pattern = <<~EOS.strip
Exception rescued in listen-worker_thread:
ArgumentError: boom!
.*\\/listen\\/thread\\.rb.*--- Thread.new ---.*\\/listen\\/thread_spec\\.rb
EOS
expect(Listen.logger).to receive(:error).with(/#{pattern}/m)
subject.join
end
end

context "when nested exceptions raised" do
let(:block) { raise_nested_exception_block }

it "details exception causes" do
expect(Listen.logger).to receive(:error).
with(/RuntimeError: nested outer\n--- Caused by: ---\nRuntimeError: nested inner\n--- Caused by: ---\nArgumentError: boom!/)
pattern = <<~EOS
RuntimeError: nested outer
--- Caused by: ---
RuntimeError: nested inner
--- Caused by: ---
ArgumentError: boom!
EOS
expect(Listen.logger).to receive(:error).with(/#{pattern}/)
subject.join
end
end
Expand All @@ -77,10 +91,15 @@

describe '.rescue_and_log' do
it 'rescues and logs nested exceptions' do
expect(Listen.logger).to receive(:error).
with(/Exception rescued in method:\nRuntimeError: nested outer\n--- Caused by: ---\nRuntimeError: nested inner\n--- Caused by: ---\nArgumentError: boom!/) do |message|
expect(message).to_not match(/Thread\.new/)
end
pattern = <<~EOS
Exception rescued in method:
RuntimeError: nested outer
--- Caused by: ---
RuntimeError: nested inner
--- Caused by: ---
ArgumentError: boom!
EOS
expect(Listen.logger).to receive(:error).with(/#{pattern}/)
described_class.rescue_and_log("method", &raise_nested_exception_block)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance_helper.rb
Expand Up @@ -143,7 +143,7 @@ def allow_changes(reset_queue = true)

# Conveniently wrap a Listener instance for testing
class ListenerWrapper
attr_reader :listener, :changes
attr_reader :listener
attr_accessor :lag

def initialize(callback, paths, *args)
Expand Down

0 comments on commit 870e8f0

Please sign in to comment.