Skip to content

Commit c8a7836

Browse files
authored
Revert changes which broke reuse of workers (#105)
* Revert "Remove useless `workers` variable (#103)" This reverts commit 7f27702. * Revert "Slightly improve inferred types in `runtests` (#100)" This reverts commit 16f47d4. * Add a test to ensure workers are reused correctly
1 parent 7f27702 commit c8a7836

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/ParallelTestRunner.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -787,20 +787,21 @@ function runtests(mod::Module, args::ParsedArgs;
787787
sort!(tests, by = x -> -get(historical_durations, x, Inf))
788788

789789
# determine parallelism
790-
_jobs = something(args.jobs, default_njobs())
791-
jobs::Int = clamp(_jobs, 1, length(tests))
790+
jobs = something(args.jobs, default_njobs())
791+
jobs = clamp(jobs, 1, length(tests))
792792
println(stdout, "Running $jobs tests in parallel. If this is too many, specify the `--jobs=N` argument to the tests, or set the `JULIA_CPU_THREADS` environment variable.")
793793
nworkers = min(jobs, length(tests))
794+
workers = fill(nothing, nworkers)
794795

795796
t0 = time()
796797
results = []
797798
running_tests = Dict{String, Float64}() # test => start_time
798799
test_lock = ReentrantLock() # to protect crucial access to tests and running_tests
799800

800-
done = Ref(false)
801+
done = false
801802
function stop_work()
802-
if !done[]
803-
done[] = true
803+
if !done
804+
done = true
804805
for task in worker_tasks
805806
task == current_task() && continue
806807
Base.istaskdone(task) && continue
@@ -949,7 +950,7 @@ function runtests(mod::Module, args::ParsedArgs;
949950
end
950951

951952
# After a while, display a status line
952-
if !done[] && time() - t0 >= 5 && (got_message || (time() - last_status_update[] >= 1))
953+
if !done && time() - t0 >= 5 && (got_message || (time() - last_status_update[] >= 1))
953954
update_status()
954955
last_status_update[] = time()
955956
end
@@ -980,9 +981,9 @@ function runtests(mod::Module, args::ParsedArgs;
980981
#
981982

982983
worker_tasks = Task[]
983-
for _ in 1:nworkers
984+
for p in workers
984985
push!(worker_tasks, @async begin
985-
while !done[]
986+
while !done
986987
# get a test to run
987988
test, test_t0 = Base.@lock test_lock begin
988989
isempty(tests) && break
@@ -1000,11 +1001,12 @@ function runtests(mod::Module, args::ParsedArgs;
10001001
else
10011002
test_worker(test, init_worker_code)
10021003
end
1004+
if wrkr === nothing
1005+
wrkr = p
1006+
end
10031007
# if a worker failed, spawn a new one
1004-
p = if isnothing(wrkr) || !Malt.isrunning(wrkr)
1005-
wrkr = addworker(; init_worker_code, io_ctx.color)
1006-
else
1007-
nothing
1008+
if wrkr === nothing || !Malt.isrunning(wrkr)
1009+
wrkr = p = addworker(; init_worker_code, io_ctx.color)
10081010
end
10091011

10101012
# run the test
@@ -1073,7 +1075,7 @@ function runtests(mod::Module, args::ParsedArgs;
10731075
if any(istaskfailed, worker_tasks)
10741076
println(io_ctx.stderr, "\nCaught an error, stopping...")
10751077
break
1076-
elseif done[] || Base.@lock(test_lock, isempty(tests) && isempty(running_tests))
1078+
elseif done || Base.@lock(test_lock, isempty(tests) && isempty(running_tests))
10771079
break
10781080
end
10791081
sleep(1)

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,23 @@ end
382382
@test contains(str, "SUCCESS")
383383
end
384384

385+
@testset "reuse of workers" begin
386+
testsuite = Dict(
387+
"a" => :(),
388+
"b" => :(),
389+
"c" => :(),
390+
"d" => :(),
391+
"e" => :(),
392+
"f" => :(),
393+
)
394+
io = IOBuffer()
395+
ioc = IOContext(io, :color => true)
396+
old_id_counter = ParallelTestRunner.ID_COUNTER[]
397+
njobs = 1
398+
runtests(ParallelTestRunner, ["--jobs=$(njobs)"]; testsuite, stdout=ioc, stderr=ioc)
399+
str = String(take!(io))
400+
@test contains(str, "Running $(njobs) tests in parallel")
401+
@test ParallelTestRunner.ID_COUNTER[] == old_id_counter + njobs
402+
end
403+
385404
end

0 commit comments

Comments
 (0)