Skip to content

Commit d7d5016

Browse files
committed
Factor out printing logic again.
1 parent b595e8c commit d7d5016

File tree

1 file changed

+82
-43
lines changed

1 file changed

+82
-43
lines changed

src/ParallelTestRunner.jl

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ function memory_usage(rec::TestRecord)
7676
return rec.rss
7777
end
7878

79+
80+
#
81+
# overridable I/O context for pretty-printing
82+
#
83+
7984
struct TestIOContext
8085
io::IO
8186
lock::ReentrantLock
@@ -100,23 +105,77 @@ function test_IOContext(::Type{TestRecord}, io::IO, lock::ReentrantLock, name_al
100105
)
101106
end
102107

103-
# Message types for the printer channel
104-
# (:started, test_name, worker_id)
105-
# (:finished, test_name, worker_id, record)
106-
# (:errored, test_name, worker_id)
107-
const printer_channel = Channel{Tuple}(100)
108-
109108
function print_header(::Type{TestRecord}, ctx::TestIOContext, testgroupheader, workerheader)
110-
printstyled(ctx.io, " "^(ctx.name_align + textwidth(testgroupheader) - 3), " | ")
111-
printstyled(ctx.io, " | ---------------- CPU ---------------- |\n", color = :white)
112-
printstyled(ctx.io, testgroupheader, color = :white)
113-
printstyled(ctx.io, lpad(workerheader, ctx.name_align - textwidth(testgroupheader) + 1), " | ", color = :white)
114-
printstyled(ctx.io, "Time (s) | GC (s) | GC % | Alloc (MB) | RSS (MB) |\n", color = :white)
115-
return nothing
109+
lock(ctx.lock)
110+
try
111+
printstyled(ctx.io, " "^(ctx.name_align + textwidth(testgroupheader) - 3), " | ")
112+
printstyled(ctx.io, " | ---------------- CPU ---------------- |\n", color = :white)
113+
printstyled(ctx.io, testgroupheader, color = :white)
114+
printstyled(ctx.io, lpad(workerheader, ctx.name_align - textwidth(testgroupheader) + 1), " | ", color = :white)
115+
printstyled(ctx.io, "Time (s) | GC (s) | GC % | Alloc (MB) | RSS (MB) |\n", color = :white)
116+
flush(ctx.io)
117+
finally
118+
unlock(ctx.lock)
119+
end
120+
end
121+
122+
function print_test_started(::Type{TestRecord}, wrkr, test, ctx::TestIOContext)
123+
lock(ctx.lock)
124+
try
125+
printstyled(test, color = :white)
126+
printstyled(
127+
lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), " |",
128+
" "^ctx.elapsed_align, "started at $(now())\n", color = :white
129+
)
130+
flush(ctx.io)
131+
finally
132+
unlock(ctx.lock)
133+
end
134+
end
135+
136+
function print_test_finished(test, wrkr, record::TestRecord, ctx::TestIOContext)
137+
lock(ctx.lock)
138+
try
139+
printstyled(ctx.io, test, color = :white)
140+
printstyled(ctx.io, lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), " | ", color = :white)
141+
time_str = @sprintf("%7.2f", record.time)
142+
printstyled(ctx.io, lpad(time_str, ctx.elapsed_align, " "), " | ", color = :white)
143+
144+
gc_str = @sprintf("%5.2f", record.gctime)
145+
printstyled(ctx.io, lpad(gc_str, ctx.gc_align, " "), " | ", color = :white)
146+
percent_str = @sprintf("%4.1f", 100 * record.gctime / record.time)
147+
printstyled(ctx.io, lpad(percent_str, ctx.percent_align, " "), " | ", color = :white)
148+
alloc_str = @sprintf("%5.2f", record.bytes / 2^20)
149+
printstyled(ctx.io, lpad(alloc_str, ctx.alloc_align, " "), " | ", color = :white)
150+
151+
rss_str = @sprintf("%5.2f", record.rss / 2^20)
152+
printstyled(ctx.io, lpad(rss_str, ctx.rss_align, " "), " |\n", color = :white)
153+
154+
flush(ctx.io)
155+
finally
156+
unlock(ctx.lock)
157+
end
158+
end
159+
160+
function print_test_errorred(::Type{TestRecord}, wrkr, test, ctx::TestIOContext)
161+
lock(ctx.lock)
162+
try
163+
printstyled(test, color = :red)
164+
printstyled(
165+
lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), " |",
166+
" "^ctx.elapsed_align, " failed at $(now())\n", color = :red
167+
)
168+
169+
flush(ctx.io)
170+
finally
171+
unlock(ctx.lock)
172+
end
116173
end
117174

118175

119-
## entry point
176+
#
177+
# entry point
178+
#
120179

121180
function runtest(::Type{TestRecord}, f, name, init_code)
122181
function inner()
@@ -526,6 +585,12 @@ function runtests(ARGS; testfilter = Returns(true), RecordType = TestRecord,
526585
status_lines_visible[] = 3
527586
end
528587

588+
# Message types for the printer channel
589+
# (:started, test_name, worker_id)
590+
# (:finished, test_name, worker_id, record)
591+
# (:errored, test_name, worker_id)
592+
printer_channel = Channel{Tuple}(100)
593+
529594
push!(tasks, @async begin
530595
last_status_update = Ref(now())
531596
try
@@ -544,47 +609,21 @@ function runtests(ARGS; testfilter = Returns(true), RecordType = TestRecord,
544609

545610
# Optionally print verbose started message
546611
if do_verbose
547-
printstyled(test_name, color = :white)
548-
printstyled(
549-
lpad("($wrkr)", name_align - textwidth(test_name) + 1, " "), " |",
550-
" "^elapsed_align, "started at $(now())\n", color = :white
551-
)
552-
flush(stdout)
612+
clear_status()
613+
print_test_started(RecordType, wrkr, test_name, io_ctx)
553614
end
554615

555616
elseif msg_type == :finished
556617
test_name, wrkr, record = msg[2], msg[3], msg[4]
557618

558-
# Clear status lines before printing result
559619
clear_status()
560-
561-
# Print test result
562-
printstyled(test_name, color = :white)
563-
printstyled(stdout, lpad("($wrkr)", name_align - textwidth(test_name) + 1, " "), " | ", color = :white)
564-
time_str = @sprintf("%7.2f", record.time)
565-
printstyled(stdout, lpad(time_str, elapsed_align, " "), " | ", color = :white)
566-
gc_str = @sprintf("%5.2f", record.gctime)
567-
printstyled(stdout, lpad(gc_str, io_ctx.gc_align, " "), " | ", color = :white)
568-
percent_str = @sprintf("%4.1f", 100 * record.gctime / record.time)
569-
printstyled(stdout, lpad(percent_str, io_ctx.percent_align, " "), " | ", color = :white)
570-
alloc_str = @sprintf("%5.2f", record.bytes / 2^20)
571-
printstyled(stdout, lpad(alloc_str, io_ctx.alloc_align, " "), " | ", color = :white)
572-
rss_str = @sprintf("%5.2f", record.rss / 2^20)
573-
printstyled(stdout, lpad(rss_str, io_ctx.rss_align, " "), " |\n", color = :white)
574-
flush(stdout)
620+
print_test_finished(test_name, wrkr, record, io_ctx)
575621

576622
elseif msg_type == :errored
577623
test_name, wrkr = msg[2], msg[3]
578624

579-
# Clear status lines before printing error
580625
clear_status()
581-
582-
printstyled(test_name, color = :red)
583-
printstyled(
584-
lpad("($wrkr)", name_align - textwidth(test_name) + 1, " "), " |",
585-
" "^elapsed_align, " failed at $(now())\n", color = :red
586-
)
587-
flush(stdout)
626+
print_test_errorred(RecordType, wrkr, test_name, io_ctx)
588627
end
589628
end
590629

0 commit comments

Comments
 (0)