From 8ae40412b270a15ed6df70e2b08babbfd7ff3b11 Mon Sep 17 00:00:00 2001 From: Amit Murthy Date: Tue, 31 May 2016 10:13:24 +0530 Subject: [PATCH] Fix serialization for RandomDevice. Closes #16451. --- base/random.jl | 3 ++- base/serialize.jl | 10 ++++++++++ test/parallel_exec.jl | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/base/random.jl b/base/random.jl index 844c25d6eb7b2..a311cd5aa9114 100644 --- a/base/random.jl +++ b/base/random.jl @@ -44,8 +44,9 @@ if is_windows() else # !windows immutable RandomDevice <: AbstractRNG file::IOStream + unlimited::Bool - RandomDevice(unlimited::Bool=true) = new(open(unlimited ? "/dev/urandom" : "/dev/random")) + RandomDevice(unlimited::Bool=true) = new(open(unlimited ? "/dev/urandom" : "/dev/random"), unlimited) end rand{ T<:Union{Bool, Base.BitInteger}}(rd::RandomDevice, ::Type{T}) = read( rd.file, T) diff --git a/base/serialize.jl b/base/serialize.jl index 4e9bdb3c7e663..65ed2d5654710 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -895,4 +895,14 @@ function deserialize(s::SerializationState, t::Type{Regex}) Regex(pattern, compile_options, match_options) end +if !is_windows() + function serialize(s::SerializationState, rd::RandomDevice) + serialize_type(s, typeof(rd)) + serialize(s, rd.unlimited) + end + function deserialize(s::SerializationState, t::Type{RandomDevice}) + unlimited = deserialize(s) + return RandomDevice(unlimited) + end +end end diff --git a/test/parallel_exec.jl b/test/parallel_exec.jl index dfc030c19306a..076c1bfa8c1e5 100644 --- a/test/parallel_exec.jl +++ b/test/parallel_exec.jl @@ -986,3 +986,16 @@ end # issue #15451 @test remotecall_fetch(x->(y->2y)(x)+1, workers()[1], 3) == 7 + +# issue #16451 +rng=RandomDevice() +retval = @parallel (+) for _ in 1:10 + rand(rng) +end +@test retval > 0.0 && retval < 10.0 + +rand(rng) +retval = @parallel (+) for _ in 1:10 + rand(rng) +end +@test retval > 0.0 && retval < 10.0