2014-10-23 07:37:53 -04:00
|
|
|
RSpec.describe Mutant::Actor::Receiver do
|
2014-12-05 23:34:04 -05:00
|
|
|
let(:messages) { double('Messages') }
|
|
|
|
let(:mutex) { double('Mutex') }
|
|
|
|
let(:condition_variable) { double('Condition Variable') }
|
|
|
|
let(:message) { double('Message') }
|
2014-10-23 07:37:53 -04:00
|
|
|
|
2014-12-05 23:34:04 -05:00
|
|
|
let(:object) { described_class.new(condition_variable, mutex, messages) }
|
2014-10-23 07:37:53 -04:00
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
subject { object.call }
|
|
|
|
|
2014-12-05 23:34:04 -05:00
|
|
|
context 'when messages contains a message' do
|
2014-10-23 07:37:53 -04:00
|
|
|
before do
|
2014-12-05 23:34:04 -05:00
|
|
|
expect(mutex).to receive(:synchronize).and_yield.ordered
|
|
|
|
expect(messages).to receive(:empty?).and_return(false).ordered
|
|
|
|
expect(messages).to receive(:shift).and_return(message).ordered
|
2014-10-23 07:37:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { should be(message) }
|
|
|
|
end
|
|
|
|
|
2014-12-05 23:34:04 -05:00
|
|
|
context 'when messages initially contains no message' do
|
2014-10-23 07:37:53 -04:00
|
|
|
before do
|
|
|
|
# 1rst failing try
|
2014-12-05 23:34:04 -05:00
|
|
|
expect(mutex).to receive(:synchronize).and_yield.ordered
|
|
|
|
expect(messages).to receive(:empty?).and_return(true).ordered
|
|
|
|
expect(condition_variable).to receive(:wait).with(mutex).ordered
|
2014-10-23 07:37:53 -04:00
|
|
|
# 2nd successful try
|
2014-12-05 23:34:04 -05:00
|
|
|
expect(mutex).to receive(:synchronize).and_yield.ordered
|
|
|
|
expect(messages).to receive(:empty?).and_return(false).ordered
|
|
|
|
expect(messages).to receive(:shift).and_return(message).ordered
|
2014-10-23 07:37:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'waits for message' do
|
|
|
|
should be(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-05 23:34:04 -05:00
|
|
|
context 'when messages contains no message but thread gets waken without message arrived' do
|
2014-10-23 07:37:53 -04:00
|
|
|
before do
|
|
|
|
# 1rst failing try
|
2014-12-05 23:34:04 -05:00
|
|
|
expect(mutex).to receive(:synchronize).and_yield.ordered
|
|
|
|
expect(messages).to receive(:empty?).and_return(true).ordered
|
|
|
|
expect(condition_variable).to receive(:wait).with(mutex).ordered
|
2014-10-23 07:37:53 -04:00
|
|
|
# 2nd failing try
|
2014-12-05 23:34:04 -05:00
|
|
|
expect(mutex).to receive(:synchronize).and_yield.ordered
|
|
|
|
expect(messages).to receive(:empty?).and_return(true).ordered
|
|
|
|
expect(condition_variable).to receive(:wait).with(mutex).ordered
|
2014-10-23 07:37:53 -04:00
|
|
|
end
|
|
|
|
|
2014-12-05 23:34:04 -05:00
|
|
|
it 'fails with error' do
|
2014-10-23 07:37:53 -04:00
|
|
|
expect { subject }.to raise_error(Mutant::Actor::ProtocolError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|