Skip to content

Commit 5577f68

Browse files
Isolate threads test from parent state (#4243)
Co-authored-by: gbaraldi <[email protected]>
1 parent 88629b5 commit 5577f68

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

test/new.jl

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,57 +2111,66 @@ end
21112111
mktempdir() do dir
21122112
path = copy_test_package(dir, "TestThreads")
21132113
cd(path) do
2114-
with_current_env() do
2115-
default_nthreads_default = Threads.nthreads(:default)
2116-
default_nthreads_interactive = Threads.nthreads(:interactive)
2117-
other_nthreads_default = default_nthreads_default == 1 ? 2 : 1
2118-
other_nthreads_interactive = default_nthreads_interactive == 0 ? 1 : 0
2119-
@testset "default" begin
2114+
# Do this all in a subprocess to protect against the parent having non-default threadpool sizes.
2115+
script = """
2116+
using Pkg, Test
2117+
@testset "JULIA_NUM_THREADS=1" begin
21202118
withenv(
2121-
"EXPECTED_NUM_THREADS_DEFAULT" => "$default_nthreads_default",
2122-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2123-
"JULIA_NUM_THREADS" => nothing,
2119+
"EXPECTED_NUM_THREADS_DEFAULT" => "1",
2120+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0", # https://github.com/JuliaLang/julia/pull/57454
2121+
"JULIA_NUM_THREADS" => "1",
21242122
) do
21252123
Pkg.test("TestThreads")
21262124
end
21272125
end
2128-
@testset "JULIA_NUM_THREADS=other_nthreads_default" begin
2126+
@testset "JULIA_NUM_THREADS=2" begin
21292127
withenv(
2130-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2131-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2132-
"JULIA_NUM_THREADS" => "$other_nthreads_default",
2128+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2129+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "1",
2130+
"JULIA_NUM_THREADS" => "2",
21332131
) do
21342132
Pkg.test("TestThreads")
21352133
end
21362134
end
2137-
@testset "JULIA_NUM_THREADS=other_nthreads_default,other_nthreads_interactive" begin
2135+
@testset "JULIA_NUM_THREADS=2,0" begin
21382136
withenv(
2139-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2140-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2141-
"JULIA_NUM_THREADS" => "$other_nthreads_default,$other_nthreads_interactive",
2137+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2138+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0",
2139+
"JULIA_NUM_THREADS" => "2,0",
21422140
) do
21432141
Pkg.test("TestThreads")
21442142
end
21452143
end
2146-
@testset "--threads=other_nthreads_default" begin
2144+
2145+
@testset "--threads=1" begin
21472146
withenv(
2148-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2149-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2147+
"EXPECTED_NUM_THREADS_DEFAULT" => "1",
2148+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0", # https://github.com/JuliaLang/julia/pull/57454
21502149
"JULIA_NUM_THREADS" => nothing,
21512150
) do
2152-
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default`)
2151+
Pkg.test("TestThreads"; julia_args=`--threads=1`)
21532152
end
21542153
end
2155-
@testset "--threads=other_nthreads_default,other_nthreads_interactive" begin
2154+
@testset "--threads=2" begin
21562155
withenv(
2157-
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2158-
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2156+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2157+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "1",
21592158
"JULIA_NUM_THREADS" => nothing,
21602159
) do
2161-
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default,$other_nthreads_interactive`)
2160+
Pkg.test("TestThreads"; julia_args=`--threads=2`)
21622161
end
21632162
end
2164-
end
2163+
@testset "--threads=2,0" begin
2164+
withenv(
2165+
"EXPECTED_NUM_THREADS_DEFAULT" => "2",
2166+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "0",
2167+
"JULIA_NUM_THREADS" => nothing,
2168+
) do
2169+
Pkg.test("TestThreads"; julia_args=`--threads=2,0`)
2170+
end
2171+
end
2172+
"""
2173+
@test Utils.show_output_if_command_errors(`$(Base.julia_cmd()) --project=$(path) --startup-file=no -e "$script"`)
21652174
end
21662175
end
21672176
end

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
@@ -346,7 +346,7 @@ function show_output_if_command_errors(cmd::Cmd)
346346
println(read(out, String))
347347
Base.pipeline_error(proc)
348348
end
349-
return nothing
349+
return true
350350
end
351351

352352
function recursive_rm_cov_files(rootdir::String)

0 commit comments

Comments
 (0)