From f0523f72b46db14e2f50c8347a8708734c650f84 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 15 Feb 2010 21:44:30 +0700 Subject: Rename Rails::Subscriber to Rails::LogSubscriber --- railties/lib/rails/log_subscriber/test_helper.rb | 98 ++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 railties/lib/rails/log_subscriber/test_helper.rb (limited to 'railties/lib/rails/log_subscriber') diff --git a/railties/lib/rails/log_subscriber/test_helper.rb b/railties/lib/rails/log_subscriber/test_helper.rb new file mode 100644 index 0000000000..9ede56cad4 --- /dev/null +++ b/railties/lib/rails/log_subscriber/test_helper.rb @@ -0,0 +1,98 @@ +require 'rails/log_subscriber' + +module Rails + class LogSubscriber + # Provides some helpers to deal with testing log subscribers by setting up + # notifications. Take for instance ActiveRecord subscriber tests: + # + # class SyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::TestHelper + # Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) + # + # def test_basic_query_logging + # Developer.all + # wait + # assert_equal 1, @logger.logged(:debug).size + # assert_match /Developer Load/, @logger.logged(:debug).last + # assert_match /SELECT \* FROM "developers"/, @logger.logged(:debug).last + # end + # + # class SyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::SyncTestHelper + # include LogSubscriberTest + # end + # + # class AsyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::AsyncTestHelper + # include LogSubscriberTest + # end + # end + # + # All you need to do is to ensure that your log subscriber is added to Rails::Subscriber, + # as in the second line of the code above. The test helpers is reponsible for setting + # up the queue, subscriptions and turning colors in logs off. + # + # The messages are available in the @logger instance, which is a logger with limited + # powers (it actually do not send anything to your output), and you can collect them + # doing @logger.logged(level), where level is the level used in logging, like info, + # debug, warn and so on. + # + module TestHelper + def setup + @logger = MockLogger.new + @notifier = ActiveSupport::Notifications::Notifier.new(queue) + + Rails::LogSubscriber.colorize_logging = false + @notifier.subscribe { |*args| Rails::LogSubscriber.dispatch(args) } + + set_logger(@logger) + ActiveSupport::Notifications.notifier = @notifier + end + + def teardown + set_logger(nil) + ActiveSupport::Notifications.notifier = nil + end + + class MockLogger + attr_reader :flush_count + + def initialize + @flush_count = 0 + @logged = Hash.new { |h,k| h[k] = [] } + end + + def method_missing(level, message) + @logged[level] << message + end + + def logged(level) + @logged[level].compact.map { |l| l.to_s.strip } + end + + def flush + @flush_count += 1 + end + end + + # Wait notifications to be published. + def wait + @notifier.wait + end + + # Overwrite if you use another logger in your log subscriber: + # + # def logger + # ActiveRecord::Base.logger = @logger + # end + # + def set_logger(logger) + Rails.logger = logger + end + + def queue + ActiveSupport::Notifications::Fanout.new + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From c2dbc391a9292e6f73cadce2f0ba1be871b29e82 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Tue, 2 Mar 2010 13:05:25 -0800 Subject: Have log subscribers subscribe to the actual events, so the subscriber doesn't subscribe to *every* event, so we can have events that are slow-ish but are not actually run in production. --- railties/lib/rails/log_subscriber/test_helper.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'railties/lib/rails/log_subscriber') diff --git a/railties/lib/rails/log_subscriber/test_helper.rb b/railties/lib/rails/log_subscriber/test_helper.rb index 9ede56cad4..02f5079462 100644 --- a/railties/lib/rails/log_subscriber/test_helper.rb +++ b/railties/lib/rails/log_subscriber/test_helper.rb @@ -43,7 +43,6 @@ module Rails @notifier = ActiveSupport::Notifications::Notifier.new(queue) Rails::LogSubscriber.colorize_logging = false - @notifier.subscribe { |*args| Rails::LogSubscriber.dispatch(args) } set_logger(@logger) ActiveSupport::Notifications.notifier = @notifier -- cgit v1.2.3