From 707e532963eea3ace622c23dfba157a69b21f22f Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Fri, 5 Jan 2018 14:04:20 +0300 Subject: [PATCH] Specs for instrumentation --- lib/telegram/bot/updates_controller.rb | 3 +- .../bot/updates_controller/instrumentation.rb | 11 +-- .../instrumentation_spec.rb | 86 +++++++++++++++++++ 3 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 spec/telegram/bot/updates_controller/instrumentation_spec.rb diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index 041201d..96f4403 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -81,7 +81,8 @@ module Telegram include AbstractController::Translation include Rescue include ReplyHelpers - prepend Instrumentation + include Instrumentation + extend Session::ConfigMethods PAYLOAD_TYPES = %w[ diff --git a/lib/telegram/bot/updates_controller/instrumentation.rb b/lib/telegram/bot/updates_controller/instrumentation.rb index a2d4ce8..6f84c5f 100644 --- a/lib/telegram/bot/updates_controller/instrumentation.rb +++ b/lib/telegram/bot/updates_controller/instrumentation.rb @@ -4,12 +4,13 @@ module Telegram # Most methods are taken from ActionController::Instrumentation, # some are slightly modified. module Instrumentation - class << self - def prepended(base) - base.send :config_accessor, :logger - base.extend ClassMethods - end + extend ActiveSupport::Concern + included do + config_accessor :logger + end + + class << self def instrument(action, *args, &block) ActiveSupport::Notifications.instrument( "#{action}.updates_controller.bot.telegram", diff --git a/spec/telegram/bot/updates_controller/instrumentation_spec.rb b/spec/telegram/bot/updates_controller/instrumentation_spec.rb new file mode 100644 index 0000000..6a3c90e --- /dev/null +++ b/spec/telegram/bot/updates_controller/instrumentation_spec.rb @@ -0,0 +1,86 @@ +RSpec.describe Telegram::Bot::UpdatesController::Instrumentation do + include_context 'telegram/bot/updates_controller' + + subject { -> { dispatch } } + let(:update) do + build_update :message, default_message_options.merge(text: '/start') + end + + let(:controller_class) do + Class.new(Telegram::Bot::UpdatesController) do + def start(*) + end + end + end + + let(:action_scope) { 'updates_controller.bot.telegram' } + let(:events) { Hash.new { |h, k| h[k] = [] } } + let!(:subscriber) do + ActiveSupport::Notifications.subscribe(/#{action_scope}/) do |action, *args| + events[action] << args + end + end + after { ActiveSupport::Notifications.unsubscribe(subscriber) } + + def actions_list(*prefixes) + prefixes.map { |x| action_name(x) } + end + + def action_name(prefix) + "#{prefix}.#{action_scope}" + end + + describe '#process_action' do + it 'fires start_processing and process_action events' do + should change(events, :keys).by(actions_list(:start_processing, :process_action)) + + action = action_name(:start_processing) + expect(events[action].size).to eq(1) + expect(events[action][0].last).to include(update: update) + + action = action_name(:process_action) + expect(events[action].size).to eq(1) + expect(events[action][0].last).to include(update: update) + end + end + + describe '#halted_callback_hook' do + let(:controller_class) do + super().try(:class_eval) do |x| + x.before_action :halter_method + + def halter_method + throw :abort + end + + self + end + end + + it 'fires halted_callback event' do + should change(events, :keys). + by(actions_list(:start_processing, :halted_callback, :process_action)) + + action = action_name(:halted_callback) + expect(events[action].size).to eq(1) + expect(events[action][0].last).to include(filter: :halter_method) + end + end + + describe '#respond_with' do + before do + def controller.start(*) + respond_with :message, text: 'sample response' + end + end + + it 'fires respond_with event' do + should change(events, :keys). + by(actions_list(:start_processing, :respond_with, :process_action)) + + action = action_name(:respond_with) + expect(events[action].size).to eq(1) + expect(events[action][0].last).to include(type: :message) + end + end +end