Skip to content

Commit abdcd1b

Browse files
committed
select operation on a channel list
1 parent 6563469 commit abdcd1b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/concurrent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
require 'concurrent/utilities'
3434

3535
require 'concurrent/channel/probe'
36+
require 'concurrent/channel/channel'
3637
require 'concurrent/channel/unbuffered_channel'
3738

3839
require 'concurrent/cached_thread_pool'

lib/concurrent/channel/channel.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Concurrent
2+
class Channel
3+
def self.select(*channels)
4+
probe = Probe.new
5+
6+
channels.each { |channel| channel.select(probe) }
7+
probe.value
8+
end
9+
end
10+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'spec_helper'
2+
3+
module Concurrent
4+
5+
describe Channel do
6+
7+
describe '.select' do
8+
9+
context 'without timeout' do
10+
it 'returns the first value available on a channel' do
11+
channels = [ UnbufferedChannel.new, UnbufferedChannel.new]
12+
13+
Thread.new { channels[1].push 77 }
14+
15+
value = Channel.select(*channels)
16+
17+
value.should eq 77
18+
end
19+
end
20+
21+
end
22+
23+
end
24+
end

0 commit comments

Comments
 (0)