diff --git a/lib/lita/adapters/slack/event_loop.rb b/lib/lita/adapters/slack/event_loop.rb index b78376a..8404234 100644 --- a/lib/lita/adapters/slack/event_loop.rb +++ b/lib/lita/adapters/slack/event_loop.rb @@ -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 diff --git a/lib/lita/adapters/slack/rtm_connection.rb b/lib/lita/adapters/slack/rtm_connection.rb index 0d416e0..90bae4d 100644 --- a/lib/lita/adapters/slack/rtm_connection.rb +++ b/lib/lita/adapters/slack/rtm_connection.rb @@ -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}") } @@ -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 @@ -115,6 +115,7 @@ def websocket_options options[:proxy] = { :origin => config.proxy } if config.proxy options end + end end end diff --git a/spec/lita/adapters/slack/rtm_connection_spec.rb b/spec/lita/adapters/slack/rtm_connection_spec.rb index d733a80..32188f3 100644 --- a/spec/lita/adapters/slack/rtm_connection_spec.rb +++ b/spec/lita/adapters/slack/rtm_connection_spec.rb @@ -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 @@ -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