diff --git a/spec/mongo/bulk_write_spec.rb b/spec/mongo/bulk_write_spec.rb index 89c45dda3d..cd3ff7b35f 100644 --- a/spec/mongo/bulk_write_spec.rb +++ b/spec/mongo/bulk_write_spec.rb @@ -1889,32 +1889,26 @@ it_behaves_like 'an operation using a session' end - context 'when retryable writes are supported', if: (sessions_enabled? && (replica_set? || sharded?)) do + context 'when retryable writes are supported', if: test_sessions? do let(:client) do - authorized_client.with(heartbeat_frequency: 100, retry_writes: true).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end + authorized_client_with_retry_writes end let(:collection) do client[authorized_collection.name] end - let(:subscriber) do - EventSubscriber.new - end - let!(:result) do bulk_write.execute end let(:first_txn_number) do - subscriber.started_events[-2].command['txnNumber'].instance_variable_get(:@integer) + EventSubscriber.started_events[-2].command['txnNumber'].instance_variable_get(:@integer) end let(:second_txn_number) do - subscriber.started_events[-1].command['txnNumber'].instance_variable_get(:@integer) + EventSubscriber.started_events[-1].command['txnNumber'].instance_variable_get(:@integer) end it 'inserts the documents' do diff --git a/spec/mongo/client_spec.rb b/spec/mongo/client_spec.rb index 9d45d0bb27..b6e8bcdfe8 100644 --- a/spec/mongo/client_spec.rb +++ b/spec/mongo/client_spec.rb @@ -1360,24 +1360,23 @@ end let(:client) do - # Monitoring subscribers won't be set up when using Client#with, so a new client must be created. Mongo::Client.new(ADDRESSES, client_options).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) + cl.subscribe(Mongo::Monitoring::COMMAND, EventSubscriber.clear_events!) end end - let(:subscriber) do - EventSubscriber.new - end - let(:command) do - subscriber.started_events.find { |c| c.command_name == :listDatabases }.command + EventSubscriber.started_events.find { |c| c.command_name == :listDatabases }.command end before do client.list_databases({}, true) end + after do + client.close + end + it 'sends the command with the nameOnly flag set to true' do expect(command[:nameOnly]).to be(true) end diff --git a/spec/mongo/collection/view/aggregation_spec.rb b/spec/mongo/collection/view/aggregation_spec.rb index 2fb5ab285a..4c05db72e6 100644 --- a/spec/mongo/collection/view/aggregation_spec.rb +++ b/spec/mongo/collection/view/aggregation_spec.rb @@ -231,26 +231,20 @@ end let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end + subscribed_client end let(:session) do client.start_session end - let(:subscriber) do - EventSubscriber.new - end - let(:view) do Mongo::Collection::View.new(client[TEST_COLL], selector, view_options) end let(:command) do aggregation.explain - subscriber.started_events.find { |c| c.command_name == 'aggregate'}.command + EventSubscriber.started_events.find { |c| c.command_name == 'aggregate'}.command end it 'sends the session id' do diff --git a/spec/mongo/collection/view/map_reduce_spec.rb b/spec/mongo/collection/view/map_reduce_spec.rb index efa48b4c53..d4487d96e5 100644 --- a/spec/mongo/collection/view/map_reduce_spec.rb +++ b/spec/mongo/collection/view/map_reduce_spec.rb @@ -240,17 +240,11 @@ end let(:client) do - authorized_client.with(monitoring: true).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new + subscribed_client end let(:find_command) do - subscriber.started_events[-1].command + EventSubscriber.started_events[-1].command end before do diff --git a/spec/mongo/collection/view/readable_spec.rb b/spec/mongo/collection/view/readable_spec.rb index 9bca84b15d..681d26480c 100644 --- a/spec/mongo/collection/view/readable_spec.rb +++ b/spec/mongo/collection/view/readable_spec.rb @@ -191,7 +191,7 @@ let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == 'mapreduce' }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == 'mapreduce' }.command end it_behaves_like 'an operation supporting causally consistent reads' diff --git a/spec/mongo/collection_spec.rb b/spec/mongo/collection_spec.rb index 6ec727ccf1..c7cafbb0fa 100644 --- a/spec/mongo/collection_spec.rb +++ b/spec/mongo/collection_spec.rb @@ -900,26 +900,20 @@ end let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end + subscribed_client end let(:session) do client.start_session end - let(:subscriber) do - EventSubscriber.new - end - let(:view) do Mongo::Collection::View.new(client[TEST_COLL], selector, view_options) end let(:command) do client[TEST_COLL].find({}, session: session).explain - subscriber.started_events.find { |c| c.command_name == :explain }.command + EventSubscriber.started_events.find { |c| c.command_name == :explain }.command end it 'sends the session id' do @@ -935,7 +929,7 @@ let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == 'find' }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == 'find' }.command end it_behaves_like 'an operation supporting causally consistent reads' @@ -1205,13 +1199,7 @@ def generate context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new + subscribed_client end let(:documents) do @@ -1227,7 +1215,7 @@ def generate end let(:insert_events) do - subscriber.started_events.select { |e| e.command_name == :insert } + EventSubscriber.started_events.select { |e| e.command_name == :insert } end it 'sends the documents in one OP_MSG' do @@ -1574,7 +1562,7 @@ def generate let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == 'aggregate' }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == 'aggregate' }.command end it_behaves_like 'an operation supporting causally consistent reads' @@ -1738,7 +1726,7 @@ def generate let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == :count }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == :count }.command end it_behaves_like 'an operation supporting causally consistent reads' @@ -1852,7 +1840,7 @@ def generate let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == :distinct }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == :distinct }.command end it_behaves_like 'an operation supporting causally consistent reads' @@ -2344,7 +2332,7 @@ def generate let(:command) do operation - subscriber.started_events.find { |cmd| cmd.command_name == :parallelCollectionScan }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == :parallelCollectionScan }.command end it_behaves_like 'an operation supporting causally consistent reads' @@ -3486,13 +3474,7 @@ def generate context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new + subscribed_client end let(:documents) do @@ -3509,7 +3491,7 @@ def generate end let(:update_events) do - subscriber.started_events.select { |e| e.command_name == :update } + EventSubscriber.started_events.select { |e| e.command_name == :update } end it 'sends the documents in one OP_MSG' do diff --git a/spec/mongo/cursor_spec.rb b/spec/mongo/cursor_spec.rb index e38a7b0814..c2eee0b54a 100644 --- a/spec/mongo/cursor_spec.rb +++ b/spec/mongo/cursor_spec.rb @@ -329,7 +329,7 @@ context 'when an implicit session is used', if: sessions_enabled? do let(:collection) do - authorized_client_with_subscriber[TEST_COLL] + subscribed_client[TEST_COLL] end before do @@ -354,7 +354,7 @@ end let(:find_events) do - event_subscriber.started_events.select { |e| e.command_name == "find" } + EventSubscriber.started_events.select { |e| e.command_name == "find" } end context 'when all results are retrieved in the first response' do diff --git a/spec/mongo/database_spec.rb b/spec/mongo/database_spec.rb index 9e9c6d9b41..5539b49d71 100644 --- a/spec/mongo/database_spec.rb +++ b/spec/mongo/database_spec.rb @@ -256,13 +256,7 @@ end let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new + subscribed_client end it_behaves_like 'an operation using a session' @@ -270,7 +264,7 @@ let(:full_command) do - subscriber.started_events.find { |cmd| cmd.command_name == :ismaster }.command + EventSubscriber.started_events.find { |cmd| cmd.command_name == :ismaster }.command end it 'does not add a afterClusterTime field' do diff --git a/spec/mongo/retryable_writes_spec.rb b/spec/mongo/retryable_writes_spec.rb index 57a9fc7e58..3548e5e88c 100644 --- a/spec/mongo/retryable_writes_spec.rb +++ b/spec/mongo/retryable_writes_spec.rb @@ -17,13 +17,7 @@ end let(:client) do - authorized_client_with_retry_writes.tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new + authorized_client_with_retry_writes end before do diff --git a/spec/mongo/session/session_pool_spec.rb b/spec/mongo/session/session_pool_spec.rb index 93d281f109..3090647ec4 100644 --- a/spec/mongo/session/session_pool_spec.rb +++ b/spec/mongo/session/session_pool_spec.rb @@ -150,19 +150,13 @@ end let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end + subscribed_client end after do client.close end - let(:subscriber) do - EventSubscriber.new - end - context 'when the number of ids is not larger than 10,000' do before do @@ -177,7 +171,7 @@ let(:end_sessions_command) do pool.end_sessions - subscriber.started_events.find { |c| c.command_name == :endSessions} + EventSubscriber.started_events.find { |c| c.command_name == :endSessions} end it 'sends the endSessions command with all the session ids' do @@ -216,7 +210,7 @@ end let(:end_sessions_commands) do - subscriber.started_events.select { |c| c.command_name == :endSessions} + EventSubscriber.started_events.select { |c| c.command_name == :endSessions} end it 'sends the command more than once' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1f1b3d5448..116a42e66b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -38,6 +38,7 @@ require 'support/travis' require 'support/matchers' +require 'support/event_subscriber' require 'support/authorization' require 'support/server_discovery_and_monitoring' require 'support/server_selection_rtt' @@ -254,65 +255,5 @@ def initialize_scanned_client! Mongo::Client.new(ADDRESSES, TEST_OPTIONS.merge(database: TEST_DB)) end -# Test event subscriber. -# -# @since 2.5.0 -class EventSubscriber - - # The started events. - # - # @since 2.5.0 - attr_reader :started_events - - # The succeeded events. - # - # @since 2.5.0 - attr_reader :succeeded_events - - # The failed events. - # - # @since 2.5.0 - attr_reader :failed_events - - # Create the test event subscriber. - # - # @example Create the subscriber - # EventSubscriber.new - # - # @since 2.5.0 - def initialize - @started_events = [] - @succeeded_events = [] - @failed_events = [] - end - - # Cache the succeeded event. - # - # @param [ Event ] event The event. - # - # @since 2.5.0 - def succeeded(event) - @succeeded_events.push(event) - end - - # Cache the started event. - # - # @param [ Event ] event The event. - # - # @since 2.5.0 - def started(event) - @started_events.push(event) - end - - # Cache the failed event. - # - # @param [ Event ] event The event. - # - # @since 2.5.0 - def failed(event) - @failed_events.push(event) - end -end - # require all shared examples Dir['./spec/support/shared/*.rb'].sort.each { |file| require file } diff --git a/spec/support/authorization.rb b/spec/support/authorization.rb index 439e188a3b..934273bc8b 100644 --- a/spec/support/authorization.rb +++ b/spec/support/authorization.rb @@ -200,6 +200,19 @@ monitoring: false ) +# A client that has an event subscriber for commands. +# +# @since 2.5.1 +SUBSCRIBED_CLIENT = Mongo::Client.new( + ADDRESSES, + TEST_OPTIONS.merge( + database: TEST_DB, + user: TEST_USER.name, + password: TEST_USER.password) +) +SUBSCRIBED_CLIENT.subscribe(Mongo::Monitoring::COMMAND, EventSubscriber) +AUTHROIZED_CLIENT_WITH_RETRY_WRITES.subscribe(Mongo::Monitoring::COMMAND, EventSubscriber) + module Authorization # On inclusion provides helpers for use with testing with and without @@ -228,28 +241,19 @@ def self.included(context) # Provides an authorized mongo client on the default test database that retries writes. # # @since 2.5.1 - context.let(:authorized_client_with_retry_writes) { AUTHROIZED_CLIENT_WITH_RETRY_WRITES } + context.let(:authorized_client_with_retry_writes) do + EventSubscriber.clear_events! + AUTHROIZED_CLIENT_WITH_RETRY_WRITES + end # Provides an authorized mongo client that has a Command subscriber. # # @since 2.5.1 - context.let(:authorized_client_with_subscriber) do - Mongo::Client.new( - ADDRESSES, - TEST_OPTIONS.merge( - database: TEST_DB, - user: TEST_USER.name, - password: TEST_USER.password) - ).tap do |client| - client.subscribe(Mongo::Monitoring::COMMAND, event_subscriber) - end + context.let(:subscribed_client) do + EventSubscriber.clear_events! + SUBSCRIBED_CLIENT end - # A command event subscriber. - # - # @since 2.5.1 - context.let(:event_subscriber) { EventSubscriber.new } - # Provides an unauthorized mongo client on the default test database. # # @since 2.0.0 diff --git a/spec/support/event_subscriber.rb b/spec/support/event_subscriber.rb new file mode 100644 index 0000000000..aa821b4002 --- /dev/null +++ b/spec/support/event_subscriber.rb @@ -0,0 +1,59 @@ +# Test event subscriber. +# +# @since 2.5.0 +class EventSubscriber + + class << self + # The started events. + # + # @since 2.5.0 + attr_reader :started_events + + # The succeeded events. + # + # @since 2.5.0 + attr_reader :succeeded_events + + # The failed events. + # + # @since 2.5.0 + attr_reader :failed_events + + # Cache the succeeded event. + # + # @param [ Event ] event The event. + # + # @since 2.5.0 + def succeeded(event) + @succeeded_events.push(event) + end + + # Cache the started event. + # + # @param [ Event ] event The event. + # + # @since 2.5.0 + def started(event) + @started_events.push(event) + end + + # Cache the failed event. + # + # @param [ Event ] event The event. + # + # @since 2.5.0 + def failed(event) + @failed_events.push(event) + end + + # Clear all cached events. + # + # @since 2.5.1 + def clear_events! + @started_events = [] + @succeeded_events = [] + @failed_events = [] + self + end + end +end diff --git a/spec/support/shared/session.rb b/spec/support/shared/session.rb index f25b00a301..b48b97711b 100644 --- a/spec/support/shared/session.rb +++ b/spec/support/shared/session.rb @@ -135,17 +135,7 @@ shared_examples 'an operation supporting causally consistent reads' do let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new - end - - after do - client.close + subscribed_client end context 'when connected to a standalone', if: sessions_enabled? && standalone? do @@ -572,17 +562,7 @@ end let(:client) do - authorized_client.with(heartbeat_frequency: 100).tap do |cl| - cl.subscribe(Mongo::Monitoring::COMMAND, subscriber) - end - end - - let(:subscriber) do - EventSubscriber.new - end - - after do - client.close + subscribed_client end context 'when the command is run once' do @@ -593,7 +573,7 @@ let!(:reply_cluster_time) do operation_with_session - subscriber.succeeded_events[-1].reply['$clusterTime'] + EventSubscriber.succeeded_events[-1].reply['$clusterTime'] end it 'updates the cluster time of the cluster' do @@ -613,7 +593,7 @@ let!(:reply_cluster_time) do operation_with_session - subscriber.succeeded_events[-1].reply['$clusterTime'] + EventSubscriber.succeeded_events[-1].reply['$clusterTime'] end it 'does not update the cluster time of the cluster' do @@ -634,7 +614,7 @@ let!(:reply_cluster_time) do operation - subscriber.succeeded_events[-1].reply['$clusterTime'] + EventSubscriber.succeeded_events[-1].reply['$clusterTime'] end it 'does not update the cluster time of the cluster' do @@ -647,7 +627,7 @@ let!(:reply_cluster_time) do operation_with_session - subscriber.succeeded_events[-1].reply['$clusterTime'] + EventSubscriber.succeeded_events[-1].reply['$clusterTime'] end context 'when the cluster is sharded or a replica set', if: test_sessions? do @@ -660,7 +640,7 @@ let(:second_command_cluster_time) do second_operation - subscriber.started_events[-1].command['$clusterTime'] + EventSubscriber.started_events[-1].command['$clusterTime'] end context 'when the advanced cluster time is greater than the existing cluster time' do @@ -696,7 +676,7 @@ let(:second_command_cluster_time) do second_operation - subscriber.started_events[-1].command['$clusterTime'] + EventSubscriber.started_events[-1].command['$clusterTime'] end it 'includes the received cluster time in the second command' do @@ -713,7 +693,7 @@ let(:second_command_cluster_time) do second_operation - subscriber.started_events[-1].command['$clusterTime'] + EventSubscriber.started_events[-1].command['$clusterTime'] end it 'does not update the cluster time of the cluster' do