spec/acceptance/realtime/connection_failures_spec.rb in ably-0.8.4 vs spec/acceptance/realtime/connection_failures_spec.rb in ably-0.8.5

- old
+ new

@@ -9,11 +9,11 @@ { key: api_key, environment: environment, protocol: protocol } end let(:client_options) { default_options } let(:client) do - Ably::Realtime::Client.new(client_options) + auto_close Ably::Realtime::Client.new(client_options) end context 'authentication failure' do let(:client_options) do default_options.merge(key: invalid_key, log_level: :none) @@ -279,11 +279,11 @@ context 'connection resume' do let(:channel_name) { random_str } let(:channel) { client.channel(channel_name) } let(:publishing_client) do - Ably::Realtime::Client.new(client_options) + auto_close Ably::Realtime::Client.new(client_options) end let(:publishing_client_channel) { publishing_client.channel(channel_name) } let(:client_options) { default_options.merge(log_level: :none) } def fail_if_suspended_or_failed @@ -390,11 +390,14 @@ previous_connection_id = connection.id previous_connection_key = connection.key connection.transport.close_connection_after_writing connection.once(:connected) do - expect(connection.key).to eql(previous_connection_key) + # Connection key left part should match new connection key left part i.e. + # wVIsgTHAB1UvXh7z-1991d8586 becomes wVIsgTHAB1UvXh7z-1990d8586 after resume + expect(connection.key[/^\w{5,}-/, 0]).to_not be_nil + expect(connection.key[/^\w{5,}-/, 0]).to eql(previous_connection_key[/^\w{5,}-/, 0]) expect(connection.id).to eql(previous_connection_id) stop_reactor end end end @@ -463,13 +466,15 @@ end end context 'when failing to resume' do context 'because the connection_key is not or no longer valid' do + let(:channel) { client.channel(random_str) } + def kill_connection_transport_and_prevent_valid_resume connection.transport.close_connection_after_writing - connection.configure_new '0123456789abcdef', '0123456789abcdef', -1 # force the resume connection key to be invalid + connection.configure_new '0123456789abcdef', 'wVIsgTHAB1UvXh7z-1991d8586', -1 # force the resume connection key to be invalid end it 'updates the connection_id and connection_key' do connection.once(:connected) do previous_connection_id = connection.id @@ -491,11 +496,11 @@ when_all(*channels.map(&:attach)) do detached_channels = [] channels.each do |channel| channel.on(:detached) do |channel_state_change| error = channel_state_change.reason - expect(error.message).to match(/Invalid connection key/i) + expect(error.message).to match(/Unable to recover connection/i) detached_channels << channel next unless detached_channels.count == channel_count expect(detached_channels.count).to eql(channel_count) stop_reactor end @@ -504,20 +509,20 @@ kill_connection_transport_and_prevent_valid_resume end end it 'emits an error on the channel and sets the error reason' do - client.channel(random_str).attach do |channel| - channel.on(:error) do |error| - expect(error.message).to match(/Invalid connection key/i) - expect(error.code).to eql(80008) - expect(channel.error_reason).to eql(error) - stop_reactor - end - + channel.attach do kill_connection_transport_and_prevent_valid_resume end + + channel.on(:error) do |error| + expect(error.message).to match(/Unable to recover connection/i) + expect(error.code).to eql(80008) + expect(channel.error_reason).to eql(error) + stop_reactor + end end end end end @@ -622,21 +627,22 @@ allow(connection).to receive(:internet_up?).and_yield(true) end it 'uses a fallback host on every subsequent disconnected attempt until suspended' do request = 0 - expect(EventMachine).to receive(:connect).exactly(retry_count_for_one_state).times do |host| + # Expect retry attempts + 1 attempt for the next state + expect(EventMachine).to receive(:connect).exactly(retry_count_for_one_state + 1).times do |host| if request == 0 expect(host).to eql(expected_host) else - expect(custom_hosts).to include(host) fallback_hosts_used << host end request += 1 raise EventMachine::ConnectionError end connection.on(:suspended) do + fallback_hosts_used.pop # remove suspended attempt host expect(fallback_hosts_used.uniq).to match_array(custom_hosts) stop_reactor end end