Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select: fix case when select send executed before select receive, fixed #3862 #3899

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions spec/std/concurrent/select_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require "spec"

describe "select" do
it "select many receviers" do
ch1 = Channel(Int32).new
ch2 = Channel(Int32).new
res = [] of Int32
spawn do
10.times do |i|
(i % 2 == 0) ? ch1.send(i) : ch2.send(i)
end
end

10.times do
select
when x = ch1.receive
res << x
when y = ch2.receive
res << y
end
end
res.should eq (0...10).to_a
end

it "select many senders" do
ch1 = Channel(Int32).new
ch2 = Channel(Int32).new
res = [] of Int32
spawn do
5.times { res << ch1.receive }
end

spawn do
5.times { res << ch2.receive }
end

10.times do |i|
select
when ch1.send(i)
when ch2.send(i)
end
end
res.should eq (0...10).to_a
end

it "select many receivers, senders" do
ch1 = Channel(Int32).new
ch2 = Channel(Int32).new
res = [] of Int32
spawn do
10.times do |i|
select
when x = ch1.receive
res << x
when ch2.send(i)
end
end
end

10.times do |i|
select
when ch1.send(i)
when y = ch2.receive
res << y
end
end
res.should eq (0...10).to_a
end

it "select should work with send which started before receive, fixed #3862" do
ch1 = Channel(Int32).new
ch2 = Channel(Int32).new

spawn do
select
when ch1.send(1)
when ch2.receive
end
end

x = nil

spawn do
select
when a = ch1.receive
x = a
when b = ch2.receive
x = b
end
end

Fiber.yield

x.should eq 1
end
end
2 changes: 1 addition & 1 deletion src/concurrent/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class Channel::Unbuffered(T) < Channel(T)
end

def empty?
!@has_value
!@has_value && @senders.empty?
end

def full?
Expand Down