spec/mongo/operation/write/command/insert_spec.rb in mongo-2.5.0.beta vs spec/mongo/operation/write/command/insert_spec.rb in mongo-2.5.0

- old
+ new

@@ -1,16 +1,18 @@ require 'spec_helper' describe Mongo::Operation::Write::Command::Insert do let(:documents) { [{ :_id => 1, :foo => 1 }] } + let(:session) { nil } let(:spec) do { :documents => documents, :db_name => authorized_collection.database.name, :coll_name => authorized_collection.name, :write_concern => write_concern, - :ordered => true + :ordered => true, + :session => session } end let(:write_concern) do Mongo::WriteConcern.get(WRITE_CONCERN) @@ -95,11 +97,12 @@ let(:global_args) do { insert: TEST_COLL, ordered: true, writeConcern: write_concern.options, - '$db' => TEST_DB + '$db' => TEST_DB, + lsid: session.session_id } end let(:expected_payload_1) do { @@ -108,12 +111,16 @@ sequence: documents } } end - context 'when the topology is sharded', if: sharded? && op_msg_enabled? do + let(:session) do + authorized_client.start_session + end + context 'when the topology is replica set or sharded', if: (replica_set? || sharded?) && op_msg_enabled? do + let(:expected_global_args) do global_args.merge(Mongo::Operation::CLUSTER_TIME => authorized_client.cluster.cluster_time) end it 'creates the correct OP_MSG message' do @@ -124,11 +131,11 @@ expected_payload_1) op.send(:message, authorized_primary) end end - context 'when the topology is not sharded', if: !sharded? && op_msg_enabled? do + context 'when the topology is standalone', if: standalone? && op_msg_enabled? do let(:expected_global_args) do global_args end @@ -138,21 +145,44 @@ { validating_keys: true }, expected_global_args, expected_payload_1) op.send(:message, authorized_primary) end + + context 'when an implicit session is created and the topology is then updated and the server does not support sessions' do + + let(:expected_global_args) do + global_args.delete(:lsid) + global_args + end + + before do + session.instance_variable_set(:@options, { implicit: true }) + allow(authorized_primary.features).to receive(:sessions_enabled?).and_return(false) + end + + it 'creates the correct OP_MSG message' do + authorized_client.command(ping:1) + expect(Mongo::Protocol::Msg).to receive(:new).with([:none], + { validating_keys: true }, + expected_global_args, + expected_payload_1) + op.send(:message, authorized_primary) + end + end end context 'when the write concern is 0' do let(:write_concern) do Mongo::WriteConcern.get(w: 0) end - context 'when the topology is sharded', if: sharded? && op_msg_enabled? do + context 'when the topology is replica set or sharded', if: (replica_set? || sharded?) && op_msg_enabled? do let(:expected_global_args) do + global_args.delete(:lsid) global_args.merge(Mongo::Operation::CLUSTER_TIME => authorized_client.cluster.cluster_time) end it 'creates the correct OP_MSG message' do authorized_client.command(ping:1) @@ -162,12 +192,13 @@ expected_payload_1) op.send(:message, authorized_primary) end end - context 'when the topology is not sharded', if: !sharded? && op_msg_enabled? do + context 'when the topology is standalone', if: standalone? && op_msg_enabled? do let(:expected_global_args) do + global_args.delete(:lsid) global_args end it 'creates the correct OP_MSG message' do authorized_client.command(ping:1)