Skip to content

Commit

Permalink
Add test for gracefully terminating multiple Julia processes
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Mar 8, 2024
1 parent 4e7ed3c commit 5097c6f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
91 changes: 67 additions & 24 deletions test/graceful_termination.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,74 @@
@testset "graceful_terminator / graceful_terminate" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator() do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
@testset "graceful_terminator" begin
@testset "Julia entrypoint" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator() do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
end
sleep(60)
end
sleep(60)

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer = IOBuffer()
p = run(pipeline(cmd; stdout=buffer, stderr=buffer); wait=false)
@test timedwait(() -> process_running(p), Second(5)) === :ok

# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)

# When no PID is passed in the process ID is read from the Julia entrypoint file.
# Blocks untils the process terminates.
@test graceful_terminate() === nothing

@test process_exited(p)
@test p.exitcode == 2

output = String(take!(buffer))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output == expected
end

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer = IOBuffer()
p = run(pipeline(cmd; stdout=buffer, stderr=buffer); wait=false)
@test timedwait(() -> process_running(p), Second(5)) === :ok
@testset "multiple Julia processes" begin
code = quote
using K8sDeputy
atexit(() -> @info "SHUTDOWN COMPLETE")
graceful_terminator(; set_entrypoint=false) do
@info "GRACEFUL TERMINATION HANDLER"
exit(2)
return nothing
end
sleep(60)
end

cmd = `$(Base.julia_cmd()) --color=no -e $code`
buffer1 = IOBuffer()
buffer2 = IOBuffer()
p1 = run(pipeline(cmd; stdout=buffer1, stderr=buffer1); wait=false)
p2 = run(pipeline(cmd; stdout=buffer2, stderr=buffer2); wait=false)
@test timedwait(() -> process_running(p1) && process_running(p2), Second(5)) === :ok

# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)
# Allow some time for Julia to startup and the graceful terminator to be registered.
sleep(3)

@test graceful_terminate(getpid(p)) === nothing # Blocks untils the HTTP server goes down
@test process_exited(p)
@test p.exitcode == 2
# Blocks untils the process terminates
@test graceful_terminate(getpid(p1)) === nothing
@test graceful_terminate(getpid(p2)) === nothing
@test process_exited(p1)
@test process_exited(p2)

output = String(take!(buffer))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output == expected
output1 = String(take!(buffer1))
output2 = String(take!(buffer2))
expected = """
[ Info: GRACEFUL TERMINATION HANDLER
[ Info: SHUTDOWN COMPLETE
"""
@test output1 == expected
@test output2 == expected
end
end
5 changes: 3 additions & 2 deletions test/health.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ end
atexit(() -> @info "SHUTDOWN COMPLETE")

deputy = Deputy(; shutdown_handler)
graceful_terminator() do
graceful_terminator(; set_entrypoint=false) do
@info "GRACEFUL TERMINATION HANDLER"
shutdown!(deputy)
return nothing
Expand All @@ -219,7 +219,8 @@ end
return r.status == 200
end === :ok

graceful_terminate(getpid(p)) # Blocks untils the HTTP server goes down
# Blocks untils the process terminates
graceful_terminate(getpid(p))
@test process_exited(p)
@test p.exitcode == 1

Expand Down

0 comments on commit 5097c6f

Please sign in to comment.