File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,14 @@ def count
17
17
@mutex . synchronize { @count }
18
18
end
19
19
20
+ def full?
21
+ @mutex . synchronize { @count == @buffer . size }
22
+ end
23
+
24
+ def empty?
25
+ @mutex . synchronize { @count == 0 }
26
+ end
27
+
20
28
def put ( value )
21
29
@mutex . synchronize do
22
30
wait_while_full
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ module Concurrent
7
7
let ( :capacity ) { 3 }
8
8
let ( :buffer ) { RingBuffer . new ( capacity ) }
9
9
10
+ def fill_buffer
11
+ capacity . times { buffer . put 3 }
12
+ end
13
+
10
14
describe '#capacity' do
11
15
it 'returns the value passed in constructor' do
12
16
buffer . capacity . should eq capacity
@@ -35,9 +39,31 @@ module Concurrent
35
39
end
36
40
end
37
41
42
+ describe '#empty?' do
43
+ it 'is true when count is zero' do
44
+ buffer . empty? . should be_true
45
+ end
46
+
47
+ it 'is false when count is not zero' do
48
+ buffer . put 82
49
+ buffer . empty? . should be_false
50
+ end
51
+ end
52
+
53
+ describe '#full?' do
54
+ it 'is true when count is capacity' do
55
+ fill_buffer
56
+ buffer . full? . should be_true
57
+ end
58
+
59
+ it 'is false when count is not capacity' do
60
+ buffer . full? . should be_false
61
+ end
62
+ end
63
+
38
64
describe '#put' do
39
65
it 'block when buffer is full' do
40
- capacity . times { buffer . put 27 }
66
+ fill_buffer
41
67
42
68
t = Thread . new { buffer . put 32 }
43
69
@@ -87,7 +113,7 @@ module Concurrent
87
113
88
114
context 'circular condition' do
89
115
it 'can filled many times' do
90
- capacity . times { buffer . put 3 }
116
+ fill_buffer
91
117
capacity . times { buffer . take }
92
118
93
119
buffer . put 'hi'
You can’t perform that action at this time.
0 commit comments