File: sdam_monitoring.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (144 lines) | stat: -rw-r--r-- 4,263 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2014-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

RSpec::Matchers.define :match_topology_opening_event do |expectation|

  match do |event|
    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)
  end
end

RSpec::Matchers.define :match_server_opening_event do |expectation|

  match do |event|
    true
  end
end

RSpec::Matchers.define :match_server_description_changed_event do |expectation|
  include Mongo::SDAMMonitoring::Matchable

  match do |event|
    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']
  end
end

RSpec::Matchers.define :match_sdam_monitoring_event do |expectation|

  match do |event|
    expect(event).to send("match_#{expectation.name}", expectation)
  end
end

module Mongo
  module SDAMMonitoring
    module Matchable

      def descriptions_match?(event, expectation)
        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'])
      end

      def description_matches?(actual, expected)
        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
      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?
        end
      end
    end

    # Test subscriber for SDAM monitoring.
    #
    # @since 2.4.0
    class TestSubscriber

      # The mappings of event names to types.
      #
      # @since 2.4.0
      MAPPINGS = {
        'topology_opening_event' => Mongo::Monitoring::Event::TopologyOpening,
        'topology_description_changed_event' => Mongo::Monitoring::Event::TopologyChanged,
        'server_opening_event' => Mongo::Monitoring::Event::ServerOpening,
        'server_description_changed_event' => Mongo::Monitoring::Event::ServerDescriptionChanged,
        'server_closed_event' => Mongo::Monitoring::Event::ServerClosed
      }.freeze

      # Implement the succeeded event.
      #
      # @param [ Event ] event The event.
      #
      # @since 2.4.0
      def succeeded(event)
        events.push(event)
      end

      # Get the first event fired for the name, and then delete it.
      #
      # @param [ String ] name The event name.
      #
      # @return [ Event ] The matching event.
      def first_event(name)
        matching = events.find do |event|
          event.class == MAPPINGS[name]
        end
        events.delete(matching)
        matching
      end

      private

      def events
        @events ||= []
      end
    end
  end
end