Skip to content

Commit

Permalink
Test suite: Only run the c1,c2 hostname test in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
DilumAluthge committed Jan 20, 2025
1 parent 83fe844 commit 21e033c
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions test/script.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,77 @@
# We don't use `using Foo` here.
# We either use `using Foo: hello, world`, or we use `import Foo`.
# https://github.com/JuliaLang/julia/pull/42080
using Distributed: addprocs, workers, nworkers, remotecall_fetch
using Distributed: addprocs, workers, nworkers, remotecall_fetch, @everywhere
using SlurmClusterManager: SlurmManager
using Test: @testset, @test

addprocs(SlurmManager())

# We intentionally do not use `@assert` here.
# In a future minor release of Julia, `@assert`s may be disabled by default.
const SLURM_NTASKS = parse(Int, ENV["SLURM_NTASKS"])
if nworkers() != SLURM_NTASKS
msg = "Test failed: nworkers=$(nworkers()) does not match SLURM_NTASKS=$(SLURM_NTASKS)"
error(msg)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Instructions:
# To run tests outside of CI, set e.g.
# `export JULIA_SLURMCLUSTERMANAGER_IS_CI=false`
# in your Bash session before you launch the Slurm job.
function is_ci()
name = "JULIA_SLURMCLUSTERMANAGER_IS_CI"

# We intentionally default to true.
# This allows things to work in our CI (which is inside of Docker).
default_value = "true"

value_str = strip(get(ENV, name, default_value))
value_b = parse(Bool, value_str)
return value_b
end

function get_SLURM_NTASKS()
return parse(Int, ENV["SLURM_NTASKS"])
end

const hosts = map(workers()) do id
remotecall_fetch(() -> gethostname(), id)
function get_observed_hosts()
observed_hosts = map(workers()) do id
remotecall_fetch(() -> gethostname(), id)
end
sort!(observed_hosts)
return observed_hosts
end
sort!(hosts)
println("List of hosts: ", hosts)

# We don't use `@assert` here, for reason described above.
if hosts != ["c1", "c1", "c2", "c2"]
msg = "Test failed: observed_hosts=$(hosts) does not match expected_hosts=[c1, c1, c2, c2]"
error(msg)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# We intentionally do not use `@assert` here.
# In a future minor release of Julia, `@assert`s may be disabled by default.
#
# So instead we use `@test`.
@testset "SlurmClusterManager/script.jl" begin
@show Distributed.nworkers()
@show get_SLURM_NTASKS()
@show get_observed_hosts()

@testset "Number of workers" begin
@test Distributed.nworkers() == get_SLURM_NTASKS()
@test length(Distributed.workers()) == get_SLURM_NTASKS()
@test length(get_observed_hosts()) == get_SLURM_NTASKS()
end

@testset "Hostnames of workers" begin
if is_ci()
@info "This is CI, so we will perform the hostname test"
@test get_observed_hosts() == ["c1", "c1", "c2", "c2"]
else
# The specific hostnames of c1 and c2 are specific to the CI setup.
# We don't expect people to have the same hostnames if they run the test suite locally.
@warn "This is not CI, so we will skip the hostname test"
@test_skip false
end
end

@everywhere import Distributed

# Workers report in:
@everywhere println("Host $(Distributed.myid()): $(gethostname())")
end

0 comments on commit 21e033c

Please sign in to comment.