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

- old
+ new

@@ -9,11 +9,11 @@ let(:default_options) do { key: api_key, environment: environment, protocol: protocol } end let(:client_options) { default_options } - let(:client) { Ably::Realtime::Client.new(client_options) } + let(:client) { auto_close Ably::Realtime::Client.new(client_options) } before(:example) do EventMachine.add_shutdown_hook do connection.off # minimise side effects of callbacks from finished test calling stop_reactor end @@ -27,11 +27,11 @@ end end context 'with :auto_connect option set to false' do let(:client) do - Ably::Realtime::Client.new(default_options.merge(auto_connect: false)) + auto_close Ably::Realtime::Client.new(default_options.merge(auto_connect: false)) end it 'does not connect automatically' do EventMachine.add_timer(1) do expect(connection).to be_initialized @@ -94,11 +94,11 @@ # Ensure tokens issued expire immediately after issue @original_renew_token_buffer = Ably::Auth::TOKEN_DEFAULTS.fetch(:renew_token_buffer) stub_const 'Ably::Auth::TOKEN_DEFAULTS', Ably::Auth::TOKEN_DEFAULTS.merge(renew_token_buffer: 0) # Authorise synchronously to ensure token has been issued - client.auth.authorise_sync(token_params: { ttl: ttl }) + client.auth.authorise_sync(ttl: ttl) end let(:original_renew_token_buffer) { @original_renew_token_buffer } context 'opening a new connection' do @@ -156,11 +156,11 @@ end context 'when connected with a valid non-expired token' do context 'that then expires following the connection being opened' do let(:ttl) { 5 } - let(:channel) { client.channel('test') } + let(:channel) { client.channel(random_str) } context 'the server' do it 'disconnects the client, and the client automatically renews the token and then reconnects', em_timeout: 15 do original_token = client.auth.current_token_details expect(original_token).to_not be_expired @@ -202,11 +202,12 @@ stub_const 'Ably::Auth::TOKEN_DEFAULTS', Ably::Auth::TOKEN_DEFAULTS.merge(renew_token_buffer: 0) end let!(:expired_token_details) do # Request a token synchronously - Ably::Realtime::Client.new(default_options).auth.request_token_sync(token_params: { ttl: 0.01 }) + token_client = auto_close Ably::Realtime::Client.new(default_options) + token_client.auth.request_token_sync(ttl: 0.01) end context 'opening a new connection' do let(:client_options) { default_options.merge(key: nil, token: expired_token_details.token, log_level: :none) } @@ -268,12 +269,11 @@ expect(connection.connect).to be_a(Ably::Util::SafeDeferrable) stop_reactor end it 'calls the Deferrable callback on success' do - connection.connect.callback do |connection| - expect(connection).to be_a(Ably::Realtime::Connection) + connection.connect.callback do expect(connection.state).to eq(:connected) stop_reactor end end @@ -304,11 +304,12 @@ stop_reactor end end describe 'once connected' do - let(:connection2) { Ably::Realtime::Client.new(client_options).connection } + let(:client2) { auto_close Ably::Realtime::Client.new(client_options) } + let(:connection2) { client2.connection } describe 'connection#id' do it 'is a string' do connection.connect do expect(connection.id).to be_a(String) @@ -434,11 +435,11 @@ end end it 'calls the Deferrable callback on success' do connection.connect do - connection.close.callback do |connection| + connection.close.callback do expect(connection).to be_a(Ably::Realtime::Connection) expect(connection.state).to eq(:closed) stop_reactor end end @@ -574,11 +575,11 @@ context 'recovery' 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: :fatal) } before do @@ -592,21 +593,22 @@ describe '#recovery_key' do def self.available_states [:connecting, :connected, :disconnected, :suspended, :failed] end - let(:available_states) { self.class.available_states} + let(:available_states) { self.class.available_states } let(:states) { Hash.new } let(:client_options) { default_options.merge(log_level: :none) } + let(:channel) { client.channel(random_str) } it 'is composed of connection key and serial that is kept up to date with each message ACK received' do connection.on(:connected) do expected_serial = -1 expect(connection.key).to_not be_nil expect(connection.serial).to eql(expected_serial) - client.channel('test').attach do |channel| + channel.attach do channel.publish('event', 'data') do expected_serial += 1 # attach message received expect(connection.serial).to eql(expected_serial) channel.publish('event', 'data') do @@ -668,13 +670,14 @@ previous_connection_key = connection.key connection.transition_state_machine! :failed end connection.once(:failed) do - recover_client = Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) + recover_client = auto_close Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) recover_client.connection.on(:connected) do - expect(recover_client.connection.key).to eql(previous_connection_key) + expect(recover_client.connection.key[/^\w{5,}-/, 0]).to_not be_nil + expect(recover_client.connection.key[/^\w{5,}-/, 0]).to eql(previous_connection_key[/^\w{5,}-/, 0]) expect(recover_client.connection.id).to eql(previous_connection_id) stop_reactor end end end @@ -683,11 +686,11 @@ connection.once(:connected) do connection.transition_state_machine! :failed end connection.once(:failed) do - recover_client = Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) + recover_client = auto_close Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) recover_client.connection.on_resume do raise 'Should not call the resume callback' end recover_client.connection.on(:connected) do EventMachine.add_timer(0.5) { stop_reactor } @@ -699,18 +702,19 @@ context 'when messages have been sent whilst the old connection is disconnected' do describe 'the new connection' do let(:client_options) { default_options.merge(log_level: :none) } it 'recovers server-side queued messages' do - channel.attach do |message| + channel.attach do connection.transition_state_machine! :failed end connection.on(:failed) do publishing_client_channel.publish('event', 'message') do - recover_client = Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) - recover_client.channel(channel_name).attach do |recover_client_channel| + recover_client = auto_close Ably::Realtime::Client.new(default_options.merge(recover: client.connection.recovery_key)) + recover_client_channel = recover_client.channel(channel_name) + recover_client_channel.attach do recover_client_channel.subscribe('event') do |message| expect(message.data).to eql('message') stop_reactor end end @@ -735,27 +739,27 @@ let(:client_options) { default_options.merge(recover: 'not-a-valid-connection-key:1', log_level: :none) } it 'emits 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(error.message).to match(/Invalid connection key/) - expect(connection.error_reason.message).to match(/Invalid connection key/) - expect(connection.error_reason.code).to eql(40006) + expect(error.message).to match(/Invalid connectionKey/i) + expect(connection.error_reason.message).to match(/Invalid connectionKey/i) + expect(connection.error_reason.code).to eql(80018) 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) } + let(:client_options) { default_options.merge(recover: 'wVIsgTHAB1UvXh7z-1991d8586:0', log_level: :fatal) } it 'emits 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(error.message).to match(/Invalid connection key/i) - expect(connection.error_reason.message).to match(/Invalid connection key/i) + expect(error.message).to match(/Unable to recover connection/i) + expect(connection.error_reason.message).to match(/Unable to recover connection/i) expect(connection.error_reason.code).to eql(80008) expect(connection.error_reason).to eql(error) stop_reactor end end @@ -1044,11 +1048,11 @@ end end end context 'retry_in' do - let(:client_options) { default_options.merge(log_level: :debug) } + let(:client_options) { default_options.merge(log_level: :error) } it 'is nil when a retry is not required' do connection.on(:connected) do |connection_state_change| expect(connection_state_change.retry_in).to be_nil stop_reactor @@ -1059,11 +1063,11 @@ connection.once(:connecting) do connection.once(:disconnected) do |connection_state_change| expect(connection_state_change.retry_in).to eql(0) stop_reactor end - EventMachine.add_timer(0.001) { connection.transport.unbind } + EventMachine.add_timer(0.005) { connection.transport.unbind } end end it 'is 0 when an immediate reconnect will occur' do connection.once(:connected) do @@ -1080,10 +1084,10 @@ connection.once(:connecting) do connection.once(:disconnected) do |connection_state_change| expect(connection_state_change.retry_in).to be > 0 stop_reactor end - EventMachine.add_timer(0.001) { connection.transport.unbind } + EventMachine.add_timer(0.005) { connection.transport.unbind } end connection.transport.unbind end end end