Skip to content

Commit 51f7a68

Browse files
committed
Add test for IfAction
1 parent e4fe20e commit 51f7a68

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

test/logging.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ end
7878
end
7979
end
8080
end
81+
82+
@testset "IfAction only logs on even iterations" begin
83+
problem = LogDummyProblem()
84+
algorithm = LogDummyAlgorithm(StopAfterIteration(4))
85+
86+
# Callback that logs the iteration
87+
iter_logger = CallbackAction() do problem, algorithm, state
88+
@info "Even Iter $(state.iteration)"
89+
end
90+
91+
# Predicate: only log on even iterations
92+
even_predicate = (problem, algorithm, state; kwargs...) -> state.iteration % 2 == 0
93+
if_logger = IfAction(even_predicate, iter_logger)
94+
95+
# Expect logs only for iterations 2 and 4
96+
@test_logs (:info, "Even Iter 2") (:info, "Even Iter 4") begin
97+
with_algorithmlogger(:PostStep => if_logger) do
98+
solve(problem, algorithm)
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)