Skip to content
Browse files

Making it so the various calls withing selectors do not require blocks

  • Loading branch information...
1 parent f6c43a8 commit 6cd5773fc51455bea83379263aef88f53feb8a96 @nate nate committed Feb 20, 2013
Showing with 35 additions and 20 deletions.
  1. +0 −1 lib/agent/selector.rb
  2. +1 −1 spec/channel_spec.rb
  3. +34 −18 spec/selector_spec.rb
View
1 lib/agent/selector.rb
@@ -44,7 +44,6 @@ def timeout(t, &blk)
def case(chan, direction, value=nil, &blk)
raise "invalid case, must be a channel" unless chan.is_a?(Channel)
- raise Errors::BlockMissing if blk.nil? && direction == :receive
raise Errors::InvalidDirection if direction != :send && direction != :receive
add_case(chan, direction, value, &blk)
end
View
2 spec/channel_spec.rb
@@ -48,7 +48,7 @@
# timeout blocking receive calls
timed_out = false
select! do |s|
- s.case(c, :receive){}
+ s.case(c, :receive)
s.timeout(0.1){ timed_out = true }
end
timed_out.should == true
View
52 spec/selector_spec.rb
@@ -27,6 +27,23 @@
(Time.now.to_f - now).should be_within(0.05).of(0.1)
end
+ it "should not raise an error when a block is missing on default" do
+ lambda {
+ select! do |s|
+ s.default
+ end
+ }.should_not raise_error(Agent::Errors::BlockMissing)
+ end
+
+ it "should not raise an error when a block is missing on timeout" do
+ lambda {
+ select! do |s|
+ s.timeout(1)
+ s.default
+ end
+ }.should_not raise_error(Agent::Errors::BlockMissing)
+ end
+
context "with unbuffered channels" do
before do
@c = channel!(Integer)
@@ -38,28 +55,27 @@
it "should evaluate select statements top to bottom" do
select! do |s|
- s.case(@c, :send, 1) {}
- s.case(@c, :receive) {}
- s.default {}
+ s.case(@c, :send, 1)
+ s.case(@c, :receive)
+ s.default
s.cases.size.should == 3
end
end
- it "should raise an error when a block is missing on receive" do
+ it "should not raise an error when a block is missing on receive" do
lambda {
select! do |s|
s.case(@c, :receive)
+ s.default
end
- }.should raise_error(Agent::Errors::BlockMissing)
+ }.should_not raise_error(Agent::Errors::BlockMissing)
end
it "should not raise an error when a block is missing on send" do
lambda {
- go!{ @c.receive }
-
select! do |s|
s.case(@c, :send, 1)
- s.cases.size.should == 0
+ s.default
end
}.should_not raise_error(Agent::Errors::BlockMissing)
end
@@ -98,7 +114,7 @@
lambda {
select! do |s|
s.case(@c, :send, 1)
- s.case(@c, :receive){}
+ s.case(@c, :receive)
end
}.should raise_error(Agent::Errors::ChannelClosed)
end
@@ -168,7 +184,7 @@
go!{sleep(0.2); c.receive[0].should == 2 }
select! do |s|
- s.case(c, :send, 2) {}
+ s.case(c, :send, 2)
end
(Time.now.to_f - now).should be_within(0.1).of(0.2)
@@ -223,26 +239,26 @@
it "should evaluate select statements top to bottom" do
select! do |s|
- s.case(@c, :send, 1) {}
- s.case(@c, :receive) {}
+ s.case(@c, :send, 1)
+ s.case(@c, :receive)
s.cases.size.should == 2
end
end
- it "should raise an error when a block is missing on receive" do
+ it "should not raise an error when a block is missing on receive" do
lambda {
select! do |s|
s.case(@c, :receive)
- s.cases.size.should == 0
+ s.default
end
- }.should raise_error(Agent::Errors::BlockMissing)
+ }.should_not raise_error(Agent::Errors::BlockMissing)
end
it "should not raise an error when a block is missing on send" do
lambda {
select! do |s|
s.case(@c, :send, 1)
- s.cases.size.should == 0
+ s.default
end
}.should_not raise_error(Agent::Errors::BlockMissing)
end
@@ -283,7 +299,7 @@
lambda {
select! do |s|
s.case(@c, :send, 1)
- s.case(@c, :send, 2){}
+ s.case(@c, :send, 2)
end
}.should raise_error(Agent::Errors::ChannelClosed)
end
@@ -348,7 +364,7 @@
go!{sleep(0.2); c.receive }
select! do |s|
- s.case(c, :send, 2) {}
+ s.case(c, :send, 2)
end
c.receive[0].should == 2

0 comments on commit 6cd5773

Please sign in to comment.
Something went wrong with that request. Please try again.