spec/acceptance/realtime/connection_spec.rb in ably-0.7.0 vs spec/acceptance/realtime/connection_spec.rb in ably-0.7.1

- old
+ new

@@ -91,11 +91,11 @@ let(:ttl) { 2 } it 'renews the token on connect' do sleep ttl + 0.1 expect(client.auth.current_token).to be_expired - expect(client.auth).to receive(:authorise).once.and_call_original + expect(client.auth).to receive(:authorise).at_least(:once).and_call_original connection.once(:connected) do expect(client.auth.current_token).to_not be_expired stop_reactor end end @@ -103,11 +103,11 @@ context 'with immediately expiring token' do let(:ttl) { 0.01 } it 'renews the token on connect, and only makes one subsequent attempt to obtain a new token' do - expect(client.auth).to receive(:authorise).twice.and_call_original + expect(client.auth).to receive(:authorise).at_least(:twice).and_call_original connection.once(:disconnected) do connection.once(:failed) do |error| expect(error.code).to eql(40140) # token expired stop_reactor end @@ -115,11 +115,14 @@ end it 'uses the primary host for subsequent connection and auth requests' do EventMachine.add_timer(1) do # wait for token to expire connection.once(:disconnected) do - expect(client.rest_client.connection).to receive(:post).with(/requestToken$/, anything).and_call_original + expect(client.rest_client.connection).to receive(:post). + with(/requestToken$/, anything). + at_least(:once). + and_call_original expect(client.rest_client).to_not receive(:fallback_connection) expect(client).to_not receive(:fallback_endpoint) connection.once(:failed) do @@ -137,27 +140,29 @@ let(:ttl) { 2 } let(:channel) { client.channel('test') } context 'the server' do it 'disconnects the client, and the client automatically renews the token and then reconnects', em_timeout: 10 do - expect(client.auth.current_token).to_not be_expired - - channel.attach original_token = client.auth.current_token + expect(original_token).to_not be_expired connection.once(:connected) do started_at = Time.now connection.once(:disconnected) do |error| - expect(Time.now - started_at >= ttl) - expect(original_token).to be_expired - expect(error.code).to eql(40140) # token expired - connection.once(:connected) do - expect(client.auth.current_token).to_not be_expired - stop_reactor + EventMachine.add_timer(1) do # allow 1 second + expect(Time.now - started_at >= ttl) + expect(original_token).to be_expired + expect(error.code).to eql(40140) # token expired + connection.once(:connected) do + expect(client.auth.current_token).to_not be_expired + stop_reactor + end end end end + + channel.attach end end skip 'retains connection state' skip 'changes state to failed if a new token cannot be issued' @@ -599,17 +604,32 @@ expect { Ably::Realtime::Client.new(invaid_client_options) }.to raise_error ArgumentError, /Recover/ stop_reactor end end - context 'with invalid value' do - let(:client_options) { default_options.merge(recover: 'invalid:key', log_level: :fatal) } + context 'with invalid formatted value sent to server' do + let(:client_options) { default_options.merge(recover: 'not-a-valid-connection-key:1', log_level: :none) } - skip 'triggers an error on the connection object, sets the #error_reason and connects anyway' do - connection.on(:error) do |error| + it 'triggers a fatal error on the connection object, sets the #error_reason and disconnects' do + connection.once(:error) do |error| + expect(connection.state).to eq(:failed) + expect(connection.error_reason.message).to match(/Invalid connection key/) + expect(connection.error_reason.code).to eql(40006) + expect(connection.error_reason).to eql(error) + stop_reactor + end + end + end + + context 'with expired (missing) value sent to server' do + let(:client_options) { default_options.merge(recover: '0123456789abcdef:0', log_level: :fatal) } + + it 'triggers an error on the connection object, sets the #error_reason, yet will connect anyway' do + connection.once(:error) do |error| expect(connection.state).to eq(:connected) - expect(connection.error_reason.message).to match(/Recover/) + expect(connection.error_reason.message).to match(/Invalid connection key/i) + expect(connection.error_reason.code).to eql(80008) expect(connection.error_reason).to eql(error) stop_reactor end end end @@ -647,9 +667,53 @@ end connection.on(:error) do |error| expect(error).to be_a(Ably::Exceptions::StateChangeError) stop_reactor + end + end + end + + + context 'undocumented method' do + context '#internet_up?' do + it 'returns a Deferrable' do + expect(connection.internet_up?).to be_a(EventMachine::Deferrable) + stop_reactor + end + + context 'when the Internet is up' do + it 'calls the block with true' do + connection.internet_up? do |result| + expect(result).to be_truthy + stop_reactor + end + end + + it 'calls the success callback of the Deferrable' do + connection.internet_up?.callback do + stop_reactor + end + end + end + + context 'when the Internet is down' do + before do + stub_const 'Ably::INTERNET_CHECK', { url: 'http://does.not.exist.com', ok_text: 'no.way.this.will.match' } + end + + it 'calls the block with false' do + connection.internet_up? do |result| + expect(result).to be_falsey + stop_reactor + end + end + + it 'calls the failure callback of the Deferrable' do + connection.internet_up?.errback do + stop_reactor + end + end end end end end end