diff options
Diffstat (limited to 'test')
-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 |