spec/acceptance/realtime/connection_failures_spec.rb in ably-1.1.1 vs spec/acceptance/realtime/connection_failures_spec.rb in ably-1.1.2

- old
+ new

@@ -43,11 +43,11 @@ it 'enters the failed state and returns an authorization error' do connection.on(:failed) do |connection_state_change| error = connection_state_change.reason expect(connection.state).to eq(:failed) - # TODO: Check error type is a TokenNotFOund exception + # TODO: Check error type is a TokenNotFound exception expect(error.status).to eq(401) expect(error.code).to eq(40400) # not found stop_reactor end end @@ -108,10 +108,76 @@ end end end end end + + context 'request fails due to slow response and subsequent timeout', :webmock, em_timeout: (Ably::Rest::Client::HTTP_DEFAULTS.fetch(:request_timeout) + 5) * 2 do + let(:auth_url) { "http://#{random_str}.domain.will.be.stubbed/path" } + let(:client_options) { default_options.reject { |k, v| k == :key }.merge(auth_url: auth_url, log_level: :fatal) } + + # Timeout +5 seconds, beyond default allowed timeout + before do + stub_request(:get, auth_url). + to_return do |request| + sleep Ably::Rest::Client::HTTP_DEFAULTS.fetch(:request_timeout) + 5 + { status: [500, "Internal Server Error"] } + end + end + + specify 'the connection moves to the disconnected state and tries again, returning again to the disconnected state (#RSA4c, #RSA4c1, #RSA4c2)' do + states = Hash.new { |hash, key| hash[key] = [] } + + connection.once(:connected) { raise "Connection can never move to connected because of auth failures" } + + connection.on do |connection_state| + states[connection_state.current.to_sym] << Time.now + if states[:disconnected].count == 2 && connection_state.current == :disconnected + expect(connection.error_reason).to be_a(Ably::Exceptions::ConnectionError) + expect(connection.error_reason.message).to match(/auth_url/) + EventMachine.add_timer(2) do + expect(states.keys).to include(:connecting, :disconnected) + expect(states[:connecting].count).to eql(2) + expect(states[:connected].count).to eql(0) + stop_reactor + end + end + end + end + end + + context 'request fails once due to slow response but succeeds the second time' do + let(:auth_url) { "http://#{random_str}.domain.will.be.stubbed/path" } + let(:client_options) { default_options.reject { |k, v| k == :key }.merge(auth_url: auth_url, log_level: :fatal) } + + # Timeout +5 seconds, beyond default allowed timeout + before do + token_response = Ably::Rest::Client.new(default_options).auth.request_token + WebMock.enable! + + stub_request(:get, auth_url). + to_return do |request| + sleep Ably::Rest::Client::HTTP_DEFAULTS.fetch(:request_timeout) + { status: [500, "Internal Server Error"] } + end.then. + to_return(:status => 201, :body => token_response.to_json, :headers => { 'Content-Type' => 'application/json' }) + end + + specify 'the connection moves to the disconnected state and tries again, returning again to the disconnected state (#RSA4c, #RSA4c1, #RSA4c2)' do + states = Hash.new { |hash, key| hash[key] = [] } + + connection.once(:connected) do + expect(states[:disconnected].count).to eql(1) + expect(states[:connecting].count).to eql(2) + stop_reactor + end + + connection.on do |connection_state| + states[connection_state.current.to_sym] << Time.now + end + end + end end context 'existing CONNECTED connection' do context 'authorize request failure leaves connection in existing condition' do let(:auth_options) { { auth_url: "http://#{random_str}.domain.will.never.resolve.to/path" } } @@ -423,10 +489,10 @@ let(:timeout) { 1.5 } let(:client_options) do default_options.merge( log_level: :none, - realtime_request_timeout: timeout + realtime_request_timeout: timeout, ) end before do connection.on(:connected) { raise "Connection should not open in this test as CONNECTED ProtocolMessage is never received" }