@@ -50,8 +50,15 @@ module Concurrent
5050 end
5151
5252 describe '#broken?' do
53- it 'should not be broken when created'
54- it 'should not be broken when reset is called without waiting thread'
53+ it 'should not be broken when created' do
54+ barrier . broken? . should eq false
55+ end
56+
57+ it 'should not be broken when reset is called without waiting thread' do
58+ barrier . reset
59+ barrier . broken? . should eq false
60+ end
61+
5562 it 'should be broken when at least one thread timed out'
5663 it 'should be restored when reset is called'
5764 end
@@ -74,18 +81,61 @@ module Concurrent
7481 latch = CountDownLatch . new ( parties )
7582
7683 parties . times { Thread . new { barrier . wait ; latch . count_down } }
77- latch . wait ( 0.2 ) . should be_true
84+ latch . wait ( 0.1 ) . should be_true
7885 barrier . number_waiting . should eq 0
7986 end
8087
81- it 'executes the block'
88+ it 'returns true when released' do
89+ latch = CountDownLatch . new ( parties )
90+
91+ parties . times { Thread . new { latch . count_down if barrier . wait == true } }
92+ latch . wait ( 0.1 ) . should be_true
93+ end
94+
95+ it 'executes the block once' do
96+ counter = AtomicFixnum . new
97+ barrier = described_class . new ( parties ) { counter . increment }
98+
99+ latch = CountDownLatch . new ( parties )
100+
101+ parties . times { Thread . new { latch . count_down if barrier . wait == true } }
102+ latch . wait ( 0.1 ) . should be_true
103+
104+ counter . value . should eq 1
105+ end
82106 end
83107
84108 context 'with timeout' do
85- it 'should block the thread'
86- it 'should release all threads when their number matches the desired one'
87- it 'can return early and break the barrier'
88- it 'does not execute the block on timeout'
109+ context 'timeout not expiring' do
110+ it 'should block the thread' do
111+ t = Thread . new { barrier . wait ( 1 ) }
112+ sleep ( 0.1 )
113+
114+ t . status . should eq 'sleep'
115+ end
116+
117+ it 'should release all threads when their number matches the desired one' do
118+ latch = CountDownLatch . new ( parties )
119+
120+ parties . times { Thread . new { barrier . wait ( 1 ) ; latch . count_down } }
121+ latch . wait ( 0.2 ) . should be_true
122+ barrier . number_waiting . should eq 0
123+ end
124+
125+ it 'returns true when released' do
126+ latch = CountDownLatch . new ( parties )
127+
128+ parties . times { Thread . new { latch . count_down if barrier . wait ( 1 ) == true } }
129+ latch . wait ( 0.1 ) . should be_true
130+ end
131+ end
132+
133+ context 'timeout expiring' do
134+
135+ it 'returns false'
136+ it 'can return early and break the barrier'
137+ it 'does not execute the block on timeout'
138+ end
89139 end
90140 end
91141
0 commit comments