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
|
require_relative 'buffered_shared'
module Concurrent::Channel::Buffer
RSpec.describe Buffered, edge: true, notravis: true do
let(:capacity) { 10 }
subject { described_class.new(capacity) }
it_behaves_like :channel_buffered_buffer
specify do
expect(subject).to be_blocking
end
context '#full?' do
it 'returns true when at max capacity' do
subject = described_class.new(1)
subject.put(:foo)
expect(subject).to be_full
end
end
context '#put' do
it 'blocks when at capacity until a thread is ready to take' do
subject = described_class.new(1)
subject.put(13)
bucket = Concurrent::AtomicReference.new(nil)
t = in_thread do
subject.put(42)
bucket.value = 42
end
t.join(0.1)
before = bucket.value
subject.take
t.join(0.1)
after = bucket.value
expect(before).to be nil
expect(after).to eq 42
expect(t.status).to be false
end
end
context '#offer' do
it 'returns false immediately when full' do
subject = described_class.new(1)
subject.put(:foo)
expect(subject.offer(:bar)).to be false
end
end
end
end
|