spec/support/sdam_monitoring.rb in mongo-2.6.4 vs spec/support/sdam_monitoring.rb in mongo-2.7.0.rc0
- old
+ new
@@ -14,41 +14,46 @@
#
RSpec::Matchers.define :match_topology_opening_event do |expectation|
match do |event|
- event.topology != nil
+ event.is_a?(Mongo::Monitoring::Event::TopologyOpening) &&
+ event.topology != nil
end
end
RSpec::Matchers.define :match_topology_description_changed_event do |expectation|
include Mongo::SDAMMonitoring::Matchable
match do |event|
- topologies_match?(event, expectation)
+ event.is_a?(Mongo::Monitoring::Event::TopologyChanged) &&
+ topologies_match?(event, expectation)
end
end
RSpec::Matchers.define :match_server_opening_event do |expectation|
match do |event|
- true
+ event.is_a?(Mongo::Monitoring::Event::ServerOpening) &&
+ event.address.to_s == expectation.data['address']
end
end
RSpec::Matchers.define :match_server_description_changed_event do |expectation|
include Mongo::SDAMMonitoring::Matchable
match do |event|
- descriptions_match?(event, expectation)
+ event.is_a?(Mongo::Monitoring::Event::ServerDescriptionChanged) &&
+ descriptions_match?(event, expectation)
end
end
RSpec::Matchers.define :match_server_closed_event do |expectation|
match do |event|
- event.address.to_s == expectation.data['address']
+ event.is_a?(Mongo::Monitoring::Event::ServerClosed) &&
+ event.address.to_s == expectation.data['address']
end
end
RSpec::Matchers.define :match_sdam_monitoring_event do |expectation|
@@ -65,36 +70,66 @@
description_matches?(event.previous_description, expectation.data['previousDescription']) &&
description_matches?(event.new_description, expectation.data['newDescription'])
end
def topologies_match?(event, expectation)
- topology_matches?(event.previous_topology, expectation.data['previousDescription']) &&
- topology_matches?(event.new_topology, expectation.data['newDescription'])
+ unless topology_matches?(event.previous_topology, expectation.data['previousDescription'])
+ if ENV['VERBOSE_MATCHERS']
+ $stderr.puts "Previous topology mismatch"
+ end
+ return false
+ end
+ unless topology_matches?(event.new_topology, expectation.data['newDescription'])
+ if ENV['VERBOSE_MATCHERS']
+ $stderr.puts "New topology mismatch:\nHave: #{event.new_topology}\nWant: #{expectation.data['newDescription']}"
+ end
+ return false
+ end
+ true
end
def description_matches?(actual, expected)
- case expected['type']
+ type_ok = case expected['type']
when 'Standalone' then actual.standalone?
when 'RSPrimary' then actual.primary?
when 'RSSecondary' then actual.secondary?
when 'RSArbiter' then actual.arbiter?
when 'Mongos' then actual.mongos?
when 'Unknown' then actual.unknown?
when 'PossiblePrimary' then actual.unknown?
when 'RSGhost' then actual.ghost?
when 'RSOther' then actual.other?
end
+ return false unless type_ok
+
+ return false if actual.address.to_s != expected['address']
+ return false if actual.arbiters != expected['arbiters']
+ return false if actual.hosts != expected['hosts']
+ return false if actual.passives != expected['passives']
+ return false if actual.primary_host != expected['primary']
+ return false if actual.replica_set_name != expected['setName']
+ true
end
def topology_matches?(actual, expected)
- case expected['topologyType']
- when 'ReplicaSetWithPrimary' then actual.replica_set?
- when 'ReplicaSetNoPrimary' then (actual.replica_set? || actual.unknown?)
- when 'Sharded' then actual.sharded?
- when 'Single' then actual.single?
- when 'Unknown' then actual.unknown?
+ expected_type = ::Mongo::Cluster::Topology.const_get(expected['topologyType'])
+ return false unless actual.is_a?(expected_type)
+
+ return false unless actual.replica_set_name == expected['setName']
+
+ expected['servers'].each do |server|
+ desc = actual.server_descriptions[server['address'].to_s]
+ return false unless description_matches?(desc, server)
end
+
+ actual.server_descriptions.keys.each do |address_str|
+ unless expected['servers'].any? { |server| server['address'] == address_str }
+ return false
+ end
+ end
+
+ true
end
end
# Test subscriber for SDAM monitoring.
#
@@ -105,10 +140,11 @@
#
# @since 2.4.0
MAPPINGS = {
'topology_opening_event' => Mongo::Monitoring::Event::TopologyOpening,
'topology_description_changed_event' => Mongo::Monitoring::Event::TopologyChanged,
+ 'topology_closed_event' => Mongo::Monitoring::Event::TopologyClosed,
'server_opening_event' => Mongo::Monitoring::Event::ServerOpening,
'server_description_changed_event' => Mongo::Monitoring::Event::ServerDescriptionChanged,
'server_closed_event' => Mongo::Monitoring::Event::ServerClosed
}.freeze
@@ -125,20 +161,44 @@
#
# @param [ String ] name The event name.
#
# @return [ Event ] The matching event.
def first_event(name)
+ cls = MAPPINGS[name]
+ if cls.nil?
+ raise ArgumentError, "Bogus event name #{name}"
+ end
matching = events.find do |event|
- event.class == MAPPINGS[name]
+ cls === event
end
events.delete(matching)
matching
end
- private
-
def events
@events ||= []
+ end
+ end
+
+ class PhasedTestSubscriber < TestSubscriber
+ def initialize
+ super
+ @phase_events = {}
+ end
+
+ def phase_finished(phase_index)
+ @phase_events[phase_index] = events
+ @events = []
+ end
+
+ def phase_events(phase_index)
+ @phase_events[phase_index]
+ end
+
+ def event_count
+ @phase_events.inject(0) do |sum, event|
+ sum + event.length
+ end
end
end
end
end