spec/acceptance/realtime/channel_history_spec.rb in ably-0.7.5 vs spec/acceptance/realtime/channel_history_spec.rb in ably-0.7.6

- old
+ new

@@ -1,14 +1,15 @@ # encoding: utf-8 require 'spec_helper' describe Ably::Realtime::Channel, '#history', :event_machine do vary_by_protocol do - let(:default_options) { options.merge(api_key: api_key, environment: environment, protocol: protocol) } + let(:default_options) { options.merge(key: api_key, environment: environment, protocol: protocol) } let(:client) { Ably::Realtime::Client.new(default_options) } let(:channel) { client.channel(channel_name) } + let(:rest_channel) { client.rest_client.channel(channel_name) } let(:client2) { Ably::Realtime::Client.new(default_options) } let(:channel2) { client2.channel(channel_name) } let(:channel_name) { "persisted:#{random_str(2)}" } @@ -19,40 +20,40 @@ it 'returns a SafeDeferrable that catches exceptions in callbacks and logs them' do channel.publish('event', payload) do |message| history = channel.history expect(history).to be_a(Ably::Util::SafeDeferrable) - history.callback do |messages| - expect(messages.count).to eql(1) - expect(messages).to be_a(Ably::Models::PaginatedResource) + history.callback do |page| + expect(page.items.count).to eql(1) + expect(page).to be_a(Ably::Models::PaginatedResource) stop_reactor end end end context 'with a single client publishing and receiving' do - it 'retrieves real-time history' do + it 'retrieves realtime history' do channel.publish('event', payload) do |message| - channel.history do |history| - expect(history.length).to eql(1) - expect(history[0].data).to eql(payload) + channel.history do |page| + expect(page.items.length).to eql(1) + expect(page.items[0].data).to eql(payload) stop_reactor end end end end context 'with two clients publishing messages on the same channel' do - it 'retrieves real-time history on both channels' do + it 'retrieves realtime history on both channels' do channel.publish('event', payload) do |message| channel2.publish('event', payload) do |message| - channel.history do |history| - expect(history.length).to eql(2) - expect(history.map(&:data).uniq).to eql([payload]) + channel.history do |page| + expect(page.items.length).to eql(2) + expect(page.items.map(&:data).uniq).to eql([payload]) - channel2.history do |history_2| - expect(history_2.length).to eql(2) + channel2.history do |page_2| + expect(page_2.items.length).to eql(2) stop_reactor end end end end @@ -63,22 +64,22 @@ let(:messages_sent) { 30 } let(:rate_per_second) { 10 } let(:limit) { 15 } def ensure_message_history_direction_and_paging_is_correct(direction) - channel.history(direction: direction, limit: limit) do |history| - expect(history.length).to eql(limit) + channel.history(direction: direction, limit: limit) do |history_page| + expect(history_page.items.length).to eql(limit) limit.times do |index| - expect(history[index].data).to eql("history#{index}") + expect(history_page.items[index].data).to eql("history#{index}") end - history.next_page do |history| - expect(history.length).to eql(limit) + history_page.next do |next_page| + expect(next_page.items.length).to eql(limit) limit.times do |index| - expect(history[index].data).to eql("history#{index + limit}") + expect(next_page.items[index].data).to eql("history#{index + limit}") end - expect(history.last_page?).to eql(true) + expect(next_page).to be_last stop_reactor end end end @@ -139,16 +140,66 @@ end channel.subscribe('event') do |message| messages << message if messages.count == batches * messages_per_batch - channel.history do |history| - expect(history.map(&:id).sort).to eql(messages.map(&:id).sort) + channel.history do |page| + expect(page.items.map(&:id).sort).to eql(messages.map(&:id).sort) stop_reactor end end end end + end + end + + context 'with option until_attach: true' do + let(:event) { random_str } + let(:message_before_attach) { random_str } + let(:message_after_attach) { random_str } + + it 'retrieves all messages before channel was attached' do + rest_channel.publish event, message_before_attach + + channel.attach do + channel.publish(event, message_after_attach) do + channel.history(until_attach: true) do |messages| + expect(messages.items.count).to eql(1) + expect(messages.items.first.data).to eql(message_before_attach) + stop_reactor + end + end + end + end + + context 'and two pages of messages' do + it 'retrieves two pages of messages before channel was attached' do + 10.times { rest_channel.publish event, message_before_attach } + + channel.attach do + 10.times { rest_channel.publish event, message_after_attach } + + EventMachine.add_timer(0.5) do + channel.history(until_attach: true, limit: 5) do |messages| + expect(messages.items.count).to eql(5) + expect(messages.items.map(&:data).uniq.first).to eql(message_before_attach) + + messages.next do |next_page_messages| + expect(next_page_messages.items.count).to eql(5) + expect(next_page_messages.items.map(&:data).uniq.first).to eql(message_before_attach) + expect(next_page_messages).to be_last + + stop_reactor + end + end + end + end + end + end + + it 'raises an exception unless state is attached' do + expect { channel.history(until_attach: true) }.to raise_error(ArgumentError, /not attached/) + stop_reactor end end end end