Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #116 from NickolasVashchenko/em_shutdown_bugfix
Browse files Browse the repository at this point in the history
Several bugfixes for improper closing of websocket and eventmachine
  • Loading branch information
jimmycuadra authored Feb 9, 2017
2 parents 7af55c2 + 16ee5d2 commit 8c02e9f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/lita/adapters/slack/event_loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def run
end

def safe_stop
EM.stop if EM.reactor_running?
EM.stop if running?
end

def running?
EM.reactor_running? && !EM.stopping?
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/lita/adapters/slack/rtm_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run(queue = nil, options = {})
websocket.on(:message) { |event| receive_message(event) }
websocket.on(:close) do
log.info("Disconnected from Slack.")
shut_down
EventLoop.safe_stop
end
websocket.on(:error) { |event| log.debug("WebSocket error: #{event.message}") }

Expand All @@ -64,7 +64,7 @@ def send_messages(channel, strings)
end

def shut_down
if websocket
if websocket && EventLoop.running?
log.debug("Closing connection to the Slack Real Time Messaging API.")
websocket.close
end
Expand Down Expand Up @@ -115,6 +115,7 @@ def websocket_options
options[:proxy] = { :origin => config.proxy } if config.proxy
options
end

end
end
end
Expand Down
12 changes: 11 additions & 1 deletion spec/lita/adapters/slack/rtm_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def with_websocket(subject, queue)
end
let(:token) { 'abcd-1234567890-hWYd21AmMH2UHAkx29vb5c1Y' }
let(:queue) { Queue.new }
let(:proxy_url) { "http://foo:3128" }
let(:proxy_url) { "http://example.com:3128" }
let(:config) { Lita::Adapters::Slack.configuration_builder.build }

before do
Expand Down Expand Up @@ -114,6 +114,16 @@ def with_websocket(subject, queue)
# the WebSocket.
subject.send(:receive_message, event)
end

context "when the WebSocket is closed from outside" do
it "shuts down the reactor" do
with_websocket(subject, queue) do |websocket|
websocket.close
expect(EM.stopping?).to be_truthy
end
end
end

end

describe "#send_messages" do
Expand Down

0 comments on commit 8c02e9f

Please sign in to comment.