spec/acceptance/rest/stats_spec.rb in ably-0.6.2 vs spec/acceptance/rest/stats_spec.rb in ably-0.7.0

- old
+ new

@@ -1,56 +1,61 @@ +# encoding: utf-8 require 'spec_helper' -require 'securerandom' -describe 'Ably::Rest::Client Stats' do - [:json, :msgpack].each do |protocol| - context "over #{protocol}" do - describe 'fetching application stats' do - before(:context) do - reload_test_app - end +describe Ably::Rest::Client, '#stats' do + before(:context) do + WebMock.disable! # ensure previous test's WebMock does not have side effects + reload_test_app + end - def number_of_channels() 3 end - def number_of_messages_per_channel() 5 end + before(:context) do + client = Ably::Rest::Client.new(api_key: api_key, environment: environment) - before(:context) do - @context_client = Ably::Rest::Client.new(api_key: api_key, environment: environment, protocol: protocol) + number_of_channels = 3 + number_of_messages_per_channel = 5 - # Wait until the start of the next minute according to the service - # time because stats are created in 1 minute intervals - service_time = @context_client.time - @interval_start = (service_time.to_i / 60 + 1) * 60 - sleep_time = @interval_start - Time.now.to_i + # Wait until the start of the next minute according to the service + # time because stats are created in 1 minute intervals + service_time = client.time + stats_setup_at = (service_time.to_i / 60 + 1) * 60 + sleep_time = stats_setup_at - Time.now.to_i - if sleep_time > 30 - @interval_start -= 60 # there is enough time to generate the stats in this minute interval - elsif sleep_time > 0 - sleep sleep_time - end + if sleep_time > 30 + stats_setup_at -= 60 # there is enough time to generate the stats in this minute interval + elsif sleep_time > 0 + sleep sleep_time + end - number_of_channels.times do |i| - channel = @context_client.channel("stats-#{i}") + number_of_channels.times do |i| + channel = client.channel("stats-#{i}") - number_of_messages_per_channel.times do |j| - channel.publish("event-#{j}", "data-#{j}") || raise("Unable to publish message") - end - end + number_of_messages_per_channel.times do |j| + channel.publish("event-#{j}", "data-#{j}") || raise("Unable to publish message") + end + end - sleep(10) - end + # wait for stats to be persisted + sleep 10 - [:minute, :hour, :day, :month].each do |interval| - context "by #{interval}" do - it 'should return all the stats for the application' do - stats = @context_client.stats(start: @interval_start * 1000, by: interval.to_s, direction: 'forwards') + @stats_setup_at = stats_setup_at + @messages_published_count = number_of_channels * number_of_messages_per_channel + end - expect(stats.size).to eql(1) + vary_by_protocol do + describe 'fetching application stats' do + [:minute, :hour, :day, :month].each do |interval| + context "by #{interval}" do + let(:client) { Ably::Rest::Client.new(api_key: api_key, environment: environment, protocol: protocol) } - stat = stats.first + it 'should return all the stats for the application' do + stats = client.stats(start: @stats_setup_at * 1000, by: interval.to_s, direction: 'forwards') - expect(stat[:inbound][:all][:messages][:count]).to eql(number_of_channels * number_of_messages_per_channel) - expect(stat[:inbound][:rest][:messages][:count]).to eql(number_of_channels * number_of_messages_per_channel) - end + expect(stats.size).to eql(1) + stat = stats.first + + expect(@messages_published_count).to be_a(Numeric) + expect(stat[:inbound][:all][:messages][:count]).to eql(@messages_published_count) + expect(stat[:inbound][:rest][:messages][:count]).to eql(@messages_published_count) end end end end end