Skip to content

Commit

Permalink
Add Channel() constr, defaults to unbufferred channel
Browse files Browse the repository at this point in the history
Add unit tests for default constructor
  • Loading branch information
NHDaly committed Jul 1, 2019
1 parent 9a017f1 commit 126ae69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Representation of a channel passing objects of type `T`.
abstract type AbstractChannel{T} end

"""
Channel{T}(sz::Int)
Channel{T}(sz::Int=0)
Constructs a `Channel` with an internal buffer that can hold a maximum of `sz` objects
of type `T`.
Expand All @@ -19,8 +19,12 @@ And vice-versa.
Other constructors:
* `Channel()`: default constructor, equivalent to `Channel{Any}(0)`
* `Channel(Inf)`: equivalent to `Channel{Any}(typemax(Int))`
* `Channel(sz)`: equivalent to `Channel{Any}(sz)`
!!! compat "Julia 1.3"
The default constructor `Channel()` was added in Julia 1.3.
"""
mutable struct Channel{T} <: AbstractChannel{T}
cond_take::Threads.Condition # waiting for data to become available
Expand Down Expand Up @@ -48,6 +52,7 @@ function Channel{T}(sz::Float64) where T
return Channel{T}(sz)
end
Channel(sz) = Channel{Any}(sz)
Channel() = Channel{Any}(0)

# special constructors
"""
Expand Down
8 changes: 7 additions & 1 deletion test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ using Random
end

@testset "various constructors" begin
c = Channel()
@test eltype(c) == Any

c = Channel(1)
@test eltype(c) == Any
@test put!(c, 1) == 1
Expand All @@ -31,7 +34,6 @@ end
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)
end
Expand All @@ -48,6 +50,10 @@ end
@test c.sz_max == 0
@test collect(c) == 1:100

c = Channel() do c; put!(1); put!("hi") end
@test c.sz_max == 0
@test collect(c) == [1, "hi"]

c = Channel{Int}(Inf) do c; put!(c,1); end
@test eltype(c) == Int
@test c.sz_max == typemax(Int)
Expand Down

0 comments on commit 126ae69

Please sign in to comment.