lib/submodules/ably-ruby/spec/acceptance/realtime/presence_spec.rb in ably-rest-0.8.2 vs lib/submodules/ably-ruby/spec/acceptance/realtime/presence_spec.rb in ably-rest-0.8.3
- old
+ new
@@ -43,16 +43,71 @@
%w(detached failed).each do |state|
it "raise an exception if the channel is #{state}" do
setup_test(method_name, args, options) do
channel_client_one.attach do
channel_client_one.change_state state.to_sym
- expect { presence_client_one.public_send(method_name, args) }.to raise_error Ably::Exceptions::IncompatibleStateForOperation, /Operation is not allowed when channel is in STATE.#{state}/i
+ expect { presence_client_one.public_send(method_name, args) }.to raise_error Ably::Exceptions::InvalidStateChange, /Operation is not allowed when channel is in STATE.#{state}/i
stop_reactor
end
end
end
end
+
+ it 'implicitly attaches the channel' do
+ expect(channel_client_one).to_not be_attached
+ presence_client_one.public_send(method_name, args) do
+ expect(channel_client_one).to be_attached
+ stop_reactor
+ end
+ end
+
+ context 'when :queue_messages client option is false' do
+ let(:client_one) { Ably::Realtime::Client.new(default_options.merge(queue_messages: false, client_id: random_str)) }
+
+ context 'and connection state initialized' do
+ it 'raises an exception' do
+ expect { presence_client_one.public_send(method_name, args) }.to raise_error Ably::Exceptions::MessageQueueingDisabled
+ expect(client_one.connection).to be_initialized
+ stop_reactor
+ end
+ end
+
+ context 'and connection state connecting' do
+ it 'raises an exception' do
+ client_one.connect
+ EventMachine.next_tick do
+ expect { presence_client_one.public_send(method_name, args) }.to raise_error Ably::Exceptions::MessageQueueingDisabled
+ expect(client_one.connection).to be_connecting
+ stop_reactor
+ end
+ end
+ end
+
+ context 'and connection state disconnected' do
+ let(:client_one) { Ably::Realtime::Client.new(default_options.merge(queue_messages: false, client_id: random_str, :log_level => :error)) }
+
+ it 'raises an exception' do
+ client_one.connection.once(:connected) do
+ client_one.connection.once(:disconnected) do
+ expect { presence_client_one.public_send(method_name, args) }.to raise_error Ably::Exceptions::MessageQueueingDisabled
+ expect(client_one.connection).to be_disconnected
+ stop_reactor
+ end
+ client_one.connection.transition_state_machine :disconnected
+ end
+ end
+ end
+
+ context 'and connection state connected' do
+ it 'publishes the message' do
+ client_one.connection.once(:connected) do
+ presence_client_one.public_send(method_name, args)
+ stop_reactor
+ end
+ end
+ end
+ end
end
context 'with supported data payload content type' do
def register_presence_and_check_data(method_name, data)
if method_name.to_s.match(/_client/)
@@ -118,39 +173,39 @@
end
context 'Integer' do
let(:data) { 1 }
- it 'raises an UnsupportedDataTypeError 40011 exception' do
- expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataTypeError)
+ it 'raises an UnsupportedDataType 40011 exception' do
+ expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'Float' do
let(:data) { 1.1 }
- it 'raises an UnsupportedDataTypeError 40011 exception' do
- expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataTypeError)
+ it 'raises an UnsupportedDataType 40011 exception' do
+ expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'Boolean' do
let(:data) { true }
- it 'raises an UnsupportedDataTypeError 40011 exception' do
- expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataTypeError)
+ it 'raises an UnsupportedDataType 40011 exception' do
+ expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
context 'False' do
let(:data) { false }
- it 'raises an UnsupportedDataTypeError 40011 exception' do
- expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataTypeError)
+ it 'raises an UnsupportedDataType 40011 exception' do
+ expect { presence_action(method_name, data) }.to raise_error(Ably::Exceptions::UnsupportedDataType)
stop_reactor
end
end
end
@@ -159,10 +214,18 @@
expect(presence_client_one.public_send(method_name, args)).to be_a(Ably::Util::SafeDeferrable)
stop_reactor
end
end
+ it 'allows a block to be passed in that is executed upon success' do
+ setup_test(method_name, args, options) do
+ presence_client_one.public_send(method_name, args) do
+ stop_reactor
+ end
+ end
+ end
+
it 'calls the Deferrable callback on success' do
setup_test(method_name, args, options) do
presence_client_one.public_send(method_name, args).callback do |presence|
expect(presence).to eql(presence_client_one)
expect(presence_client_one.state).to eq(expected_state) if expected_state
@@ -1000,11 +1063,11 @@
%w(detached failed).each do |state|
it "raise an exception if the channel is #{state}" do
channel_client_one.attach do
channel_client_one.change_state state.to_sym
- expect { presence_client_one.get }.to raise_error Ably::Exceptions::IncompatibleStateForOperation, /Operation is not allowed when channel is in STATE.#{state}/i
+ expect { presence_client_one.get }.to raise_error Ably::Exceptions::InvalidStateChange, /Operation is not allowed when channel is in STATE.#{state}/i
stop_reactor
end
end
end
@@ -1216,10 +1279,40 @@
end
end
end
end
end
+
+ context 'with event name' do
+ it 'calls the callback for specified presence event' do
+ when_all(channel_client_one.attach, channel_client_two.attach) do
+ presence_client_two.subscribe(:leave) do |presence_message|
+ messages << presence_message
+ next unless messages.count == 1
+
+ expect(messages.map(&:action).map(&:to_sym)).to contain_exactly(:leave)
+ stop_reactor
+ end
+
+ presence_client_one.enter do
+ presence_client_one.update do
+ presence_client_one.leave
+ end
+ end
+ end
+ end
+ end
+
+ it 'implicitly attaches' do
+ expect(client_one.connection).to be_initialized
+ presence_client_one.subscribe { true }
+ channel_client_one.on(:attached) do
+ expect(client_one.connection).to be_connected
+ expect(channel_client_one).to be_attached
+ stop_reactor
+ end
+ end
end
context '#unsubscribe' do
context 'with no arguments' do
it 'removes the callback for all presence events' do
@@ -1231,9 +1324,27 @@
presence_client_one.enter
presence_client_one.update
presence_client_one.leave do
EventMachine.add_timer(1) do
stop_reactor
+ end
+ end
+ end
+ end
+ end
+
+ context 'with event name' do
+ it 'removes the callback for specified presence event' do
+ when_all(channel_client_one.attach, channel_client_two.attach) do
+ subscribe_callback = proc { raise 'Should not be called' }
+ presence_client_two.subscribe :leave, &subscribe_callback
+ presence_client_two.unsubscribe :leave, &subscribe_callback
+
+ presence_client_one.enter do
+ presence_client_one.leave do
+ EventMachine.add_timer(1) do
+ stop_reactor
+ end
end
end
end
end
end