diff options
Diffstat (limited to 'activesupport/test/xml_mini_test.rb')
-rw-r--r-- | activesupport/test/xml_mini_test.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/activesupport/test/xml_mini_test.rb b/activesupport/test/xml_mini_test.rb index 504fc96493..a025279e16 100644 --- a/activesupport/test/xml_mini_test.rb +++ b/activesupport/test/xml_mini_test.rb @@ -99,4 +99,66 @@ module XmlMiniTest end # TODO: test the remaining functions hidden in #to_tag. end + + class WithBackendTest < ActiveSupport::TestCase + module REXML end + module LibXML end + module Nokogiri end + + setup do + @xml = ActiveSupport::XmlMini + end + + test "#with_backend should switch backend and then switch back" do + @xml.backend = REXML + @xml.with_backend(LibXML) do + assert_equal LibXML, @xml.backend + @xml.with_backend(Nokogiri) do + assert_equal Nokogiri, @xml.backend + end + assert_equal LibXML, @xml.backend + end + assert_equal REXML, @xml.backend + end + + test "backend switch inside #with_backend block" do + @xml.with_backend(LibXML) do + @xml.backend = REXML + assert_equal REXML, @xml.backend + end + assert_equal REXML, @xml.backend + end + end + + class ThreadSafetyTest < ActiveSupport::TestCase + module REXML end + module LibXML end + + setup do + @xml = ActiveSupport::XmlMini + end + + test "#with_backend should be thread-safe" do + @xml.backend = REXML + t = Thread.new do + @xml.with_backend(LibXML) { sleep 1 } + end + sleep 0.1 while t.status != "sleep" + + # We should get `old_backend` here even while another + # thread is using `new_backend`. + assert_equal REXML, @xml.backend + end + + test "nested #with_backend should be thread-safe" do + @xml.with_backend(REXML) do + t = Thread.new do + @xml.with_backend(LibXML) { sleep 1 } + end + sleep 0.1 while t.status != "sleep" + + assert_equal REXML, @xml.backend + end + end + end end |