spec/mongo/retryable_spec.rb in mongo-2.6.4 vs spec/mongo/retryable_spec.rb in mongo-2.7.0.rc0

- old
+ new

@@ -65,10 +65,16 @@ def write_concern nil end end +class RetryableHost + include Mongo::Retryable + + public :retry_write_allowed? +end + describe Mongo::Retryable do let(:operation) do double('operation') end @@ -96,10 +102,20 @@ it 'executes the operation once' do expect(retryable.read).to be true end end + context 'when ending_transaction is true' do + let(:retryable) { RetryableTestConsumer.new(operation, cluster) } + + it 'raises ArgumentError' do + expect do + retryable.write_with_retry(nil, nil, true) + end.to raise_error(ArgumentError, 'Cannot end a transaction without a session') + end + end + context 'when a socket error occurs' do before do expect(operation).to receive(:execute).and_raise(Mongo::Error::SocketError).ordered expect(cluster).to receive(:max_read_retries).and_return(1).ordered @@ -199,9 +215,77 @@ it 'returns the result' do expect(retryable.read).to be true end end + end + end + end + end + + describe '#retry_write_allowed?' do + let(:retryable) { RetryableHost.new } + + context 'nil session' do + it 'returns false' do + expect(retryable.retry_write_allowed?(nil, nil)).to be false + end + end + + context 'with session' do + let(:session) { double('session') } + + context 'retry writes enabled' do + context 'nil write concern' do + let(:write_concern) { nil } + + it 'returns true' do + expect(session).to receive(:retry_writes?).and_return(true) + expect(retryable.retry_write_allowed?(session, write_concern)).to be true + end + end + + context 'hash write concern with w: 0' do + let(:write_concern) { {w: 0} } + + it 'returns false' do + expect(session).to receive(:retry_writes?).and_return(true) + expect(retryable.retry_write_allowed?(session, write_concern)).to be false + end + end + + context 'hash write concern with w: :majority' do + let(:write_concern) { {w: :majority} } + + it 'returns true' do + expect(session).to receive(:retry_writes?).and_return(true) + expect(retryable.retry_write_allowed?(session, write_concern)).to be true + end + end + + context 'write concern object with w: 0' do + let(:write_concern) { Mongo::WriteConcern.get(w: 0) } + + it 'returns false' do + expect(session).to receive(:retry_writes?).and_return(true) + expect(retryable.retry_write_allowed?(session, write_concern)).to be false + end + end + + context 'write concern object with w: :majority' do + let(:write_concern) { Mongo::WriteConcern.get(w: :majority) } + + it 'returns true' do + expect(session).to receive(:retry_writes?).and_return(true) + expect(retryable.retry_write_allowed?(session, write_concern)).to be true + end + end + end + + context 'retry writes not enabled' do + it 'returns false' do + expect(session).to receive(:retry_writes?).and_return(false) + expect(retryable.retry_write_allowed?(session, nil)).to be false end end end end