Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit a1f51b3

Browse files
rfourquetKristofferC
authored andcommitted
@testset: with Xoshiro, restore Random.GLOBAL_SEED (JuliaLang/julia#43188)
A `@testset` is supposed to restore the "global RNG state" as it was before execution (so that they can be re-ordered easily, etc.) Also, before a testset starts, the default RNG is re-seeded with the "global seed" (to help reproduce test failures). Before `Xoshiro` as the default RNG, the "global seed" was stored within a `MersenneTwister` object. It was enough for a testset to copy the default RNG at the start, and copy it back at the end. But now the global seed is stored outside of the RNG, so it should also be restored separately. (cherry picked from commit 6b48dc1)
1 parent e9130cd commit a1f51b3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Test.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ function testset_beginend(args, tests, source)
12751275
# by wrapping the body in a function
12761276
local RNG = default_rng()
12771277
local oldrng = copy(RNG)
1278+
local oldseed = Random.GLOBAL_SEED
12781279
try
12791280
# RNG is re-seeded with its own seed to ease reproduce a failed test
12801281
Random.seed!(Random.GLOBAL_SEED)
@@ -1288,6 +1289,7 @@ function testset_beginend(args, tests, source)
12881289
record(ts, Error(:nontest_error, Expr(:tuple), err, Base.current_exceptions(), $(QuoteNode(source))))
12891290
finally
12901291
copy!(RNG, oldrng)
1292+
Random.set_global_seed!(oldseed)
12911293
pop_testset()
12921294
ret = finish(ts)
12931295
end
@@ -1368,6 +1370,7 @@ function testset_forloop(args, testloop, source)
13681370
local ts
13691371
local RNG = default_rng()
13701372
local oldrng = copy(RNG)
1373+
local oldseed = Random.GLOBAL_SEED
13711374
Random.seed!(Random.GLOBAL_SEED)
13721375
local tmprng = copy(RNG)
13731376
try
@@ -1381,6 +1384,7 @@ function testset_forloop(args, testloop, source)
13811384
push!(arr, finish(ts))
13821385
end
13831386
copy!(RNG, oldrng)
1387+
Random.set_global_seed!(oldseed)
13841388
end
13851389
arr
13861390
end

test/runtests.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,29 @@ end
869869
Random.seed!(seed)
870870
@test a == rand()
871871
@test b == rand()
872+
873+
# Even when seed!() is called within a testset A, subsequent testsets
874+
# should start with the same "global RNG state" as what A started with,
875+
# such that the test `refvalue == rand(Int)` below succeeds.
876+
# Currently, this means that Random.GLOBAL_SEED has to be restored,
877+
# in addition to the state of Random.default_rng().
878+
GLOBAL_SEED_orig = Random.GLOBAL_SEED
879+
local refvalue
880+
@testset "GLOBAL_SEED is also preserved (setup)" begin
881+
@test GLOBAL_SEED_orig == Random.GLOBAL_SEED
882+
refvalue = rand(Int)
883+
Random.seed!()
884+
@test GLOBAL_SEED_orig != Random.GLOBAL_SEED
885+
end
886+
@test GLOBAL_SEED_orig == Random.GLOBAL_SEED
887+
@testset "GLOBAL_SEED is also preserved (forloop)" for _=1:3
888+
@test refvalue == rand(Int)
889+
Random.seed!()
890+
end
891+
@test GLOBAL_SEED_orig == Random.GLOBAL_SEED
892+
@testset "GLOBAL_SEED is also preserved (beginend)" begin
893+
@test refvalue == rand(Int)
894+
end
872895
end
873896

874897
@testset "InterruptExceptions #21043" begin
@@ -1202,4 +1225,4 @@ Test.finish(ts::PassInformationTestSet) = ts
12021225
@test ts.results[2].data == ErrorException
12031226
@test ts.results[2].value == ErrorException("Msg")
12041227
@test ts.results[2].source == LineNumberNode(test_throws_line_number, @__FILE__)
1205-
end
1228+
end

0 commit comments

Comments
 (0)