spec/acceptance/realtime/presence_spec.rb in ably-0.8.14 vs spec/acceptance/realtime/presence_spec.rb in ably-0.8.15

- old
+ new

@@ -40,13 +40,15 @@ end end def setup_test(method_name, args, options) if options[:enter_first] - presence_client_one.public_send(method_name.to_s.gsub(/leave|update/, 'enter'), args) do + presence_client_one.subscribe do + presence_client_one.unsubscribe yield end + presence_client_one.public_send(method_name.to_s.gsub(/leave|update/, 'enter'), args) else yield end end @@ -466,11 +468,18 @@ end end context 'once server sync is complete' do it 'behaves like an Enumerable allowing direct access to current members' do - when_all(presence_client_one.enter, presence_client_two.enter) do + presence_client_one.enter + presence_client_two.enter + + entered = 0 + presence_client_one.subscribe(:enter) do + entered += 1 + next unless entered == 2 + presence_anonymous_client.members.once(:in_sync) do expect(presence_anonymous_client.members.count).to eql(2) member_ids = presence_anonymous_client.members.map(&:member_key) expect(member_ids.count).to eql(2) expect(member_ids.uniq.count).to eql(2) @@ -493,11 +502,14 @@ end end context 'when attaching to a channel with members present' do it 'is false and the presence channel will subsequently be synced' do - presence_client_one.enter do + presence_client_one.enter + presence_client_one.subscribe(:enter) do + presence_client_one.unsubscribe :enter + channel_anonymous_client.attach do expect(channel_anonymous_client.presence).to_not be_sync_complete channel_anonymous_client.presence.get(wait_for_sync: true) do expect(channel_anonymous_client.presence).to be_sync_complete stop_reactor @@ -678,40 +690,44 @@ context '#get' do context 'with :wait_for_sync option set to true' do it 'waits until sync is complete', em_timeout: 30 do # allow for slow connections and lots of messages enter_expected_count.times do |index| EventMachine.add_timer(index / 10) do - presence_client_one.enter_client("client:#{index}") do |message| - entered << message - next unless entered.count == enter_expected_count + presence_client_one.enter_client("client:#{index}") + end + end - presence_anonymous_client.get(wait_for_sync: true) do |members| - expect(members.map(&:client_id).uniq.count).to eql(enter_expected_count) - expect(members.count).to eql(enter_expected_count) - stop_reactor - end - end + presence_client_one.subscribe(:enter) do |message| + entered << message + next unless entered.count == enter_expected_count + + presence_anonymous_client.get(wait_for_sync: true) do |members| + expect(members.map(&:client_id).uniq.count).to eql(enter_expected_count) + expect(members.count).to eql(enter_expected_count) + stop_reactor end end end end context 'by default' do it 'it does not wait for sync', em_timeout: 30 do # allow for slow connections and lots of messages - enter_expected_count.times do |index| - EventMachine.add_timer(index / 10) do - presence_client_one.enter_client("client:#{index}") do |message| - entered << message - next unless entered.count == enter_expected_count + enter_expected_count.times do |indx| + EventMachine.add_timer(indx / 10) do + presence_client_one.enter_client "client:#{indx}" + end + end - channel_anonymous_client.attach do - presence_anonymous_client.get do |members| - expect(presence_anonymous_client.members).to_not be_in_sync - expect(members.count).to eql(0) - stop_reactor - end - end + presence_client_one.subscribe(:enter) do |message| + entered << message + next unless entered.count == enter_expected_count + + channel_anonymous_client.attach do + presence_anonymous_client.get do |members| + expect(presence_anonymous_client.members).to_not be_in_sync + expect(members.count).to eql(0) + stop_reactor end end end end end @@ -894,19 +910,29 @@ end end context 'and sync is complete' do it 'does not cache members that have left' do - presence_client_one.enter enter_data do + enter_ack = false + + presence_client_one.subscribe(:enter) do + presence_client_one.unsubscribe :enter + expect(presence_client_one.members).to be_in_sync expect(presence_client_one.members.send(:members).count).to eql(1) presence_client_one.leave data end + presence_client_one.enter(enter_data) do + enter_ack = true + end + presence_client_one.subscribe(:leave) do |presence_message| + presence_client_one.unsubscribe :leave expect(presence_message.data).to eql(data) expect(presence_client_one.members.send(:members).count).to eql(0) + expect(enter_ack).to eql(true) stop_reactor end end end end @@ -1288,11 +1314,13 @@ end # skip 'it fails if the connection changes to failed state' it 'returns the current members on the channel' do - presence_client_one.enter do + presence_client_one.enter + presence_client_one.subscribe(:enter) do + presence_client_one.unsubscribe :enter presence_client_one.get do |members| expect(members.count).to eq(1) expect(client_one.client_id).to_not be_nil @@ -1349,26 +1377,31 @@ end end end it 'does not wait for SYNC to complete if :wait_for_sync option is false' do - presence_client_one.enter do + presence_client_one.enter + presence_client_one.subscribe(:enter) do + presence_client_one.unsubscribe :enter + presence_client_two.get(wait_for_sync: false) do |members| expect(members.count).to eql(0) stop_reactor end end end context 'when a member enters and then leaves' do it 'has no members' do presence_client_one.enter do - presence_client_one.leave do - presence_client_one.get do |members| - expect(members.count).to eq(0) - stop_reactor - end + presence_client_one.leave + end + + presence_client_one.subscribe(:leave) do + presence_client_one.get do |members| + expect(members.count).to eq(0) + stop_reactor end end end end @@ -1522,11 +1555,14 @@ end end context 'REST #get' do it 'returns current members' do - presence_client_one.enter(data_payload) do + presence_client_one.enter data_payload + presence_client_one.subscribe(:enter) do + presence_client_one.unsubscribe :enter + members_page = channel_rest_client_one.presence.get this_member = members_page.items.first expect(this_member).to be_a(Ably::Models::PresenceMessage) expect(this_member.client_id).to eql(client_one.client_id) @@ -1536,11 +1572,14 @@ end end it 'returns no members once left' do presence_client_one.enter(data_payload) do - presence_client_one.leave do + presence_client_one.leave + presence_client_one.subscribe(:leave) do + presence_client_one.unsubscribe :leave + members_page = channel_rest_client_one.presence.get expect(members_page.items.count).to eql(0) stop_reactor end end @@ -1652,11 +1691,12 @@ end end context '#get' do it 'returns a list of members with decrypted data' do - encrypted_channel.presence.enter(data) do + encrypted_channel.presence.enter(data) + encrypted_channel.presence.subscribe(:enter) do encrypted_channel.presence.get do |members| member = members.first expect(member.encoding).to be_nil expect(member.data).to eql(data) stop_reactor @@ -1665,11 +1705,12 @@ end end context 'REST #get' do it 'returns a list of members with decrypted data' do - encrypted_channel.presence.enter(data) do + encrypted_channel.presence.enter(data) + encrypted_channel.presence.subscribe(:enter) do member = channel_rest_client_one.presence.get.items.first expect(member.encoding).to be_nil expect(member.data).to eql(data) stop_reactor end @@ -1736,10 +1777,10 @@ end context 'connection failure mid-way through a large member sync' do let(:members_count) { 250 } let(:sync_pages_received) { [] } - let(:client_options) { default_options.merge(log_level: :error) } + let(:client_options) { default_options.merge(log_level: :fatal) } it 'resumes the SYNC operation', em_timeout: 15 do when_all(*members_count.times.map do |index| presence_anonymous_client.enter_client("client:#{index}") end) do