|
13 | 13 | # limitations under the License. |
14 | 14 | # |
15 | 15 |
|
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 | | - |
65 | 16 | module Mongo |
66 | 17 | 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 |
133 | 18 |
|
134 | 19 | # Test subscriber for SDAM monitoring. |
135 | 20 | # |
|
0 commit comments