-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Channel constructor requires an explict size.
Move channel tests into its own file. Implement 0-sized channels.
- Loading branch information
1 parent
9f80eab
commit 7a24ca1
Showing
8 changed files
with
176 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
# Test various constructors | ||
c=Channel(1) | ||
@test eltype(c) == Any | ||
@test put!(c, 1) == 1 | ||
@test isready(c) == true | ||
@test take!(c) == 1 | ||
@test isready(c) == false | ||
|
||
@test eltype(Channel(1.0)) == Any | ||
|
||
c=Channel{Int}(1) | ||
@test eltype(c) == Int | ||
@test_throws MethodError put!(c, "Hello") | ||
|
||
c=Channel{Int}(Inf) | ||
@test eltype(c) == Int | ||
pvals = map(i->put!(c,i), 1:10^6) | ||
tvals = Int[take!(c) for i in 1:10^6] | ||
@test pvals == tvals | ||
|
||
@test_throws MethodError Channel() | ||
@test_throws ArgumentError Channel(-1) | ||
@test_throws InexactError Channel(1.5) | ||
|
||
# Test multiple concurrent put!/take! on a channel for different sizes | ||
function testcpt(sz) | ||
c = Channel{Int}(sz) | ||
size = 0 | ||
inc() = size += 1 | ||
dec() = size -= 1 | ||
@sync for i = 1:10^4 | ||
@async (sleep(rand()); put!(c, i); inc()) | ||
@async (sleep(rand()); take!(c); dec()) | ||
end | ||
@test size == 0 | ||
end | ||
testcpt(0) | ||
testcpt(1) | ||
testcpt(32) | ||
testcpt(Inf) | ||
|
||
# Test multiple "for" loops waiting on the same channel which | ||
# is closed after adding a few elements. | ||
c=Channel(32) | ||
results=[] | ||
@sync begin | ||
for i in 1:20 | ||
@async for i in c | ||
push!(results, i) | ||
end | ||
end | ||
sleep(1.0) | ||
for i in 1:5 | ||
put!(c,i) | ||
end | ||
close(c) | ||
end | ||
@test sum(results) == 15 | ||
|
||
# Testing timedwait on multiple channels | ||
@sync begin | ||
rr1 = Channel(1) | ||
rr2 = Channel(1) | ||
rr3 = Channel(1) | ||
|
||
callback() = all(map(isready, [rr1, rr2, rr3])) | ||
# precompile functions which will be tested for execution time | ||
@test !callback() | ||
@test timedwait(callback, 0.0) === :timed_out | ||
|
||
@async begin sleep(0.5); put!(rr1, :ok) end | ||
@async begin sleep(1.0); put!(rr2, :ok) end | ||
@async begin sleep(2.0); put!(rr3, :ok) end | ||
|
||
tic() | ||
timedwait(callback, 1.0) | ||
et=toq() | ||
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU | ||
try | ||
@test (et >= 1.0) && (et <= 1.5) | ||
@test !isready(rr3) | ||
catch | ||
warn("timedwait tests delayed. et=$et, isready(rr3)=$(isready(rr3))") | ||
end | ||
@test isready(rr1) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters