Skip to content

Commit f1fba33

Browse files
add atomic write usage file test
1 parent e1ee0a7 commit f1fba33

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/pkg.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,56 @@ temp_pkg_dir() do project_path
363363
@test any(x -> startswith(x, manifest), keys(usage))
364364
end
365365

366+
@testset "test atomicity of write_env_usage with $(Sys.CPU_THREADS) parallel processes" begin
367+
tasks = Task[]
368+
iobs = IOBuffer[]
369+
Sys.CPU_THREADS == 1 && error("Cannot test for atomic usage log file interaction effectively with only Sys.CPU_THREADS=1")
370+
run(`$(Base.julia_cmd()) --project="$(pkgdir(Pkg))" -e "import Pkg"`) # to precompile Pkg given we're in a different depot
371+
flag_start_dir = tempdir() # once n=Sys.CPU_THREADS files are in here, the processes can proceed to the concurrent test
372+
flag_end_file = tempname() # use creating this file as a way to stop the processes early if an error happens
373+
for i in 1:Sys.CPU_THREADS
374+
iob = IOBuffer()
375+
t = @async run(pipeline(`$(Base.julia_cmd()[1]) --project="$(pkgdir(Pkg))"
376+
-e "import Pkg;
377+
Pkg.UPDATED_REGISTRY_THIS_SESSION[] = true;
378+
Pkg.activate(temp = true);
379+
Pkg.add(\"Random\", io = devnull);
380+
touch(tempname(raw\"$flag_start_dir\")) # file marker that first part has finished
381+
while length(readdir(raw\"$flag_start_dir\")) < $(Sys.CPU_THREADS)
382+
# sync all processes to start at the same time
383+
sleep(0.1)
384+
end
385+
@async begin
386+
sleep(15)
387+
touch(raw\"$flag_end_file\")
388+
end
389+
i = 0
390+
while !isfile(raw\"$flag_end_file\")
391+
global i += 1
392+
try
393+
Pkg.Types.EnvCache()
394+
catch
395+
touch(raw\"$flag_end_file\")
396+
println(stderr, \"Errored after $i iterations\")
397+
rethrow()
398+
end
399+
yield()
400+
end"`,
401+
stderr = iob, stdout = devnull))
402+
push!(tasks, t)
403+
push!(iobs, iob)
404+
end
405+
for i in eachindex(tasks)
406+
try
407+
fetch(tasks[i]) # If any of these failed it will throw when fetched
408+
catch
409+
print(String(take!(iobs[i])))
410+
break
411+
end
412+
end
413+
@test any(istaskfailed, tasks) == false
414+
end
415+
366416
@testset "adding nonexisting packages" begin
367417
nonexisting_pkg = randstring(14)
368418
@test_throws PkgError Pkg.add(nonexisting_pkg)

0 commit comments

Comments
 (0)