summaryrefslogtreecommitdiff
path: root/test/digest
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-31 13:21:51 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-31 13:21:51 +0000
commitc02fa39463a0c6bf698b01bc610135604aca2ff4 (patch)
tree7a5a7384ed322e2d8810c7b78e77b456f78236ed /test/digest
parent2172b44898a643058eb6a1943943d33635a6db61 (diff)
Make Digest() thread-safe.
* ext/digest/lib/digest.rb (Digest()): This function should now be thread-safe. If you have a problem with regard to on-demand loading under a multi-threaded environment, preload "digest/*" modules on boot or use this method instead of directly referencing Digest::*. [Bug #9494] cf. https://github.com/aws/aws-sdk-ruby/issues/525 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/digest')
-rw-r--r--test/digest/digest/foo.rb10
-rw-r--r--test/digest/test_digest.rb61
2 files changed, 71 insertions, 0 deletions
diff --git a/test/digest/digest/foo.rb b/test/digest/digest/foo.rb
new file mode 100644
index 0000000..d576ef0
--- /dev/null
+++ b/test/digest/digest/foo.rb
@@ -0,0 +1,10 @@
+module Digest
+ Foo = nil
+
+ sleep 0.2
+
+ remove_const(:Foo)
+
+ class Foo < Class
+ end
+end
diff --git a/test/digest/test_digest.rb b/test/digest/test_digest.rb
index f4ec023..ccf0a26 100644
--- a/test/digest/test_digest.rb
+++ b/test/digest/test_digest.rb
@@ -208,4 +208,65 @@ module TestDigest
end
end
end
+
+ class TestDigestParen < Test::Unit::TestCase
+ def test_sha2
+ assert_separately(%w[-rdigest], <<-'end;')
+ assert_nothing_raised {
+ Digest(:SHA256).new
+ Digest(:SHA384).new
+ Digest(:SHA512).new
+ }
+ end;
+ end
+
+ def test_no_lib
+ assert_separately(%w[-rdigest], <<-'end;')
+ class Digest::Nolib < Digest::Class
+ end
+
+ assert_nothing_raised {
+ Digest(:Nolib).new
+ }
+ end;
+ end
+
+ def test_no_lib_no_def
+ assert_separately(%w[-rdigest], <<-'end;')
+ assert_raise(LoadError) {
+ Digest(:Nodef).new
+ }
+ end;
+ end
+
+ def test_race
+ assert_separately(['-rdigest', "-I#{File.dirname(__FILE__)}"], <<-'end;')
+ assert_nothing_raised {
+ t = Thread.start {
+ sleep 0.1
+ Digest(:Foo).new
+ }
+ Digest(:Foo).new
+ t.join
+ }
+ end;
+ end
+
+ def test_race_mixed
+ assert_separately(['-rdigest', "-I#{File.dirname(__FILE__)}"], <<-'end;')
+ assert_nothing_raised {
+ t = Thread.start {
+ sleep 0.1
+ Digest::Foo.new
+ }
+ Digest(:Foo).new
+ begin
+ t.join
+ rescue NoMethodError, NameError
+ # NoMethodError is highly likely; NameError is listed just in case
+ end
+ }
+ end;
+ end
+ end
end