Skip to content

Commit

Permalink
Merge pull request #383 from guard/fix_state_transition
Browse files Browse the repository at this point in the history
allow stopping when not fully initialized
  • Loading branch information
e2 committed Apr 30, 2016
2 parents 0db811a + 9b2d077 commit 8368b87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/listen/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

module Listen
class Listener
# TODO: move the state machine's methods private
include Listen::FSM

# Initializes the directories listener.
Expand Down Expand Up @@ -63,11 +64,11 @@ def initialize(*dirs, &block)

state :initializing, to: :backend_started

state :backend_started, to: [:frontend_ready] do
state :backend_started, to: [:frontend_ready, :stopped] do
backend.start
end

state :frontend_ready, to: [:processing_events] do
state :frontend_ready, to: [:processing_events, :stopped] do
processor.setup
end

Expand Down
53 changes: 35 additions & 18 deletions spec/lib/listen/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,43 @@
allow(backend).to receive(:start)
allow(processor).to receive(:setup)
allow(processor).to receive(:resume)
subject.start
end

it 'terminates' do
allow(backend).to receive(:stop)
allow(processor).to receive(:teardown)
subject.stop
context 'when fully started' do
before do
subject.start
end

it 'terminates' do
allow(backend).to receive(:stop)
allow(processor).to receive(:teardown)
subject.stop
end
end

context 'when frontend is ready' do
before do
subject.transition :backend_started
subject.transition :frontend_ready
end

it 'terminates' do
allow(backend).to receive(:stop)
allow(processor).to receive(:teardown)
subject.stop
end
end

context 'when only backend is already started' do
before do
subject.transition :backend_started
end

it 'terminates' do
allow(backend).to receive(:stop)
allow(processor).to receive(:teardown)
subject.stop
end
end
end

Expand Down Expand Up @@ -307,17 +337,4 @@
end
end
end

describe 'processing changes' do
before do
allow(backend).to receive(:start)
end
end

context 'when listener is stopped' do
before do
subject.stop
allow(silencer).to receive(:silenced?) { true }
end
end
end

0 comments on commit 8368b87

Please sign in to comment.