Skip to content

Commit

Permalink
Merge pull request #143 from emschwar/shutdown_workqueue_on_close
Browse files Browse the repository at this point in the history
Shutdown workqueue on close
  • Loading branch information
benlangfeld committed Aug 18, 2014
2 parents 1a057cf + 11ae27f commit 1f0fbbb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/blather/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ def write_with_handler(stanza, &handler)

# Close the connection
def close
EM.next_tick { self.stream.close_connection_after_writing }
EM.next_tick {
handler_queue.shutdown if handler_queue
@handler_queue = nil
self.stream.close_connection_after_writing if connected?
}
end

# @private
Expand Down
57 changes: 51 additions & 6 deletions spec/blather/client/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,57 @@
end
end

it 'writes to the connection the closes when #close is called' do
stream.expects(:close_connection_after_writing)
EM.stubs(:next_tick).yields
subject.setup 'me.com', 'secret'
subject.post_init stream, Blather::JID.new('me.com')
subject.close
describe '#close' do
before do
EM.stubs(:next_tick).yields
subject.setup 'me.com', 'secret'
end

context "without a setup stream" do
it "does not close the connection" do
stream.expects(:close_connection_after_writing).never
subject.close
end
end

context "when a stream is setup" do
let(:stream_stopped) { false }
before do
subject.post_init stream, Blather::JID.new('me.com')
stream.stubs(:stopped? => stream_stopped)
end

context "when the stream is stopped" do
let(:stream_stopped) { true }

it "does not close the connection, since it's already closed" do
stream.expects(:close_connection_after_writing).never
end
end

it 'writes to the connection the closes when #close is called' do
stream.expects(:close_connection_after_writing)
subject.close
end

it 'shuts down the workqueue' do
stream.stubs(:close_connection_after_writing)
subject.handler_queue.expects(:shutdown)
subject.close
end

it 'forces the work queue to be re-created when referenced' do
stream.stubs(:close_connection_after_writing)
subject.close

fake_queue = stub('GirlFriday::WorkQueue')
GirlFriday::WorkQueue.expects(:new)
.with(:handle_stanza, :size => subject.queue_size)
.returns(fake_queue)

subject.handler_queue.should == fake_queue
end
end
end

it 'shuts down EM when #unbind is called if it is running' do
Expand Down

0 comments on commit 1f0fbbb

Please sign in to comment.