Skip to content

Commit ebc3da7

Browse files
p-mongop
authored andcommitted
RUBY-2015 Convert sdam monitoring matchers to verifier (#1574)
1 parent 1d27563 commit ebc3da7

File tree

3 files changed

+97
-119
lines changed

3 files changed

+97
-119
lines changed

spec/runners/sdam/verifier.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module Sdam
2+
class Verifier
3+
include RSpec::Matchers
4+
5+
def verify_sdam_event(expected_events, actual_events, i)
6+
expect(expected_events.length).to be > i
7+
expect(actual_events.length).to be > i
8+
9+
expected_event = expected_events[i]
10+
actual_event = actual_events[i]
11+
12+
actual_event_name = Utils.underscore(actual_event.class.name.sub(/.*::/, ''))
13+
actual_event_name = actual_event_name.to_s.sub('topology_changed', 'topology_description_changed') + '_event'
14+
expect(actual_event_name).to eq(expected_event.name)
15+
16+
send("verify_#{expected_event.name}", expected_event, actual_event)
17+
end
18+
19+
def verify_topology_opening_event(expected, actual)
20+
expect(actual.topology).not_to be nil
21+
end
22+
23+
def verify_topology_description_changed_event(expected, actual)
24+
verify_topology_matches(expected.data['previousDescription'], actual.previous_topology)
25+
verify_topology_matches(expected.data['newDescription'], actual.new_topology)
26+
end
27+
28+
def verify_topology_matches(expected, actual)
29+
expected_type = ::Mongo::Cluster::Topology.const_get(expected['topologyType'])
30+
expect(actual).to be_a(expected_type)
31+
32+
expect(actual.replica_set_name).to eq(expected['setName'])
33+
34+
expected['servers'].each do |server|
35+
desc = actual.server_descriptions[server['address'].to_s]
36+
expect(desc).not_to be nil
37+
verify_description_matches(server, desc)
38+
end
39+
40+
actual.server_descriptions.keys.each do |address_str|
41+
expect(
42+
expected['servers'].any? { |server| server['address'] == address_str }
43+
).to be true
44+
end
45+
end
46+
47+
def verify_server_opening_event(expected, actual)
48+
expect(actual.address.to_s).to eq(expected.data['address'])
49+
end
50+
51+
def verify_server_description_changed_event(expected, actual)
52+
verify_description_matches(expected.data['previousDescription'], actual.previous_description)
53+
verify_description_matches(expected.data['newDescription'], actual.new_description)
54+
end
55+
56+
def verify_description_matches(expected, actual)
57+
case expected['type']
58+
when 'Standalone'
59+
expect(actual).to be_standalone
60+
when 'RSPrimary'
61+
expect(actual).to be_primary
62+
when 'RSSecondary'
63+
expect(actual).to be_secondary
64+
when 'RSArbiter'
65+
expect(actual).to be_arbiter
66+
when 'Mongos'
67+
expect(actual).to be_mongos
68+
when 'Unknown', 'PossiblePrimary'
69+
expect(actual).to be_unknown
70+
when 'RSGhost'
71+
expect(actual).to be_ghost
72+
when 'RSOther'
73+
expect(actual).to be_other
74+
end
75+
76+
expect(actual.address.to_s).to eq(expected['address'])
77+
expect(actual.arbiters).to eq(expected['arbiters'])
78+
expect(actual.hosts).to eq(expected['hosts'])
79+
expect(actual.passives).to eq(expected['passives'])
80+
expect(actual.primary_host).to eq(expected['primary'])
81+
expect(actual.replica_set_name).to eq(expected['setName'])
82+
end
83+
84+
def verify_server_closed_event(expected, actual)
85+
expect(actual.address.to_s).to eq(expected.data['address'])
86+
end
87+
end
88+
end

spec/spec_tests/sdam_monitoring_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'lite_spec_helper'
22

3+
require_relative '../runners/sdam/verifier'
4+
35
describe 'SDAM Monitoring' do
46
include Mongo::SDAM
57

@@ -72,12 +74,15 @@
7274
expect(@subscriber.phase_events(phase_index).length).to eq(phase.outcome.events.length)
7375
end
7476

77+
let(:verifier) do
78+
Sdam::Verifier.new
79+
end
80+
7581
phase.outcome.events.each_with_index do |expectation, index|
7682

77-
it "expects a #{expectation.name} to be published" do
78-
published_event = @subscriber.phase_events(phase_index)[index]
79-
expect(published_event).not_to be_nil
80-
expect(published_event).to match_sdam_monitoring_event(expectation)
83+
it "expects event #{index+1} to be #{expectation.name}" do
84+
verifier.verify_sdam_event(
85+
phase.outcome.events, @subscriber.phase_events(phase_index), index)
8186
end
8287
end
8388
end

spec/support/sdam_monitoring.rb

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -13,123 +13,8 @@
1313
# limitations under the License.
1414
#
1515

16-
RSpec::Matchers.define :match_topology_opening_event do |expectation|
17-
18-
match do |event|
19-
event.is_a?(Mongo::Monitoring::Event::TopologyOpening) &&
20-
event.topology != nil
21-
end
22-
end
23-
24-
RSpec::Matchers.define :match_topology_description_changed_event do |expectation|
25-
include Mongo::SDAMMonitoring::Matchable
26-
27-
match do |event|
28-
event.is_a?(Mongo::Monitoring::Event::TopologyChanged) &&
29-
topologies_match?(event, expectation)
30-
end
31-
end
32-
33-
RSpec::Matchers.define :match_server_opening_event do |expectation|
34-
35-
match do |event|
36-
event.is_a?(Mongo::Monitoring::Event::ServerOpening) &&
37-
event.address.to_s == expectation.data['address']
38-
end
39-
end
40-
41-
RSpec::Matchers.define :match_server_description_changed_event do |expectation|
42-
include Mongo::SDAMMonitoring::Matchable
43-
44-
match do |event|
45-
event.is_a?(Mongo::Monitoring::Event::ServerDescriptionChanged) &&
46-
descriptions_match?(event, expectation)
47-
end
48-
end
49-
50-
RSpec::Matchers.define :match_server_closed_event do |expectation|
51-
52-
match do |event|
53-
event.is_a?(Mongo::Monitoring::Event::ServerClosed) &&
54-
event.address.to_s == expectation.data['address']
55-
end
56-
end
57-
58-
RSpec::Matchers.define :match_sdam_monitoring_event do |expectation|
59-
60-
match do |event|
61-
expect(event).to send("match_#{expectation.name}", expectation)
62-
end
63-
end
64-
6516
module Mongo
6617
module SDAMMonitoring
67-
module Matchable
68-
69-
def descriptions_match?(event, expectation)
70-
description_matches?(event.previous_description, expectation.data['previousDescription']) &&
71-
description_matches?(event.new_description, expectation.data['newDescription'])
72-
end
73-
74-
def topologies_match?(event, expectation)
75-
unless topology_matches?(event.previous_topology, expectation.data['previousDescription'])
76-
if ENV['VERBOSE_MATCHERS']
77-
$stderr.puts "Previous topology mismatch"
78-
end
79-
return false
80-
end
81-
unless topology_matches?(event.new_topology, expectation.data['newDescription'])
82-
if ENV['VERBOSE_MATCHERS']
83-
$stderr.puts "New topology mismatch:\nHave: #{event.new_topology}\nWant: #{expectation.data['newDescription']}"
84-
end
85-
return false
86-
end
87-
true
88-
end
89-
90-
def description_matches?(actual, expected)
91-
type_ok = case expected['type']
92-
when 'Standalone' then actual.standalone?
93-
when 'RSPrimary' then actual.primary?
94-
when 'RSSecondary' then actual.secondary?
95-
when 'RSArbiter' then actual.arbiter?
96-
when 'Mongos' then actual.mongos?
97-
when 'Unknown' then actual.unknown?
98-
when 'PossiblePrimary' then actual.unknown?
99-
when 'RSGhost' then actual.ghost?
100-
when 'RSOther' then actual.other?
101-
end
102-
return false unless type_ok
103-
104-
return false if actual.address.to_s != expected['address']
105-
return false if actual.arbiters != expected['arbiters']
106-
return false if actual.hosts != expected['hosts']
107-
return false if actual.passives != expected['passives']
108-
return false if actual.primary_host != expected['primary']
109-
return false if actual.replica_set_name != expected['setName']
110-
true
111-
end
112-
113-
def topology_matches?(actual, expected)
114-
expected_type = ::Mongo::Cluster::Topology.const_get(expected['topologyType'])
115-
return false unless actual.is_a?(expected_type)
116-
117-
return false unless actual.replica_set_name == expected['setName']
118-
119-
expected['servers'].each do |server|
120-
desc = actual.server_descriptions[server['address'].to_s]
121-
return false unless description_matches?(desc, server)
122-
end
123-
124-
actual.server_descriptions.keys.each do |address_str|
125-
unless expected['servers'].any? { |server| server['address'] == address_str }
126-
return false
127-
end
128-
end
129-
130-
true
131-
end
132-
end
13318

13419
# Test subscriber for SDAM monitoring.
13520
#

0 commit comments

Comments
 (0)