Permalink
Browse files

Closing all the channels at the end of the sieve spec

  • Loading branch information...
1 parent be33831 commit c751f4d317593d0b8b0b846c63d231a14861d86f @nate nate committed Mar 12, 2012
Showing with 22 additions and 12 deletions.
  1. +22 −12 spec/examples/sieve_spec.rb
@@ -7,16 +7,18 @@
it "should work using Channel primitives" do
# send the sequence 2,3,4, ... to returned channel
- def generate
+ def generate(channels)
ch = channel!(:type => Integer)
+ channels << ch
go!{ i = 1; loop { ch << i+= 1} }
return ch
end
# filter out input values divisible by *prime*, send rest to returned channel
- def filter(in_channel, prime)
+ def filter(in_channel, prime, channels)
out = channel!(:type => Integer)
+ channels << out
go! do
loop do
@@ -28,15 +30,16 @@ def filter(in_channel, prime)
return out
end
- def sieve
+ def sieve(channels)
out = channel!(:type => Integer)
+ channels << out
go! do
- ch = generate
+ ch = generate(channels)
loop do
prime, _ = ch.receive
out << prime
- ch = filter(ch, prime)
+ ch = filter(ch, prime, channels)
end
end
@@ -46,8 +49,9 @@ def sieve
# run the sieve
n = 20
nth = false
+ channels = []
- primes = sieve
+ primes = sieve(channels)
result = []
if nth
@@ -66,13 +70,15 @@ def sieve
end
result.should == [2,3,5,7,11,13,17,19]
+ channels.each(&:close)
end
it "should work with Ruby blocks" do
# send the sequence 2,3,4, ... to returned channel
- generate = Proc.new do
+ generate = Proc.new do |channels|
ch = channel!(:type => Integer)
+ channels << ch
go! do
i = 1
@@ -83,8 +89,9 @@ def sieve
end
# filter out input values divisible by *prime*, send rest to returned channel
- filtr = Proc.new do |in_channel, prime|
+ filtr = Proc.new do |in_channel, prime, channels|
out = channel!(:type => Integer)
+ channels << out
go! do
loop do
@@ -96,16 +103,17 @@ def sieve
out
end
- sieve = Proc.new do
+ sieve = Proc.new do |channels|
out = channel!(:type => Integer)
+ channels << out
go! do
- ch = generate.call
+ ch = generate.call(channels)
loop do
prime, _ = ch.receive
out << prime
- ch = filtr.call(ch, prime)
+ ch = filtr.call(ch, prime, channels)
end
end
@@ -115,8 +123,9 @@ def sieve
# run the sieve
n = 20
nth = false
+ channels = []
- primes = sieve.call
+ primes = sieve.call(channels)
result = []
if nth
@@ -135,5 +144,6 @@ def sieve
end
result.should == [2,3,5,7,11,13,17,19]
+ channels.each(&:close)
end
end

0 comments on commit c751f4d

Please sign in to comment.