Skip to content

Commit 34a1a02

Browse files
NHDalygithub-actions[bot]lgoettgens
authored
test_persistent_tasks: Add an optional expr to run in the precompile package (#255)
* test_persistent_tasks: Add an optional `expr` to run in the precompile package Bump version to 0.8.4 * Add test of the new expr functionality * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply PR suggestions Co-authored-by: Lars Göttgens <lars.goettgens@rwth-aachen.de> * Apply suggestions from code review * consistency with docstring Co-authored-by: Lars Göttgens <lars.goettgens@rwth-aachen.de> * Bump version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Lars Göttgens <lars.goettgens@rwth-aachen.de>
1 parent 28d29dc commit 34a1a02

5 files changed

Lines changed: 57 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.8.4] - 2023-12-01
11+
12+
### Added
13+
14+
- `test_persistent_tasks` now accepts an optional `expr` to run in the precompile package. ([#255](https://github.com/JuliaTesting/Aqua.jl/pull/255))
15+
+ The `expr` option lets you test whether your precompile script leaves any dangling Tasks
16+
or Timers, which would make it unsafe to use as a dependency for downstream packages.
17+
1018

1119
## [0.8.3] - 2023-11-29
1220

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Aqua"
22
uuid = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
authors = ["Takafumi Arakaki <aka.tkf@gmail.com> and contributors"]
4-
version = "0.8.3"
4+
version = "0.8.4"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

docs/src/persistent_tasks.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ fails to precompile: `using PkgA` runs `PkgA.__init__()`, which
3232
leaves the `Timer` `Task` running, and that causes precompilation
3333
of `PkgB` to hang.
3434

35+
## Example with `expr`
36+
37+
You can test that an expression using your package finishes without leaving any persistent
38+
tasks by passing a quoted expression:
39+
40+
```julia
41+
Aqua.test_persistent_tasks(MyPackage, quote
42+
# Code to run after loading MyPackage
43+
server = MyPackage.start_server()
44+
MyPackage.stop_server!(server)
45+
end)
46+
```
47+
48+
Here is an example test with a dummy expr which will obviously fail, because it's explicitly
49+
spawning a Task that never dies.
50+
```@repl
51+
Aqua.test_persistent_tasks(Aqua, quote
52+
Threads.@spawn while true sleep(0.5) end
53+
end
54+
```
55+
3556
## How the test works
3657

3758
This test works by launching a Julia process that tries to precompile a

src/persistent_tasks.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Test whether loading `package` creates persistent `Task`s
55
which may block precompilation of dependent packages.
66
See also [`Aqua.find_persistent_tasks_deps`](@ref).
77
8+
If you provide an optional `expr`, this tests whether loading `package` and running `expr`
9+
creates persistent `Task`s. For example, you might start and shutdown a web server, and
10+
this will test that there aren't any persistent `Task`s.
11+
812
On Julia version 1.9 and before, this test always succeeds.
913
1014
# Arguments
@@ -16,6 +20,7 @@ On Julia version 1.9 and before, this test always succeeds.
1620
- `tmax::Real = 5`: the maximum time (in seconds) to wait after loading the
1721
package before forcibly shutting down the precompilation process (triggering
1822
a test failure).
23+
- `expr::Expr = quote end`: An expression to run in the precompile package.
1924
"""
2025
function test_persistent_tasks(package::PkgId; broken::Bool = false, kwargs...)
2126
if broken
@@ -29,10 +34,10 @@ function test_persistent_tasks(package::Module; kwargs...)
2934
test_persistent_tasks(PkgId(package); kwargs...)
3035
end
3136

32-
function has_persistent_tasks(package::PkgId; tmax = 10)
37+
function has_persistent_tasks(package::PkgId; expr::Expr = quote end, tmax = 10)
3338
root_project_path, found = root_project_toml(package)
3439
found || error("Unable to locate Project.toml")
35-
return !precompile_wrapper(root_project_path, tmax)
40+
return !precompile_wrapper(root_project_path, tmax, expr)
3641
end
3742

3843
"""
@@ -60,7 +65,7 @@ function find_persistent_tasks_deps(package::Module; kwargs...)
6065
find_persistent_tasks_deps(PkgId(package); kwargs...)
6166
end
6267

63-
function precompile_wrapper(project, tmax)
68+
function precompile_wrapper(project, tmax, expr)
6469
@static if VERSION < v"1.10.0-"
6570
return true
6671
end
@@ -84,6 +89,7 @@ function precompile_wrapper(project, tmax)
8489
"""
8590
module $wrappername
8691
using $pkgname
92+
$expr
8793
# Signal Aqua from the precompilation process that we've finished loading the package
8894
open("$(escape_string(statusfile))", "w") do io
8995
println(io, "done")
@@ -110,6 +116,8 @@ end
110116
end
111117
success = !process_running(proc)
112118
if !success
119+
# SIGKILL to prevent julia from printing the SIG 15 handler, which can
120+
# misleadingly look like it's caused by an issue in the user's program.
113121
kill(proc, Base.SIGKILL)
114122
end
115123
return success

test/test_persistent_tasks.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ end
2929
filter!(str -> !occursin("PersistentTasks", str), LOAD_PATH)
3030
end
3131

32+
@testset "test_persistent_tasks(expr)" begin
33+
if Base.VERSION >= v"1.10-"
34+
@test !Aqua.has_persistent_tasks(
35+
getid("TransientTask"),
36+
expr = quote
37+
fetch(Threads.@spawn nothing)
38+
end,
39+
)
40+
@test Aqua.has_persistent_tasks(getid("TransientTask"), expr = quote
41+
Threads.@spawn while true
42+
sleep(0.5)
43+
end
44+
end)
45+
end
46+
end
47+
3248
end

0 commit comments

Comments
 (0)