Skip to content

Commit

Permalink
test_persistent_tasks: Add an optional expr to run in the precompil…
Browse files Browse the repository at this point in the history
…e package
  • Loading branch information
NHDaly committed Nov 29, 2023
1 parent e9a4230 commit d0cca64
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/persistent_tasks.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
Aqua.test_persistent_tasks(package)
Aqua.test_persistent_tasks(package, [expr])
Test whether loading `package` creates persistent `Task`s
which may block precompilation of dependent packages.
See also [`Aqua.find_persistent_tasks_deps`](@ref).
If you provide an optional `expr`, this tests whether loading `package` and running `expr`
creates persistent `Task`s. For example, you might start and shutdown a web server, and
this will test that there aren't any persistent `Task`s.
# Motivation
Julia 1.10 and higher wait for all running `Task`s to finish
Expand Down Expand Up @@ -37,6 +41,18 @@ fails to precompile: `using PkgA` runs `PkgA.__init__()`, which
leaves the `Timer` `Task` running, and that causes precompilation
of `PkgB` to hang.
# Example invocations
```julia
Aqua.test_persistent_tasks(MyPackage)
Aqua.test_persistent_tasks(MyPackage, quote
# Code to run after loading MyPackage
server = MyPackage.start_server()
MyPackage.stop_server!(server)
end)
```
# How the test works
This test works by launching a Julia process that tries to precompile a
Expand Down Expand Up @@ -66,6 +82,7 @@ On Julia version 1.9 and before, this test always succeeds.
# Arguments
- `package`: a top-level `Module` or `Base.PkgId`.
- `expr = nothing`: An expression to run in the precompile package.
# Keyword Arguments
- `broken::Bool = false`: If true, it uses `@test_broken` instead of
Expand All @@ -74,22 +91,22 @@ On Julia version 1.9 and before, this test always succeeds.
package before forcibly shutting down the precompilation process (triggering
a test failure).
"""
function test_persistent_tasks(package::PkgId; broken::Bool = false, kwargs...)
function test_persistent_tasks(package::PkgId, expr=nothing; broken::Bool = false, kwargs...)
if broken
@test_broken !has_persistent_tasks(package; kwargs...)
@test_broken !has_persistent_tasks(package, expr; kwargs...)

Check warning on line 96 in src/persistent_tasks.jl

View check run for this annotation

Codecov / codecov/patch

src/persistent_tasks.jl#L96

Added line #L96 was not covered by tests
else
@test !has_persistent_tasks(package; kwargs...)
@test !has_persistent_tasks(package, expr; kwargs...)
end
end

function test_persistent_tasks(package::Module; kwargs...)
test_persistent_tasks(PkgId(package); kwargs...)
function test_persistent_tasks(package::Module, expr=nothing; kwargs...)
test_persistent_tasks(PkgId(package), expr; kwargs...)
end

function has_persistent_tasks(package::PkgId; tmax = 10)
function has_persistent_tasks(package::PkgId, expr=nothing; tmax = 10)
root_project_path, found = root_project_toml(package)
found || error("Unable to locate Project.toml")
return !precompile_wrapper(root_project_path, tmax)
return !precompile_wrapper(root_project_path, tmax, expr)
end

"""
Expand Down Expand Up @@ -117,7 +134,7 @@ function find_persistent_tasks_deps(package::Module; kwargs...)
find_persistent_tasks_deps(PkgId(package); kwargs...)
end

function precompile_wrapper(project, tmax)
function precompile_wrapper(project, tmax, expr)
if VERSION < v"1.10.0-"
return true
end
Expand All @@ -141,6 +158,7 @@ function precompile_wrapper(project, tmax)
"""
module $wrappername
using $pkgname
$expr
# Signal Aqua from the precompilation process that we've finished loading the package
open("$(escape_string(statusfile))", "w") do io
println(io, "done")
Expand Down

0 comments on commit d0cca64

Please sign in to comment.