Skip to content

Commit f4e4b50

Browse files
IanButterworthgbaraldi
authored andcommitted
Isolate threads test from parent state (#4243)
Co-authored-by: gbaraldi <baraldigabriel@gmail.com> (cherry picked from commit 5577f68)
1 parent 64015f7 commit f4e4b50

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

test/new.jl

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,57 +2042,69 @@ end
20422042
mktempdir() do dir
20432043
path = copy_test_package(dir, "TestThreads")
20442044
cd(path) do
2045-
with_current_env() do
2046-
default_nthreads_default = Threads.nthreads(:default)
2047-
default_nthreads_interactive = Threads.nthreads(:interactive)
2048-
other_nthreads_default = default_nthreads_default == 1 ? 2 : 1
2049-
other_nthreads_interactive = default_nthreads_interactive == 0 ? 1 : 0
2050-
@testset "default" begin
2045+
# Do this all in a subprocess to protect against the parent having non-default threadpool sizes.
2046+
script = """
2047+
using Pkg, Test
2048+
@testset "JULIA_NUM_THREADS=1" begin
20512049
withenv(
2052-
"EXPECTED_NUM_THREADS_DEFAULT" => "$default_nthreads_default",
2053-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2050+
"EXPECTED_NUM_THREADS_DEFAULT" => "1",
2051+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0", # https://github.com/JuliaLang/julia/pull/57454
2052+
"JULIA_NUM_THREADS" => "1",
20542053
) do
20552054
Pkg.test("TestThreads")
20562055
end
20572056
end
2058-
@testset "JULIA_NUM_THREADS=other_nthreads_default" begin
2057+
@testset "JULIA_NUM_THREADS=2" begin
20592058
withenv(
2060-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2061-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2062-
"JULIA_NUM_THREADS" => "$other_nthreads_default",
2059+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2060+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "1",
2061+
"JULIA_NUM_THREADS" => "2",
20632062
) do
20642063
Pkg.test("TestThreads")
20652064
end
20662065
end
2067-
@testset "JULIA_NUM_THREADS=other_nthreads_default,other_nthreads_interactive" begin
2066+
@testset "JULIA_NUM_THREADS=2,0" begin
20682067
withenv(
2069-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2070-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2071-
"JULIA_NUM_THREADS" => "$other_nthreads_default,$other_nthreads_interactive",
2068+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2069+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0",
2070+
"JULIA_NUM_THREADS" => "2,0",
20722071
) do
20732072
Pkg.test("TestThreads")
20742073
end
20752074
end
2076-
@testset "--threads=other_nthreads_default" begin
2075+
2076+
@testset "--threads=1" begin
20772077
withenv(
2078-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2079-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2078+
"EXPECTED_NUM_THREADS_DEFAULT" => "1",
2079+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0", # https://github.com/JuliaLang/julia/pull/57454
2080+
"JULIA_NUM_THREADS" => nothing,
20802081
) do
2081-
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default`)
2082+
Pkg.test("TestThreads"; julia_args=`--threads=1`)
20822083
end
20832084
end
2084-
@testset "--threads=other_nthreads_default,other_nthreads_interactive" begin
2085+
@testset "--threads=2" begin
20852086
withenv(
2086-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2087-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2087+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2088+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "1",
2089+
"JULIA_NUM_THREADS" => nothing,
20882090
) do
2089-
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default,$other_nthreads_interactive`)
2091+
Pkg.test("TestThreads"; julia_args=`--threads=2`)
20902092
end
20912093
end
2092-
end
2094+
@testset "--threads=2,0" begin
2095+
withenv(
2096+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2097+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0",
2098+
"JULIA_NUM_THREADS" => nothing,
2099+
) do
2100+
Pkg.test("TestThreads"; julia_args=`--threads=2,0`)
2101+
end
2102+
end
2103+
"""
2104+
@test Utils.show_output_if_command_errors(`$(Base.julia_cmd()) --project=$(path) --startup-file=no -e "$script"`)
20932105
end
20942106
end
2095-
end
2107+
end
20962108
end
20972109

20982110
#

test/test_packages/TestThreads/test/runtests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ EXPECTED_NUM_THREADS_DEFAULT = parse(Int, ENV["EXPECTED_NUM_THREADS_DEFAULT"])
44
EXPECTED_NUM_THREADS_INTERACTIVE = parse(Int, ENV["EXPECTED_NUM_THREADS_INTERACTIVE"])
55
@assert Threads.nthreads() == EXPECTED_NUM_THREADS_DEFAULT
66
@assert Threads.nthreads(:default) == EXPECTED_NUM_THREADS_DEFAULT
7-
@assert Threads.nthreads(:interactive) == EXPECTED_NUM_THREADS_INTERACTIVE
7+
if Threads.nthreads() == 1
8+
@info "Convert me back to an assert once https://github.com/JuliaLang/julia/pull/57454 has landed" Threads.nthreads(:interactive) EXPECTED_NUM_THREADS_INTERACTIVE
9+
else
10+
@assert Threads.nthreads(:interactive) == EXPECTED_NUM_THREADS_INTERACTIVE
11+
end
12+

test/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ function show_output_if_command_errors(cmd::Cmd)
335335
println(read(out, String))
336336
Base.pipeline_error(proc)
337337
end
338-
return nothing
338+
return true
339339
end
340340

341341
function recursive_rm_cov_files(rootdir::String)

0 commit comments

Comments
 (0)