From 5a5fe27015635c3567069167f81ee9d07f1f068d Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 17 Feb 2025 17:34:54 -0500 Subject: [PATCH] test for no log print tearing --- stdlib/Logging/test/runtests.jl | 43 +++++++++++++++++++++++++---- stdlib/Logging/test/threads_exec.jl | 3 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/stdlib/Logging/test/runtests.jl b/stdlib/Logging/test/runtests.jl index 5e54d7b837da5..778e7ef058438 100644 --- a/stdlib/Logging/test/runtests.jl +++ b/stdlib/Logging/test/runtests.jl @@ -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 diff --git a/stdlib/Logging/test/threads_exec.jl b/stdlib/Logging/test/threads_exec.jl index 853dea832aff2..254840babaae0 100644 --- a/stdlib/Logging/test/threads_exec.jl +++ b/stdlib/Logging/test/threads_exec.jl @@ -10,4 +10,5 @@ function test_threads_exec(n) end end -test_threads_exec(100000) +n = parse(Int, ARGS[1]) +test_threads_exec(n)