diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-11 22:03:36 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-11 22:03:36 +0000 |
commit | b87f2fe1e44acd66a30fad24bad2d21ddb01e8a2 (patch) | |
tree | d7b59c53d45e52d07bc13ff05858d59c844cbd09 /test/ruby | |
parent | 06279f0e4a901d57030d9dd7b3b7f39ad796ee09 (diff) | |
download | ruby-b87f2fe1e44acd66a30fad24bad2d21ddb01e8a2.tar.gz |
* random.c (random_s_rand): ensure default PRNG is re-initialized
after fork. patched by Eric Wong. [ruby-core:41209][Bug #5661]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34977 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_rand.rb | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 67b6df8562..c7139818de 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -415,14 +415,43 @@ END def test_fork_shuffle pid = fork do - (1..10).to_a.shuffle - raise 'default seed is not set' if srand == 0 + (1..10).to_a.shuffle + raise 'default seed is not set' if srand == 0 end p2, st = Process.waitpid2(pid) assert(st.success?, "#{st.inspect}") rescue NotImplementedError, ArgumentError end + def assert_fork_status(n, mesg, &block) + IO.pipe do |r, w| + (1..n).map do + p1 = fork {w.puts(block.call.to_s)} + _, st = Process.waitpid2(p1) + assert_send([st, :success?], mesg) + r.gets.strip + end + end + end + + def test_rand_reseed_on_fork + bug5661 = '[ruby-core:41209]' + + assert_fork_status(1, bug5661) {Random.rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {Random.rand} + assert_not_equal(r1, r2, bug5661) + + assert_fork_status(1, bug5661) {rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {rand} + assert_not_equal(r1, r2, bug5661) + + stable = Random.new + assert_fork_status(1, bug5661) {stable.rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {stable.rand} + assert_equal(r1, r2, bug5661) + rescue NotImplementedError + end + def test_seed bug3104 = '[ruby-core:29292]' rand_1 = Random.new(-1).rand |