aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/abstract_unit.rb9
-rw-r--r--activesupport/test/benchmarkable_test.rb86
-rw-r--r--activesupport/test/core_ext/class/delegating_attributes_test.rb7
-rw-r--r--activesupport/test/core_ext/regexp_ext_test.rb19
-rw-r--r--activesupport/test/notifications_test.rb201
-rw-r--r--activesupport/test/orchestra_test.rb161
6 files changed, 302 insertions, 181 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index af9656615c..f390c66838 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -1,4 +1,12 @@
ORIG_ARGV = ARGV.dup
+root = File.expand_path('../../..', __FILE__)
+begin
+ require "#{root}/vendor/gems/environment"
+rescue LoadError
+ $:.unshift("#{root}/activesupport/lib")
+ $:.unshift("#{root}/activerecord/lib")
+end
+
require 'test/unit'
@@ -11,7 +19,6 @@ rescue LoadError
end
ENV['NO_RELOAD'] = '1'
-$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
require 'active_support/test_case'
diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb
new file mode 100644
index 0000000000..766956f50f
--- /dev/null
+++ b/activesupport/test/benchmarkable_test.rb
@@ -0,0 +1,86 @@
+require 'abstract_unit'
+
+class BenchmarkableTest < ActiveSupport::TestCase
+ include ActiveSupport::Benchmarkable
+
+ def teardown
+ logger.send(:clear_buffer)
+ end
+
+ def test_without_block
+ assert_raise(LocalJumpError) { benchmark }
+ assert buffer.empty?
+ end
+
+ def test_defaults
+ i_was_run = false
+ benchmark { i_was_run = true }
+ assert i_was_run
+ assert_last_logged
+ end
+
+ def test_with_message
+ i_was_run = false
+ benchmark('test_run') { i_was_run = true }
+ assert i_was_run
+ assert_last_logged 'test_run'
+ end
+
+ def test_with_message_and_deprecated_level
+ i_was_run = false
+
+ assert_deprecated do
+ benchmark('debug_run', :debug) { i_was_run = true }
+ end
+
+ assert i_was_run
+ assert_last_logged 'debug_run'
+ end
+
+ def test_within_level
+ logger.level = ActiveSupport::BufferedLogger::DEBUG
+ benchmark('included_debug_run', :level => :debug) { }
+ assert_last_logged 'included_debug_run'
+ end
+
+ def test_outside_level
+ logger.level = ActiveSupport::BufferedLogger::ERROR
+ benchmark('skipped_debug_run', :level => :debug) { }
+ assert_no_match(/skipped_debug_run/, buffer.last)
+ ensure
+ logger.level = ActiveSupport::BufferedLogger::DEBUG
+ end
+
+ def test_without_silencing
+ benchmark('debug_run', :silence => false) do
+ logger.info "not silenced!"
+ end
+
+ assert_equal 2, buffer.size
+ end
+
+ def test_with_silencing
+ benchmark('debug_run', :silence => true) do
+ logger.info "silenced!"
+ end
+
+ assert_equal 1, buffer.size
+ end
+
+ private
+ def logger
+ @logger ||= begin
+ logger = ActiveSupport::BufferedLogger.new(StringIO.new)
+ logger.auto_flushing = false
+ logger
+ end
+ end
+
+ def buffer
+ logger.send(:buffer)
+ end
+
+ def assert_last_logged(message = 'Benchmarking')
+ assert_match(/^#{message} \(.*\)$/, buffer.last)
+ end
+end
diff --git a/activesupport/test/core_ext/class/delegating_attributes_test.rb b/activesupport/test/core_ext/class/delegating_attributes_test.rb
index b51d68551d..beb55ba17e 100644
--- a/activesupport/test/core_ext/class/delegating_attributes_test.rb
+++ b/activesupport/test/core_ext/class/delegating_attributes_test.rb
@@ -52,6 +52,13 @@ class DelegatingAttributesTest < Test::Unit::TestCase
assert !single_class.public_instance_methods.map(&:to_s).include?("both=")
end
+ def test_simple_accessor_declaration_with_instance_reader_false
+ single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false
+ assert single_class.respond_to?(:no_instance_reader)
+ assert single_class.respond_to?(:no_instance_reader=)
+ assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader")
+ end
+
def test_working_with_simple_attributes
single_class.superclass_delegating_accessor :both
diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb
index cc3f07d5c5..68b089d5b4 100644
--- a/activesupport/test/core_ext/regexp_ext_test.rb
+++ b/activesupport/test/core_ext/regexp_ext_test.rb
@@ -2,28 +2,9 @@ require 'abstract_unit'
require 'active_support/core_ext/regexp'
class RegexpExtAccessTests < Test::Unit::TestCase
- def test_number_of_captures
- assert_equal 0, //.number_of_captures
- assert_equal 1, /.(.)./.number_of_captures
- assert_equal 2, /.(.).(?:.).(.)/.number_of_captures
- assert_equal 3, /.((.).(?:.).(.))/.number_of_captures
- end
-
def test_multiline
assert_equal true, //m.multiline?
assert_equal false, //.multiline?
assert_equal false, /(?m:)/.multiline?
end
-
- def test_optionalize
- assert_equal "a?", Regexp.optionalize("a")
- assert_equal "(?:foo)?", Regexp.optionalize("foo")
- assert_equal "", Regexp.optionalize("")
- end
-
- def test_unoptionalize
- assert_equal "a", Regexp.unoptionalize("a?")
- assert_equal "foo", Regexp.unoptionalize("(?:foo)?")
- assert_equal "", Regexp.unoptionalize("")
- end
end
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb
new file mode 100644
index 0000000000..9175c8f26e
--- /dev/null
+++ b/activesupport/test/notifications_test.rb
@@ -0,0 +1,201 @@
+require 'abstract_unit'
+
+# Allow LittleFanout to be cleaned.
+class ActiveSupport::Notifications::LittleFanout
+ def clear
+ @listeners.clear
+ end
+end
+
+class NotificationsEventTest < Test::Unit::TestCase
+ def test_events_are_initialized_with_details
+ event = event(:foo, Time.now, Time.now + 1, 1, random_id, :payload => :bar)
+ assert_equal :foo, event.name
+ assert_equal Hash[:payload => :bar], event.payload
+ end
+
+ def test_events_consumes_information_given_as_payload
+ time = Time.now
+ event = event(:foo, time, time + 0.01, 1, random_id, {})
+
+ assert_equal Hash.new, event.payload
+ assert_equal time, event.time
+ assert_equal 1, event.result
+ assert_equal 10.0, event.duration
+ end
+
+ def test_event_is_parent_based_on_time_frame
+ time = Time.utc(2009, 01, 01, 0, 0, 1)
+
+ parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, nil, random_id, {})
+ child = event(:foo, time, time + 10, nil, random_id, {})
+ not_child = event(:foo, time, time + 100, nil, random_id, {})
+
+ assert parent.parent_of?(child)
+ assert !child.parent_of?(parent)
+ assert !parent.parent_of?(not_child)
+ assert !not_child.parent_of?(parent)
+ end
+
+protected
+
+ def random_id
+ @random_id ||= ActiveSupport::SecureRandom.hex(10)
+ end
+
+ def event(*args)
+ ActiveSupport::Notifications::Event.new(*args)
+ end
+end
+
+class NotificationsMainTest < Test::Unit::TestCase
+ def setup
+ @events = []
+ Thread.abort_on_exception = true
+ ActiveSupport::Notifications.subscribe do |*args|
+ @events << ActiveSupport::Notifications::Event.new(*args)
+ end
+ end
+
+ def teardown
+ Thread.abort_on_exception = false
+ ActiveSupport::Notifications.queue.clear
+ end
+
+ def test_notifications_returns_action_result
+ result = ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
+ 1 + 1
+ end
+
+ assert_equal 2, result
+ end
+
+ def test_events_are_published_to_a_listener
+ ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
+ 1 + 1
+ end
+
+ sleep(0.1)
+
+ assert_equal 1, @events.size
+ assert_equal :awesome, @events.last.name
+ assert_equal Hash[:payload => "notifications"], @events.last.payload
+ end
+
+ def test_nested_events_can_be_instrumented
+ ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
+ ActiveSupport::Notifications.instrument(:wot, :payload => "child") do
+ 1 + 1
+ end
+
+ sleep(0.1)
+
+ assert_equal 1, @events.size
+ assert_equal :wot, @events.first.name
+ assert_equal Hash[:payload => "child"], @events.first.payload
+ end
+
+ sleep(0.1)
+
+ assert_equal 2, @events.size
+ assert_equal :awesome, @events.last.name
+ assert_equal Hash[:payload => "notifications"], @events.last.payload
+ assert_in_delta 100, @events.last.duration, 70
+ end
+
+ def test_event_is_pushed_even_if_block_fails
+ ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
+ raise "OMG"
+ end rescue RuntimeError
+
+ sleep(0.1)
+
+ assert_equal 1, @events.size
+ assert_equal :awesome, @events.last.name
+ assert_equal Hash[:payload => "notifications"], @events.last.payload
+ end
+
+ def test_event_is_pushed_even_without_block
+ ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications")
+ sleep(0.1)
+
+ assert_equal 1, @events.size
+ assert_equal :awesome, @events.last.name
+ assert_equal Hash[:payload => "notifications"], @events.last.payload
+ end
+
+ def test_subscribed_in_a_transaction
+ @another = []
+
+ ActiveSupport::Notifications.subscribe("cache") do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+ ActiveSupport::Notifications.transaction do
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+ end
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+
+ sleep 0.1
+
+ before, during, after = @another.map {|e| e.transaction_id }
+ assert_equal before, after
+ assert_not_equal before, during
+ end
+
+ def test_subscriber_with_pattern
+ @another = []
+
+ ActiveSupport::Notifications.subscribe("cache") do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+
+ sleep(0.1)
+
+ assert_equal 1, @another.size
+ assert_equal :cache, @another.first.name
+ assert_equal 1, @another.first.result
+ end
+
+ def test_subscriber_with_pattern_as_regexp
+ @another = []
+ ActiveSupport::Notifications.subscribe(/cache/) do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ ActiveSupport::Notifications.instrument(:something){ 0 }
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+
+ sleep(0.1)
+
+ assert_equal 1, @another.size
+ assert_equal :cache, @another.first.name
+ assert_equal 1, @another.first.result
+ end
+
+ def test_with_several_consumers_and_several_events
+ @another = []
+ ActiveSupport::Notifications.subscribe do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ 1.upto(100) do |i|
+ ActiveSupport::Notifications.instrument(:value){ i }
+ end
+
+ sleep 0.1
+
+ assert_equal 100, @events.size
+ assert_equal :value, @events.first.name
+ assert_equal 1, @events.first.result
+ assert_equal 100, @events.last.result
+
+ assert_equal 100, @another.size
+ assert_equal :value, @another.first.name
+ assert_equal 1, @another.first.result
+ assert_equal 100, @another.last.result
+ end
+end
diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb
deleted file mode 100644
index 683cc36f6a..0000000000
--- a/activesupport/test/orchestra_test.rb
+++ /dev/null
@@ -1,161 +0,0 @@
-require 'abstract_unit'
-
-class OrchestraEventTest < Test::Unit::TestCase
- def setup
- @parent = ActiveSupport::Orchestra::Event.new(:parent)
- end
-
- def test_initialization_with_name_and_parent_and_payload
- event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra")
- assert_equal(:awesome, event.name)
- assert_equal(@parent, event.parent)
- assert_equal({ :payload => "orchestra" }, event.payload)
- end
-
- def test_thread_id_is_set_on_initialization
- event = ActiveSupport::Orchestra::Event.new(:awesome)
- assert_equal Thread.current.object_id, event.thread_id
- end
-
- def test_current_time_is_set_on_initialization
- previous_time = Time.now.utc
- event = ActiveSupport::Orchestra::Event.new(:awesome)
- assert_kind_of Time, event.time
- assert event.time.to_f >= previous_time.to_f
- end
-
- def test_duration_is_set_when_event_finishes
- event = ActiveSupport::Orchestra::Event.new(:awesome)
- sleep(0.1)
- event.finish!
- assert_in_delta 100, event.duration, 30
- end
-end
-
-class OrchestraMainTest < Test::Unit::TestCase
- def setup
- @listener = []
- ActiveSupport::Orchestra.register @listener
- end
-
- def teardown
- ActiveSupport::Orchestra.unregister @listener
- end
-
- def test_orchestra_allows_any_action_to_be_instrumented
- event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
- sleep(0.1)
- end
-
- assert_equal :awesome, event.name
- assert_equal "orchestra", event.payload
- assert_in_delta 100, event.duration, 30
- end
-
- def test_block_result_is_stored
- event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
- 1 + 1
- end
-
- assert_equal 2, event.result
- end
-
- def test_events_are_published_to_a_listener
- event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
- 1 + 1
- end
-
- assert_equal 1, @listener.size
- assert_equal :awesome, @listener.last.name
- assert_equal "orchestra", @listener.last.payload
- end
-
- def test_nested_events_can_be_instrumented
- ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
- ActiveSupport::Orchestra.instrument(:wot, "child") do
- sleep(0.1)
- end
-
- assert_equal 1, @listener.size
- assert_equal :wot, @listener.first.name
- assert_equal "child", @listener.first.payload
-
- assert_nil @listener.first.parent.duration
- assert_in_delta 100, @listener.first.duration, 30
- end
-
- assert_equal 2, @listener.size
- assert_equal :awesome, @listener.last.name
- assert_equal "orchestra", @listener.last.payload
- assert_in_delta 100, @listener.first.parent.duration, 30
- end
-
- def test_event_is_pushed_even_if_block_fails
- ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
- raise "OMG"
- end rescue RuntimeError
-
- assert_equal 1, @listener.size
- assert_equal :awesome, @listener.last.name
- assert_equal "orchestra", @listener.last.payload
- end
-end
-
-class OrchestraListenerTest < Test::Unit::TestCase
- class MyListener < ActiveSupport::Orchestra::Listener
- attr_reader :consumed
-
- def consume(event)
- @consumed ||= []
- @consumed << event
- end
- end
-
- def setup
- @listener = MyListener.new
- ActiveSupport::Orchestra.register @listener
- end
-
- def teardown
- ActiveSupport::Orchestra.unregister @listener
- end
-
- def test_thread_is_exposed_by_listener
- assert_kind_of Thread, @listener.thread
- end
-
- def test_event_is_consumed_when_an_action_is_instrumented
- ActiveSupport::Orchestra.instrument(:sum) do
- 1 + 1
- end
- sleep 0.1
- assert_equal 1, @listener.consumed.size
- assert_equal :sum, @listener.consumed.first.name
- assert_equal 2, @listener.consumed.first.result
- end
-
- def test_with_sevaral_consumers_and_several_events
- @another = MyListener.new
- ActiveSupport::Orchestra.register @another
-
- 1.upto(100) do |i|
- ActiveSupport::Orchestra.instrument(:value) do
- i
- end
- end
-
- sleep 0.1
-
- assert_equal 100, @listener.consumed.size
- assert_equal :value, @listener.consumed.first.name
- assert_equal 1, @listener.consumed.first.result
- assert_equal 100, @listener.consumed.last.result
-
- assert_equal 100, @another.consumed.size
- assert_equal :value, @another.consumed.first.name
- assert_equal 1, @another.consumed.first.result
- assert_equal 100, @another.consumed.last.result
- ensure
- ActiveSupport::Orchestra.unregister @another
- end
-end