Skip to content

Commit cb2ed67

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

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/pkg.jl

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

366+
@time @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()) --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+
i = 0
381+
touch(tempname(raw\"$flag_start_dir\")) # file marker that first part has finished
382+
while length(readdir(raw\"$flag_start_dir\")) < $(Sys.CPU_THREADS)
383+
# sync all processes to start at the same time
384+
sleep(0.1)
385+
end
386+
t = Timer(15)
387+
while !isfile(raw\"$flag_end_file\") && (i <= 1000 || isopen(t))
388+
global i += 1
389+
try
390+
Pkg.Types.EnvCache()
391+
catch
392+
touch(raw\"$flag_end_file\")
393+
println(stderr, \"Errored after $i iterations\")
394+
rethrow()
395+
end
396+
end"`,
397+
stderr = iob, stdout = devnull))
398+
push!(tasks, t)
399+
push!(iobs, iob)
400+
end
401+
for i in eachindex(tasks)
402+
try
403+
fetch(tasks[i]) # If any of these failed it will throw when fetched
404+
catch
405+
print(String(take!(iobs[i])))
406+
break
407+
end
408+
end
409+
@test any(istaskfailed, tasks) == false
410+
end
411+
366412
@testset "adding nonexisting packages" begin
367413
nonexisting_pkg = randstring(14)
368414
@test_throws PkgError Pkg.add(nonexisting_pkg)

0 commit comments

Comments
 (0)