Skip to content

Commit

Permalink
add tests for waiting max of 5 seconds for startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Jan 14, 2021
1 parent 5e0c64d commit 6fa5e2e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/listen/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Listen
class Error < RuntimeError
class NotStarted < Error; end
end
end
4 changes: 3 additions & 1 deletion lib/listen/event/loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
require 'timeout'
require 'listen/event/processor'
require 'listen/thread'
require 'listen/error'

module Listen
module Event
class Loop
include Listen::FSM

class Error < RuntimeError
class NotStarted < Error; end
NotStarted = ::Listen::Error::NotStarted # for backward compatibility
end

start_state :pre_start
Expand Down Expand Up @@ -40,6 +41,7 @@ def started?

MAX_STARTUP_SECONDS = 5.0

# @raises Error::NotStarted if background thread hasn't started in MAX_STARTUP_SECONDS
def start
# TODO: use a Fiber instead?
return unless state == :pre_start
Expand Down
28 changes: 21 additions & 7 deletions spec/lib/listen/event/loop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,40 @@
end

describe '#start' do
before do
it 'is started' do
expect(processor).to receive(:loop_for).with(1.234)
expect(Thread).to receive(:new) do |&block|
block.call
thread
end

expect(processor).to receive(:loop_for).with(1.234)

subject.start
end

it 'is started' do
expect(subject).to be_started
end

context 'when start is called again' do
it 'returns silently' do
expect(processor).to receive(:loop_for).with(1.234)
expect(Thread).to receive(:new) do |&block|
block.call
thread
end
subject.start
expect { subject.start }.to_not raise_exception
end
end

context 'when state change to :started takes longer than 5 seconds' do
before do
expect(Thread).to receive(:new) { thread }
expect_any_instance_of(::ConditionVariable).to receive(:wait) { } # return immediately
end

it 'raises Error::NotStarted' do
expect do
subject.start
end.to raise_exception(::Listen::Error::NotStarted, "thread didn't start in 5.0 seconds (in state: :starting)")
end
end
end

context 'when set up / started' do
Expand Down

0 comments on commit 6fa5e2e

Please sign in to comment.