Skip to content

Commit 3530d91

Browse files
committed
Add test for checking long-running test is running alone
1 parent 1c9cdbc commit 3530d91

2 files changed

Lines changed: 56 additions & 31 deletions

File tree

test/runtests.jl

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ using Test
33

44
cd(@__DIR__)
55

6+
include(joinpath(@__DIR__, "utils.jl"))
7+
68
@testset "ParallelTestRunner" verbose=true begin
79

810
@testset "basic use" begin
@@ -401,35 +403,6 @@ end
401403
@test ParallelTestRunner.ID_COUNTER[] == old_id_counter + njobs
402404
end
403405

404-
# Count direct child processes of current process (for default-worker test).
405-
# Returns -1 if unsupported so the test can be skipped.
406-
function _count_child_pids()
407-
pid = getpid()
408-
if Sys.isunix() && !isnothing(Sys.which("ps"))
409-
pids = Int[]
410-
out = try
411-
# Suggested in <https://askubuntu.com/a/512872>.
412-
readchomp(`ps -o ppid= -o pid= -A`)
413-
catch
414-
return -1
415-
end
416-
lines = split(out, '\n')
417-
# The output of `ps` always contains `ps` itself because it's spawned by
418-
# the current process, so we subtract one to always exclude it.
419-
count = -1
420-
for line in lines
421-
m = match(r" *(\d+) +(\d+)", line)
422-
if !isnothing(m)
423-
if parse(Int, m[1]) == pid
424-
count += 1
425-
end
426-
end
427-
end
428-
return count
429-
else
430-
return -1
431-
end
432-
end
433406

434407
# Issue <https://github.com/JuliaTesting/ParallelTestRunner.jl/issues/106>.
435408
@testset "default workers stopped at end" begin
@@ -441,7 +414,17 @@ end
441414
"t3" => :(),
442415
"t4" => :(),
443416
"t5" => :(),
444-
"t6" => :(),
417+
"t6" => quote
418+
# Make this test run longer than the others so that it runs alone...
419+
sleep(5)
420+
children = _count_child_pids($(getpid()))
421+
# ...then check there's only one worker still running. WARNING: this test may be
422+
# flaky on very busy systems, if at this point some of the other tests are still
423+
# running, hope for the best.
424+
if children >= 0
425+
@test children == 1
426+
end
427+
end,
445428
)
446429
before = _count_child_pids()
447430
if before < 0
@@ -450,7 +433,19 @@ end
450433
else
451434
old_id_counter = ParallelTestRunner.ID_COUNTER[]
452435
njobs = 2
453-
runtests(ParallelTestRunner, ["--jobs=$(njobs)", "--verbose"]; testsuite, stdout=devnull, stderr=devnull)
436+
io = IOBuffer()
437+
ioc = IOContext(io, :color => true)
438+
try
439+
runtests(ParallelTestRunner, ["--jobs=$(njobs)", "--verbose"];
440+
testsuite, stdout=ioc, stderr=ioc, init_code=:(include($(joinpath(@__DIR__, "utils.jl")))))
441+
catch
442+
# Show output in case of failure, to help debugging.
443+
output = String(take!(io))
444+
printstyled(stderr, "Output of failed test >>>>>>>>>>>>>>>>>>>>\n", color=:red, bold=true)
445+
println(stderr, output)
446+
printstyled(stderr, "End of output <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", color=:red, bold=true)
447+
rethrow()
448+
end
454449
# Make sure we didn't spawn more workers than expected.
455450
@test ParallelTestRunner.ID_COUNTER[] == old_id_counter + njobs
456451
# Allow a moment for worker processes to exit

test/utils.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Count direct child processes of current process (for default-worker test).
2+
# Returns -1 if unsupported so the test can be skipped.
3+
function _count_child_pids(pid = getpid())
4+
if Sys.isunix() && !isnothing(Sys.which("ps"))
5+
pids = Int[]
6+
out = try
7+
# Suggested in <https://askubuntu.com/a/512872>.
8+
readchomp(`ps -o ppid= -o pid= -A`)
9+
catch
10+
return -1
11+
end
12+
lines = split(out, '\n')
13+
# The output of `ps` for the current process always contains `ps` itself
14+
# because it's spawned by the current process, in that case we subtract
15+
# one to always exclude it, otherwise if we're getting the number of
16+
# children of another process we start from 0.
17+
count = pid == getpid() : -1 : 0
18+
for line in lines
19+
m = match(r" *(\d+) +(\d+)", line)
20+
if !isnothing(m)
21+
if parse(Int, m[1]) == pid
22+
count += 1
23+
end
24+
end
25+
end
26+
return count
27+
else
28+
return -1
29+
end
30+
end

0 commit comments

Comments
 (0)