Skip to content

Commit

Permalink
Adding unit tests and fixing CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodney Urquhart committed Oct 16, 2018
1 parent f4d4ea2 commit a69e212
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Your contribution here.
* [#226](https://github.com/slack-ruby/slack-ruby-client/pull/226): Added periodic ping that reconnects on failure. - [@RodneyU215](https://github.com/RodneyU215), [@dblock](https://github.com/dblock), [@ioquatix](https://github.com/ioquatix).


### 0.13.0 (2018/9/8)

* [#219](https://github.com/slack-ruby/slack-ruby-client/pull/219): Added support for `async-websocket` - [@dblock](https://github.com/dblock), [@ioquatix](https://github.com/ioquatix).
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def run_ping!
end
rescue Slack::RealTime::Client::ClientNotStartedError
@socket.restart_async(self)
retry
retry if started?
end
end

Expand Down
40 changes: 40 additions & 0 deletions spec/slack/real_time/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,46 @@
end
end
end
describe '#start_async' do
let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
before do
allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30, logger: Slack::Logger.default).and_return(socket)
allow(socket).to receive(:connect!)
allow(socket).to receive(:start_async)
client.start_async
end
describe '#run_ping' do
it 'sends ping messages when the connection is idle' do
allow(socket).to receive(:time_since_last_message).and_return(30)
expect(socket).to receive(:send_data).with('{"type":"ping","id":1}')
client.run_ping
end
it 'disconnects the websocket when the connection is idle for too long' do
allow(socket).to receive(:time_since_last_message).and_return(75)
allow(socket).to receive(:connected?).and_return(false)

expect(socket).to receive(:disconnect!)
expect(socket).to receive(:close)
expect { client.run_ping }.to raise_error Slack::RealTime::Client::ClientNotStartedError
end
end
describe '#run_ping!' do
it 'returns if websocket_ping is less than 1' do
client.websocket_ping = 0
expect(client).to_not receive(:run_ping)
client.run_ping!
end
it 'reconnects the websocket if an exception is thrown' do
allow(socket).to receive(:time_since_last_message).and_return(75)
allow(socket).to receive(:disconnect!)
allow(socket).to receive(:close)
allow(socket).to receive(:connected?).and_return(false)

expect(socket).to receive(:restart_async)
client.run_ping!
end
end
end
end
context 'client with starter store', vcr: { cassette_name: 'web/rtm_connect' } do
let(:client) { Slack::RealTime::Client.new(store_class: Slack::RealTime::Stores::Starter) }
Expand Down

0 comments on commit a69e212

Please sign in to comment.