mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
06279f0e4a
commit
b87f2fe1e4
3 changed files with 37 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
|||
Mon Mar 12 07:03:32 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* random.c (random_s_rand): ensure default PRNG is re-initialized
|
||||
after fork. patched by Eric Wong. [ruby-core:41209][Bug #5661]
|
||||
|
||||
Sun Mar 11 23:57:29 2012 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* pack.c (pack_unpack): when unpack('M') occurs an illegal byte
|
||||
|
|
1
random.c
1
random.c
|
@ -1293,6 +1293,7 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
|
|||
static VALUE
|
||||
random_s_rand(int argc, VALUE *argv, VALUE obj)
|
||||
{
|
||||
rand_start(&default_rand);
|
||||
return random_rand(argc, argv, rb_Random_DEFAULT);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue