spec/mongo/server/monitor_spec.rb in mongo-2.12.4 vs spec/mongo/server/monitor_spec.rb in mongo-2.13.0.beta1

- old
+ new

@@ -52,18 +52,12 @@ let(:monitor_options) do {monitoring_io: false} end - let(:socket) do - monitor.connection.connect! - monitor.connection.__send__(:socket) - end - - it 'retries the ismaster' do - expect(socket).to receive(:write).once.and_raise(Mongo::Error::SocketError) - expect(socket).to receive(:write).and_call_original + it 'runs sdam flow on unknown description' do + expect(monitor).to receive(:ismaster).once.and_raise(Mongo::Error::SocketError) expect(cluster).to receive(:run_sdam_flow) monitor.scan! end end @@ -106,28 +100,23 @@ let(:address) do default_address end - let(:socket) do - monitor.connection.connect! - monitor.connection.__send__(:socket) - end - before do - expect(socket).to receive(:write).twice.and_raise(Mongo::Error::SocketError) server.unknown! expect(server.description).to be_unknown + expect(monitor).to receive(:ismaster).and_raise(Mongo::Error::SocketError) monitor.scan! end it 'keeps the server unknown' do expect(server.description).to be_unknown end it 'disconnects the connection' do - expect(monitor.connection).to_not be_connected + expect(monitor.connection).to be nil end end end end @@ -232,23 +221,93 @@ let(:monitor_options) do {connect_timeout: connect_timeout} end it 'sets the value as the timeout on the connection' do - expect(monitor.connection.timeout).to eq(connect_timeout) + monitor.scan! + expect(monitor.connection.socket_timeout).to eq(connect_timeout) end it 'set the value as the timeout on the socket' do - monitor.connection.connect! + monitor.scan! expect(monitor.connection.send(:socket).timeout).to eq(connect_timeout) end end end describe '#log_warn' do it 'works' do expect do monitor.log_warn('test warning') end.not_to raise_error + end + end + + describe '#do_scan' do + + let(:result) { monitor.send(:do_scan) } + + it 'returns a hash' do + expect(result).to be_a(Hash) + end + + it 'is successful' do + expect(result['ok']).to eq(1.0) + end + + context 'network error during ismaster' do + let(:result) do + expect(monitor).to receive(:ismaster).and_raise(IOError) + # The retry is done on a new socket instance. + #expect(socket).to receive(:write).and_call_original + + monitor.send(:do_scan) + end + + it 'adds server diagnostics' do + expect(Mongo::Logger.logger).to receive(:warn) do |msg| + # The "on <address>" and "for <address>" bits are in different parts + # of the message. + expect(msg).to match(/on #{server.address}/) + end + expect(result).to be_a(Hash) + end + end + + context 'network error during connection' do + let(:options) { SpecConfig.instance.test_options } + + let(:expected_message) { "MONGODB | Failed to handshake with #{address}: Mongo::Error::SocketError: test error" } + + before do + monitor.connection.should be nil + end + + it 'logs a warning' do + # Note: the mock call below could mock do_write and raise IOError. + # It is correct in raising Error::SocketError if mocking write + # which performs exception mapping. + expect_any_instance_of(Mongo::Socket).to receive(:write).and_raise(Mongo::Error::SocketError, 'test error') + + messages = [] + expect(Mongo::Logger.logger).to receive(:warn).at_least(:once) do |msg| + messages << msg + end + + monitor.scan!.should be_unknown + + messages.any? { |msg| msg.include?(expected_message) }.should be true + end + + it 'adds server diagnostics' do + # Note: the mock call below could mock do_write and raise IOError. + # It is correct in raising Error::SocketError if mocking write + # which performs exception mapping. + expect_any_instance_of(Mongo::Socket).to receive(:write).and_raise(Mongo::Error::SocketError, 'test error') + + expect do + monitor.send(:ismaster) + end.to raise_error(Mongo::Error::SocketError, /on #{server.address}/) + end end end end