From c0aa49bc0e3bb6c1edc05a414d851be4b99a0b6b Mon Sep 17 00:00:00 2001 From: dblock Date: Sun, 30 Apr 2017 11:27:31 -0400 Subject: [PATCH] Fix #146: ThreadError: Target thread must not be current thread and undefined method running?. --- .rubocop_todo.yml | 8 ++++- CHANGELOG.md | 1 + lib/slack/real_time/concurrency/celluloid.rb | 6 ++-- .../real_time/concurrency/celluloid_spec.rb | 31 ++++++++++++++++--- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4ce2ba03..4e995552 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-04-28 18:14:51 -0400 using RuboCop version 0.35.0. +# on 2017-04-30 12:02:22 -0400 using RuboCop version 0.35.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -80,6 +80,12 @@ Style/MultilineTernaryOperator: Exclude: - 'spec/support/real_time/connected_client.rb' +# Offense count: 1 +# Cop supports --auto-correct. +Style/RescueModifier: + Exclude: + - 'lib/slack/real_time/concurrency/celluloid.rb' + # Offense count: 2 # Cop supports --auto-correct. Style/SpecialGlobalVars: diff --git a/CHANGELOG.md b/CHANGELOG.md index 817e2481..5f59f8f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### 0.9.0 (Next) +* [#146](https://github.com/slack-ruby/slack-ruby-client/issues/146): Fix: `undefined method running?` and `ThreadError: Target thread must not be current thread` with `Celluloid::IO` - [@dblock](https://github.com/dblock). * [#145](https://github.com/slack-ruby/slack-ruby-client/pull/145): Automatically select `rtm_connect` vs. `rtm_start` - [@dblock](https://github.com/dblock). * Your contribution here. diff --git a/lib/slack/real_time/concurrency/celluloid.rb b/lib/slack/real_time/concurrency/celluloid.rb index 672fe2e2..d5ddb57c 100644 --- a/lib/slack/real_time/concurrency/celluloid.rb +++ b/lib/slack/real_time/concurrency/celluloid.rb @@ -38,7 +38,7 @@ def run_loop logger.debug("#{self.class}##{__method__}") { e } driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'server closed connection')) unless @closing ensure - current_actor.terminate if current_actor.alive? && current_actor.running? + current_actor.terminate if current_actor.alive? rescue nil end def close @@ -90,8 +90,8 @@ def join end def build_socket - socket = TCPSocket.new(addr, port) - socket = SSLSocket.new(socket, build_ssl_context) if secure? + socket = ::Celluloid::IO::TCPSocket.new(addr, port) + socket = ::Celluloid::IO::SSLSocket.new(socket, build_ssl_context) if secure? socket end diff --git a/spec/slack/real_time/concurrency/celluloid_spec.rb b/spec/slack/real_time/concurrency/celluloid_spec.rb index fd97bc71..51d7aa56 100644 --- a/spec/slack/real_time/concurrency/celluloid_spec.rb +++ b/spec/slack/real_time/concurrency/celluloid_spec.rb @@ -7,20 +7,41 @@ context 'with url' do let(:url) { 'wss://echo.websocket.org/websocket/xyz' } let(:logger) { ::Logger.new(STDOUT) } - subject(:socket) { described_class.new(url, ping: 42, logger: logger) } + let(:test_socket) do + Class.new(described_class) do + def read + fail EOFError + end + end + end + let(:socket) { test_socket.new(url, ping: 42, logger: logger) } let(:driver) { WebSocket::Driver::Client } let(:ws) { double(driver) } + subject { socket } describe '#connect!' do pending 'connects' pending 'pings every 30s' end - describe '#disconnect!' do - it 'closes and nils the websocket' do + context 'with a driver' do + before do socket.instance_variable_set('@driver', ws) - expect(ws).to receive(:close) - socket.disconnect! + end + + describe '#disconnect!' do + it 'closes and nils the websocket' do + expect(ws).to receive(:close) + socket.disconnect! + end + end + + describe '#run_loop' do + it 'runs' do + expect(ws).to receive(:emit) + expect(ws).to receive(:start) + socket.run_loop + end end end