Skip to content

Commit 0b4a3f1

Browse files
stemannIanButterworth
authored andcommitted
Changed Pkg.test to prioritise julia_args --threads over current process threads (#4141)
Co-authored-by: Ian Butterworth <[email protected]> (cherry picked from commit f517489)
1 parent fe3714e commit 0b4a3f1

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

src/Operations.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,14 +1985,18 @@ end
19851985

19861986

19871987
function get_threads_spec()
1988-
if Threads.nthreads(:interactive) > 0
1988+
if haskey(ENV, "JULIA_NUM_THREADS")
1989+
# if set, prefer JULIA_NUM_THREADS because this is passed to the test worker via --threads
1990+
# which takes precedence in the worker
1991+
ENV["JULIA_NUM_THREADS"]
1992+
elseif Threads.nthreads(:interactive) > 0
19891993
"$(Threads.nthreads(:default)),$(Threads.nthreads(:interactive))"
19901994
else
19911995
"$(Threads.nthreads(:default))"
19921996
end
19931997
end
19941998

1995-
function gen_subprocess_flags(source_path::String; coverage, julia_args)
1999+
function gen_subprocess_flags(source_path::String; coverage, julia_args::Cmd)
19962000
coverage_arg = if coverage isa Bool
19972001
# source_path is the package root, not "src" so "ext" etc. is included
19982002
coverage ? string("@", source_path) : "none"
@@ -2330,7 +2334,7 @@ function test(ctx::Context, pkgs::Vector{PackageSpec};
23302334
test_fn !== nothing && test_fn()
23312335
sandbox_ctx = Context(;io=ctx.io)
23322336
status(sandbox_ctx.env, sandbox_ctx.registries; mode=PKGMODE_COMBINED, io=sandbox_ctx.io, ignore_indent = false, show_usagetips = false)
2333-
flags = gen_subprocess_flags(source_path; coverage,julia_args)
2337+
flags = gen_subprocess_flags(source_path; coverage, julia_args)
23342338

23352339
if should_autoprecompile()
23362340
cacheflags = Base.CacheFlags(parse(UInt8, read(`$(Base.julia_cmd()) $(flags) --eval 'show(ccall(:jl_cache_flags, UInt8, ()))'`, String)))
@@ -2340,7 +2344,7 @@ function test(ctx::Context, pkgs::Vector{PackageSpec};
23402344
printpkgstyle(ctx.io, :Testing, "Running tests...")
23412345
flush(ctx.io)
23422346
code = gen_test_code(source_path; test_args)
2343-
cmd = `$(Base.julia_cmd()) $(flags) --threads=$(get_threads_spec()) --eval $code`
2347+
cmd = `$(Base.julia_cmd()) --threads=$(get_threads_spec()) $(flags) --eval $code`
23442348
p, interrupted = subprocess_handler(cmd, ctx.io, "Tests interrupted. Exiting the test process")
23452349
if success(p)
23462350
printpkgstyle(ctx.io, :Testing, pkg.name * " tests passed ")

test/new.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,62 @@ end
20142014
Pkg.test("TestArguments"; test_args=`a b`, julia_args=`--quiet --check-bounds=no`)
20152015
Pkg.test("TestArguments"; test_args=["a", "b"], julia_args=["--quiet", "--check-bounds=no"])
20162016
end end
2017+
2018+
@testset "threads" begin
2019+
mktempdir() do dir
2020+
path = copy_test_package(dir, "TestThreads")
2021+
cd(path) do
2022+
with_current_env() do
2023+
default_nthreads_default = Threads.nthreads(:default)
2024+
default_nthreads_interactive = Threads.nthreads(:interactive)
2025+
other_nthreads_default = default_nthreads_default == 1 ? 2 : 1
2026+
other_nthreads_interactive = default_nthreads_interactive == 0 ? 1 : 0
2027+
@testset "default" begin
2028+
withenv(
2029+
"EXPECTED_NUM_THREADS_DEFAULT" => "$default_nthreads_default",
2030+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2031+
) do
2032+
Pkg.test("TestThreads")
2033+
end
2034+
end
2035+
@testset "JULIA_NUM_THREADS=other_nthreads_default" begin
2036+
withenv(
2037+
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2038+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2039+
"JULIA_NUM_THREADS" => "$other_nthreads_default",
2040+
) do
2041+
Pkg.test("TestThreads")
2042+
end
2043+
end
2044+
@testset "JULIA_NUM_THREADS=other_nthreads_default,other_nthreads_interactive" begin
2045+
withenv(
2046+
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2047+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2048+
"JULIA_NUM_THREADS" => "$other_nthreads_default,$other_nthreads_interactive",
2049+
) do
2050+
Pkg.test("TestThreads")
2051+
end
2052+
end
2053+
@testset "--threads=other_nthreads_default" begin
2054+
withenv(
2055+
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2056+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$default_nthreads_interactive",
2057+
) do
2058+
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default`)
2059+
end
2060+
end
2061+
@testset "--threads=other_nthreads_default,other_nthreads_interactive" begin
2062+
withenv(
2063+
"EXPECTED_NUM_THREADS_DEFAULT" => "$other_nthreads_default",
2064+
"EXPECTED_NUM_THREADS_INTERACTIVE" => "$other_nthreads_interactive",
2065+
) do
2066+
Pkg.test("TestThreads"; julia_args=`--threads=$other_nthreads_default,$other_nthreads_interactive`)
2067+
end
2068+
end
2069+
end
2070+
end
2071+
end
2072+
end
20172073
end
20182074

20192075
#

test/test_packages/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Manifest.toml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name = "TestThreads"
2+
uuid = "79df5fe7-ed23-44ca-b7b9-b3881e57664d"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module TestThreads
2+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@assert haskey(ENV, "EXPECTED_NUM_THREADS_DEFAULT")
2+
@assert haskey(ENV, "EXPECTED_NUM_THREADS_INTERACTIVE")
3+
EXPECTED_NUM_THREADS_DEFAULT = parse(Int, ENV["EXPECTED_NUM_THREADS_DEFAULT"])
4+
EXPECTED_NUM_THREADS_INTERACTIVE = parse(Int, ENV["EXPECTED_NUM_THREADS_INTERACTIVE"])
5+
@assert Threads.nthreads() == EXPECTED_NUM_THREADS_DEFAULT
6+
@assert Threads.nthreads(:default) == EXPECTED_NUM_THREADS_DEFAULT
7+
@assert Threads.nthreads(:interactive) == EXPECTED_NUM_THREADS_INTERACTIVE

0 commit comments

Comments
 (0)