Skip to content

Commit d99a997

Browse files
committed
chore: concurrency tests for post with threads
Updated concurrency tests to do post requests too, and use `@spawn` along with `@async`.
1 parent afd04be commit d99a997

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

test/runtests.jl

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,25 +385,64 @@ include("setup.jl")
385385
@test_skip "concurrent requests flakey on Windows"
386386
else
387387
mine = Downloader()
388+
389+
delay = 2
390+
count = 100
391+
get_url = "$server/delay/$delay"
392+
post_url = "$server/post"
393+
post_data = "Hello, world!"
394+
395+
test_post_json = (downloader) -> begin
396+
_, json = request_json(post_url, input=IOBuffer(post_data), method="POST", downloader = downloader)
397+
@test json["url"] == post_url
398+
@test json["data"] == post_data
399+
end
400+
401+
test_get_json = (id, downloader) -> begin
402+
json = download_json("$get_url?id=$id", downloader = downloader)
403+
@test get(json["args"], "id", nothing) == ["$id"]
404+
end
405+
388406
for downloader in (nothing, mine)
389407
have_lsof = Sys.which("lsof") !== nothing
390408
count_tcp() = Base.count(x->contains("TCP",x), split(read(`lsof -p $(getpid())`, String), '\n'))
391409
if have_lsof
392410
n_tcp = count_tcp()
393411
end
394-
delay = 2
395-
count = 100
396-
url = "$server/delay/$delay"
397-
t = @elapsed @sync for id = 1:count
398-
@async begin
399-
json = download_json("$url?id=$id", downloader = downloader)
400-
@test get(json["args"], "id", nothing) == ["$id"]
412+
for method in (:get, :post)
413+
for use_threads in (false, true)
414+
@info("concurrent requests test",
415+
is_default_downloader=(downloader === nothing),
416+
method=method,
417+
get_url=get_url,
418+
thread_count=Base.Threads.nthreads(),
419+
use_threads=use_threads,
420+
)
421+
t = @elapsed @sync for id = 1:count
422+
if use_threads
423+
Base.Threads.@spawn begin
424+
if method == :get
425+
test_get_json(id, downloader)
426+
else
427+
test_post_json(downloader)
428+
end
429+
end
430+
else
431+
@async begin
432+
if method == :get
433+
test_get_json(id, downloader)
434+
else
435+
test_post_json(downloader)
436+
end
437+
end
438+
end
439+
end
440+
@test t < 0.9*count*delay
441+
if have_lsof
442+
@test n_tcp == count_tcp()
443+
end
401444
end
402445
end
403-
@test t < 0.9*count*delay
404-
if have_lsof
405-
@test n_tcp == count_tcp()
406-
end
407446
end
408447
end
409448
end

0 commit comments

Comments
 (0)