Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix breaking setproperty! introduced in #304 #309

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract type AbstractProgress end
# forward common core properties to main types
function Base.setproperty!(p::T, name::Symbol, value) where T<:AbstractProgress
if hasfield(T, name)
ty = fieldtype(T, name)
value = value isa ty ? value : convert(ty, value)
setfield!(p, name, value)
else
setproperty!(p.core, name, value)
Expand Down Expand Up @@ -874,11 +876,10 @@ function showprogress(args...)
elseif expr.head == :macrocall
macroname = expr.args[1]

if macroname in (Symbol("@distributed"), :(Distributed.@distributed).args[1])
# can be changed to `:(Distributed.var"@distributed")` if support for pre-1.3 is dropped
if macroname in (Symbol("@distributed"), :(Distributed.var"@distributed"))
return showprogressdistributed(args...)

elseif macroname in (Symbol("@threads"), :(Threads.@threads).args[1])
elseif macroname in (Symbol("@threads"), :(Threads.var"@threads"))
return showprogressthreads(args...)
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ wrap = ProgressMeter.ProgressWrapper(A, prog)
@test length(wrap) == length(A)
@test eltype(wrap) == eltype(A)
@test collect(wrap) == collect(A)

# Test setproperty! on ProgressCore
prog = Progress(10)
prog.desc = "New description" # in ProgressCore
@test prog.desc == "New description"
prog.n = UInt128(20) # in Progress
@test prog.n == 20
prog.offset = Int8(5) # in ProgressCore
@test prog.offset == 5
2 changes: 1 addition & 1 deletion test/test_float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ testfunc2(50, 0.2, 0.2, "progress ", 70)
println("Testing floating normal progress bars with changing offset")
function testfunc3(n, dt, tsleep, desc, barlen)
p1 = Progress(n; dt=dt, desc=desc, barlen=barlen, offset=0)
p2 = Progress(n; dt=dt, desc=desc, barlen=barlen,offset=1)
p2 = Progress(n; dt=dt, desc=desc, barlen=barlen, offset=1)
for i = 1:n
sleep(tsleep)
next!(p1; showvalues = [(:i, i) for _ in 1:i], keep=false)
Expand Down
43 changes: 20 additions & 23 deletions test/test_threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,29 @@
@test sum(vals) <= thresh
@test length(threadsUsed) == threads #Ensure that all threads are used

if (Threads.nthreads() > 1)
threads = Threads.nthreads() - 1
println("Testing Progress() with Threads.@spawn across $threads threads")
n = 20 #per thread
tasks = Vector{Task}(undef, threads)
# threadsUsed = fill(false, threads)
vals = ones(n*threads)
p = Progress(n*threads)
p.threads_used = 1:threads

@static if VERSION >= v"1.3.0-rc1" #Threads.@spawn not available before 1.3
if (Threads.nthreads() > 1)
threads = Threads.nthreads() - 1
println("Testing Progress() with Threads.@spawn across $threads threads")
n = 20 #per thread
tasks = Vector{Task}(undef, threads)
# threadsUsed = fill(false, threads)
vals = ones(n*threads)
p = Progress(n*threads)
p.threads_used = 1:threads

for t in 1:threads
tasks[t] = Threads.@spawn for i in 1:n
# threadsUsed[Threads.threadid()] = true
vals[(n*(t-1)) + i] = 0
sleep(0.05 + (rand()*0.1))
next!(p)
end
for t in 1:threads
tasks[t] = Threads.@spawn for i in 1:n
# threadsUsed[Threads.threadid()] = true
vals[(n*(t-1)) + i] = 0
sleep(0.05 + (rand()*0.1))
next!(p)
end
wait.(tasks)
@test !any(vals .== 1) #Check that all elements have been iterated
# @test all(threadsUsed) #Ensure that all threads are used (unreliable for @spawn)
else
@info "Threads.nthreads() == 1, so Threads.@spawn tests cannot be meaningfully tested"
end
wait.(tasks)
@test !any(vals .== 1) #Check that all elements have been iterated
# @test all(threadsUsed) #Ensure that all threads are used (unreliable for @spawn)
else
@info "Threads.nthreads() == 1, so Threads.@spawn tests cannot be meaningfully tested"
end

println("Testing @showprogress on a Threads.@threads for loop")
Expand Down
Loading