Skip to content

Commit d69145f

Browse files
committed
init_worker_code with custom test_worker
1 parent 83aa246 commit d69145f

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

docs/src/advanced.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ The `test_worker` function receives the test name and should return either:
166166
- A worker object (from [`addworker`](@ref)) for tests that need special configuration
167167
- `nothing` to use the default worker pool
168168

169+
!!! note
170+
If your test suite uses both a `test_worker` function and `init_worker_code` as described in a prior section,
171+
`test_worker` must also take in `init_worker_code` as a second argument. You are responsible for passing it to
172+
[`addworker`](@ref) if your `init_code` depends on any `init_worker_code` definitions.
173+
169174
## Custom Arguments
170175

171176
If your package needs to accept its own command-line arguments in addition to `ParallelTestRunner`'s options, use [`parse_args`](@ref) with custom flags:

src/ParallelTestRunner.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ Several keyword arguments are also supported:
686686
- `init_code`: Code use to initialize each test's sandbox module (e.g., import auxiliary
687687
packages, define constants, etc).
688688
- `init_worker_code`: Code use to initialize each worker. This is run only once per worker instead of once per test.
689-
- `test_worker`: Optional function that takes a test name and returns a specific worker.
689+
- `test_worker`: Optional function that takes a test name and `init_worker_code` if `init_worker_code` is defined and returns a specific worker.
690690
When returning `nothing`, the test will be assigned to any available default worker.
691691
- `stdout` and `stderr`: I/O streams to write to (default: `Base.stdout` and `Base.stderr`)
692692
@@ -996,11 +996,16 @@ function runtests(mod::Module, args::ParsedArgs;
996996
test, test_t0
997997
end
998998

999-
# if a worker failed, spawn a new one
1000-
wrkr = test_worker(test)
1001-
if wrkr === nothing
999+
# pass in init_worker_code to custom worker function if defined
1000+
wrkr = if init_worker_code == :()
1001+
test_worker(test)
1002+
else
1003+
test_worker(test, init_worker_code)
1004+
end
1005+
if wrkr === nothing
10021006
wrkr = p
10031007
end
1008+
# if a worker failed, spawn a new one
10041009
if wrkr === nothing || !Malt.isrunning(wrkr)
10051010
wrkr = p = addworker(; init_worker_code, io_ctx.color)
10061011
end

test/runtests.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,52 @@ end
134134
@test contains(str, "SUCCESS")
135135
end
136136

137+
@testset "custom worker with `init_worker_code`" begin
138+
init_worker_code = quote
139+
should_be_defined() = true
140+
end
141+
init_code = quote
142+
using Test
143+
import ..should_be_defined
144+
end
145+
function test_worker(name, init_worker_code)
146+
if name == "needs env var"
147+
return addworker(env = ["SPECIAL_ENV_VAR" => "42"]; init_worker_code)
148+
elseif name == "threads/2"
149+
return addworker(exeflags = ["--threads=2"]; init_worker_code)
150+
end
151+
return nothing
152+
end
153+
testsuite = Dict(
154+
"needs env var" => quote
155+
@test ENV["SPECIAL_ENV_VAR"] == "42"
156+
@test should_be_defined()
157+
end,
158+
"doesn't need env var" => quote
159+
@test !haskey(ENV, "SPECIAL_ENV_VAR")
160+
@test should_be_defined()
161+
end,
162+
"threads/1" => quote
163+
@test Base.Threads.nthreads() == 1
164+
@test should_be_defined()
165+
end,
166+
"threads/2" => quote
167+
@test Base.Threads.nthreads() == 2
168+
@test should_be_defined()
169+
end
170+
)
171+
172+
io = IOBuffer()
173+
runtests(ParallelTestRunner, ["--verbose"]; test_worker, init_code, init_worker_code, testsuite, stdout=io, stderr=io)
174+
175+
str = String(take!(io))
176+
@test contains(str, r"needs env var .+ started at")
177+
@test contains(str, r"doesn't need env var .+ started at")
178+
@test contains(str, r"threads/1 .+ started at")
179+
@test contains(str, r"threads/2 .+ started at")
180+
@test contains(str, "SUCCESS")
181+
end
182+
137183
@testset "failing test" begin
138184
testsuite = Dict(
139185
"failing test" => quote

0 commit comments

Comments
 (0)