Skip to content

Commit 8eed463

Browse files
test for no log print tearing
1 parent 1c24eeb commit 8eed463

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

stdlib/Logging/test/runtests.jl

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,45 @@ end
306306
@test isempty(undoc)
307307
end
308308

309-
@testset "thread safety" begin
310-
cmd = `$(Base.julia_cmd()) -t4 $(joinpath(@__DIR__, "threads_exec.jl"))`
309+
@testset "Logging when multithreaded" begin
310+
n = 10000
311+
cmd = `$(Base.julia_cmd()) -t4 --color=no $(joinpath(@__DIR__, "threads_exec.jl")) $n`
311312
fname = tempname()
312-
f = open(fname, "w")
313-
redirect_stderr(f) do
314-
success(run(cmd))
313+
@testset "Thread safety" begin
314+
f = open(fname, "w")
315+
@test success(run(pipeline(cmd, stderr=f)))
316+
close(f)
317+
end
318+
319+
@testset "No tearing in log printing" begin
320+
# Check for print tearing by verifying that each log entry starts and ends correctly
321+
f = open(fname, "r")
322+
entry_start = r"^┌ (Info|Warning|Error): iteration"
323+
entry_end = r"^└ "
324+
325+
open_entries = 0
326+
total_entries = 0
327+
for line in eachline(fname)
328+
starts = count(entry_start, line)
329+
starts > 1 && error("Interleaved logs: Multiple log entries started on one line")
330+
if starts == 1
331+
open_entries += 1
332+
total_entries += 1
333+
end
334+
335+
ends = count(entry_end, line)
336+
starts == 1 && ends == 1 && error("Interleaved logs: Log entry started and and another ended on one line")
337+
ends > 1 && error("Interleaved logs: Multiple log entries ended on one line")
338+
if ends == 1
339+
open_entries -= 1
340+
end
341+
342+
@test open_entries >= 0 # Ensure no mismatched log entries
343+
end
344+
345+
@test open_entries == 0 # Ensure all entries closed properly
346+
@test total_entries == n * 3 # Ensure all logs were printed (3 because @debug is hidden)
315347
end
316-
close(f)
317348
end
318349

319350
end

stdlib/Logging/test/threads_exec.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ function test_threads_exec(n)
1010
end
1111
end
1212

13-
test_threads_exec(100000)
13+
n = parse(Int, ARGS[1])
14+
test_threads_exec(n)

0 commit comments

Comments
 (0)