Skip to content

Commit

Permalink
test for no log print tearing
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Feb 17, 2025
1 parent 67d0925 commit 2db9213
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
43 changes: 37 additions & 6 deletions stdlib/Logging/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,45 @@ end
@test isempty(undoc)
end

@testset "thread safety" begin
cmd = `$(Base.julia_cmd()) -t4 $(joinpath(@__DIR__, "threads_exec.jl"))`
@testset "Logging when multithreaded" begin
n = 10000
cmd = `$(Base.julia_cmd()) -t4 --color=no $(joinpath(@__DIR__, "threads_exec.jl")) $n`
fname = tempname()
f = open(fname, "w")
redirect_stderr(f) do
success(run(cmd))
@testset "Thread safety" begin
f = open(fname, "w")
@test success(run(pipeline(cmd, stderr=f)))
close(f)
end

@testset "No tearing in log printing" begin
# Check for print tearing by verifying that each log entry starts and ends correctly
f = open(fname, "r")
entry_start = r"^┌ (Info|Warning|Error): iteration"
entry_end = r"^└ "

open_entries = 0
total_entries = 0
for line in eachline(fname)
starts = count(entry_start, line)
starts > 1 && error("Interleaved logs: Multiple log entries started on one line")
if starts == 1
open_entries += 1
total_entries += 1
end

ends = count(entry_end, line)
starts == 1 && ends == 1 && error("Interleaved logs: Log entry started and and another ended on one line")
ends > 1 && error("Interleaved logs: Multiple log entries ended on one line")
if ends == 1
open_entries -= 1
end

@test open_entries >= 0 # Ensure no mismatched log entries
end

@test open_entries == 0 # Ensure all entries closed properly
@test total_entries == n * 3 # Ensure all logs were printed (3 because @debug is hidden)
end
close(f)
end

end
3 changes: 2 additions & 1 deletion stdlib/Logging/test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ function test_threads_exec(n)
end
end

test_threads_exec(100000)
n = parse(Int, ARGS[1])
test_threads_exec(n)

0 comments on commit 2db9213

Please sign in to comment.