/
base_shared.rb
134 lines (107 loc) · 2.79 KB
/
base_shared.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
RSpec.shared_examples :channel_buffer do
specify do
expect(subject).to respond_to(:blocking?)
end
context '#capacity' do
specify { expect(subject.capacity).to be >= 0 }
end
context '#size' do
it 'returns zero upon initialization' do
expect(subject.size).to eq 0
end
end
context '#empty?' do
it 'returns true when empty' do
expect(subject).to be_empty
end
end
context '#full?' do
it 'returns false when not full' do
expect(subject).to_not be_full
end
end
context '#put' do
it 'does not enqueue the item when closed' do
subject.close
subject.put(:foo)
expect(subject).to be_empty
end
it 'returns false when closed' do
subject.close
expect(subject.put(:foo)).to be false
end
end
context '#offer' do
it 'returns true on success' do
subject # initialize on this thread
t = Thread.new do
subject.take
end
t.join(0.1)
expect(subject.offer(:foo)).to be true
end
it 'does not enqueue the item when closed' do
subject.close
subject.offer(:foo)
expect(subject).to be_empty
end
it 'returns false immediately when closed' do
subject.close
expect(subject.offer(:foo)).to be false
end
end
context '#take' do
it 'returns Concurrent::NULL when closed' do
subject.close
expect(subject.take).to eq Concurrent::NULL
end
end
context '#next' do
it 'returns Concurrent::NULL, false when closed' do
subject.close
item, more = subject.next
expect(item).to eq Concurrent::NULL
expect(more).to be false
end
end
context '#poll' do
it 'returns the next item immediately if available' do
subject # initialize on this thread
t = Thread.new do
subject.put(42)
end
t.join(0.1)
# TODO (pitr-ch 15-Oct-2016): fails on JRuby https://travis-ci.org/pitr-ch/concurrent-ruby/jobs/167937038
expect(subject.poll).to eq 42
end
it 'returns Concurrent::NULL immediately if no item is available' do
expect(subject.poll).to eq Concurrent::NULL
end
it 'returns Concurrent::NULL when closed' do
subject.close
expect(subject.poll).to eq Concurrent::NULL
end
end
context '#close' do
it 'sets #closed? to false' do
subject.close
expect(subject).to be_closed
end
it 'returns true when not previously closed' do
expect(subject.close).to be true
end
it 'returns false when already closed' do
subject.close
expect(subject.close).to be false
end
end
context '#closed?' do
it 'returns true when new' do
expect(subject).to_not be_closed
end
it 'returns false after #close' do
subject.close
expect(subject).to be_closed
end
end
end