Skip to content

Commit 6867047

Browse files
committed
Simplify @lock syntax
We don't need to refer to the lock specifically, we can simplify refer to the lockable object. This also means we don't need to keep the lock objects around.
1 parent dddb1e3 commit 6867047

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

src/ParallelTestRunner.jl

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -934,16 +934,15 @@ function runtests(mod::Module, args::ParsedArgs;
934934
filter_tests!(testsuite, args)
935935

936936
# determine test order
937-
test_lock = ReentrantLock() # to protect crucial access to tests and running_tests
938-
tests = Lockable(collect(keys(testsuite)), test_lock)
939-
Random.shuffle!(@lock test_lock tests[])
937+
tests = Lockable(collect(keys(testsuite)))
938+
@lock tests Random.shuffle!(tests[])
940939
historical_durations = load_test_history(mod)
941-
sort!(@lock(test_lock, tests[]), by = x -> -get(historical_durations, x, Inf))
940+
@lock tests sort!(tests[], by = x -> -get(historical_durations, x, Inf))
942941

943942
# determine parallelism
944943
jobs = something(args.jobs, default_njobs())
945-
jobs = clamp(jobs, 1, length(@lock(test_lock, tests[])))
946-
println(stdout, "Running $(length(@lock(test_lock, tests[]))) tests using $jobs parallel jobs. If this is too many concurrent jobs, specify the `--jobs=N` argument to the tests, or set the `JULIA_CPU_THREADS` environment variable.")
944+
jobs = @lock tests clamp(jobs, 1, length(tests[]))
945+
@lock tests println(stdout, "Running $(length(tests[])) tests using $jobs parallel jobs. If this is too many concurrent jobs, specify the `--jobs=N` argument to the tests, or set the `JULIA_CPU_THREADS` environment variable.")
947946
!isnothing(args.verbose) && println(stdout, "Available memory: $(Base.format_bytes(available_memory()))")
948947
sem = Base.Semaphore(max(1, jobs))
949948
worker_pool = Channel{Union{Nothing, PTRWorker}}(jobs)
@@ -952,9 +951,8 @@ function runtests(mod::Module, args::ParsedArgs;
952951
end
953952

954953
t0 = time()
955-
results_lock = ReentrantLock() # to protect concurrent access to results
956-
results = Lockable([], results_lock)
957-
running_tests = Lockable(Dict{String, Float64}(), test_lock) # test => start_time
954+
results = Lockable([])
955+
running_tests = Lockable(Dict{String, Float64}()) # test => start_time
958956

959957
worker_tasks = Task[]
960958

@@ -978,7 +976,7 @@ function runtests(mod::Module, args::ParsedArgs;
978976
# pretty print information about gc and mem usage
979977
testgroupheader = "Test"
980978
workerheader = "(Worker)"
981-
name_align = @lock test_lock maximum(
979+
name_align = @lock tests maximum(
982980
[
983981
textwidth(testgroupheader) + textwidth(" ") + textwidth(workerheader);
984982
map(x -> textwidth(x) + 5, tests[])
@@ -1008,15 +1006,15 @@ function runtests(mod::Module, args::ParsedArgs;
10081006

10091007
function update_status()
10101008
# only draw if we have something to show
1011-
Base.@lock(test_lock, isempty(running_tests[])) && return
1012-
completed = Base.@lock results_lock length(results[])
1013-
total = @lock test_lock length(tests[])
1009+
@lock(running_tests, isempty(running_tests[])) && return
1010+
completed = @lock results length(results[])
1011+
total = @lock tests length(tests[])
10141012

10151013
# line 1: empty line
10161014
line1 = ""
10171015

10181016
# line 2: running tests
1019-
test_list = @lock test_lock sort(collect(keys(running_tests[])), by = x -> running_tests[][x])
1017+
test_list = @lock running_tests sort(collect(keys(running_tests[])), by = x -> running_tests[][x])
10201018
status_parts = map(test_list) do test
10211019
"$test"
10221020
end
@@ -1031,23 +1029,23 @@ function runtests(mod::Module, args::ParsedArgs;
10311029
line3 = "Progress: $completed/$total tests completed"
10321030
if completed > 0
10331031
# estimate per-test time (slightly pessimistic)
1034-
durations_done = Base.@lock results_lock [end_time - start_time for (_, _,_, start_time, end_time) in results[]]
1032+
durations_done = @lock results [end_time - start_time for (_, _,_, start_time, end_time) in results[]]
10351033
μ = mean(durations_done)
10361034
σ = length(durations_done) > 1 ? std(durations_done) : 0.0
10371035
est_per_test = μ + 0.5σ
10381036

10391037
est_remaining = 0.0
10401038
## currently-running
1041-
for (test, start_time) in @lock(test_lock, running_tests[])
1039+
for (test, start_time) in @lock(running_tests, running_tests[])
10421040
elapsed = time() - start_time
10431041
duration = get(historical_durations, test, est_per_test)
10441042
est_remaining += max(0.0, duration - elapsed)
10451043
end
10461044
## yet-to-run
1047-
for test in @lock(test_lock, tests[])
1048-
@lock(test_lock, haskey(running_tests[], test)) && continue
1045+
for test in @lock(tests, tests[])
1046+
@lock(running_tests, haskey(running_tests[], test)) && continue
10491047
# Test is in any completed test
1050-
Base.@lock(results_lock, any(r -> test == r.test, results[])) && continue
1048+
@lock(results, any(r -> test == r.test, results[])) && continue
10511049
est_remaining += get(historical_durations, test, est_per_test)
10521050
end
10531051

@@ -1130,7 +1128,7 @@ function runtests(mod::Module, args::ParsedArgs;
11301128
end
11311129
isa(ex, InterruptException) || rethrow()
11321130
finally
1133-
if @lock test_lock @lock results_lock isempty(running_tests[]) && length(results[]) >= length(tests[])
1131+
if @lock running_tests @lock results @lock tests isempty(running_tests[]) && length(results[]) >= length(tests[])
11341132
# XXX: only erase the status if we completed successfully.
11351133
# in other cases we'll have printed "caught interrupt"
11361134
clear_status()
@@ -1142,9 +1140,9 @@ function runtests(mod::Module, args::ParsedArgs;
11421140
# execution
11431141
#
11441142

1145-
tests_to_start = @lock test_lock Threads.Atomic{Int}(length(tests[]))
1143+
tests_to_start = @lock tests Threads.Atomic{Int}(length(tests[]))
11461144
try
1147-
@sync for test in @lock(test_lock, tests[])
1145+
@sync for test in @lock(tests, tests[])
11481146
push!(worker_tasks, Threads.@spawn begin
11491147
local p = nothing
11501148
acquired = false
@@ -1156,7 +1154,7 @@ function runtests(mod::Module, args::ParsedArgs;
11561154

11571155
done && return
11581156

1159-
test_t0 = Base.@lock test_lock begin
1157+
test_t0 = @lock running_tests begin
11601158
test_t0 = time()
11611159
running_tests[][test] = test_t0
11621160
end
@@ -1194,7 +1192,7 @@ function runtests(mod::Module, args::ParsedArgs;
11941192
end
11951193
test_t1 = time()
11961194
output = Base.@lock wrkr.io_lock String(take!(wrkr.io))
1197-
Base.@lock results_lock push!(results[], (; test, result, output, test_t0, test_t1))
1195+
@lock results push!(results[], (; test, result, output, test_t0, test_t1))
11981196

11991197
# act on the results
12001198
if result isa AbstractTestRecord
@@ -1227,7 +1225,7 @@ function runtests(mod::Module, args::ParsedArgs;
12271225
Malt.stop(wrkr)
12281226
end
12291227

1230-
Base.@lock test_lock begin
1228+
@lock running_tests begin
12311229
delete!(running_tests[], test)
12321230
end
12331231
catch ex
@@ -1284,7 +1282,7 @@ function runtests(mod::Module, args::ParsedArgs;
12841282
end
12851283

12861284
# print the output generated by each testset
1287-
for (testname, result, output, _start, _stop) in @lock(results_lock, results[])
1285+
for (testname, result, output, _start, _stop) in @lock(results, results[])
12881286
if !isempty(output)
12891287
print(io_ctx.stdout, "\nOutput generated during execution of '")
12901288
if result isa Exception || anynonpass(result[])
@@ -1342,7 +1340,7 @@ function runtests(mod::Module, args::ParsedArgs;
13421340
function collect_results()
13431341
with_testset(o_ts) do
13441342
completed_tests = Set{String}()
1345-
for (testname, result, _output, start, stop) in @lock(results_lock, results[])
1343+
for (testname, result, _output, start, stop) in @lock(results, results[])
13461344
push!(completed_tests, testname)
13471345

13481346
if result isa AbstractTestRecord
@@ -1364,7 +1362,7 @@ function runtests(mod::Module, args::ParsedArgs;
13641362
end
13651363

13661364
# mark remaining or running tests as interrupted
1367-
for test in @lock(test_lock, tests[])
1365+
for test in @lock(tests, tests[])
13681366
(test in completed_tests) && continue
13691367
testset = create_testset(test)
13701368
Test.record(testset, Test.Error(:test_interrupted, test, nothing, Base.ExceptionStack(NamedTuple[(;exception = "skipped", backtrace = [])]), LineNumberNode(1)))

0 commit comments

Comments
 (0)