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

Commit 857f1c0

Browse files
authored
Merge pull request JuliaLang/julia#43084 from JuliaLang/backports-release-1.7
release-1.7: Backports for 1.7.0/1.7.0-rc4
2 parents e9130cd + a1f51b3 commit 857f1c0

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)