Class: BrainzLab::Testing::EventStore

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/testing/event_store.rb

Overview

Thread-safe store for captured events, logs, errors, and metrics during tests

This class is used internally by the testing helpers to store all captured data from stubbed SDK calls.

Instance Method Summary collapse

Constructor Details

#initializeEventStore

Returns a new instance of EventStore.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/brainzlab/testing/event_store.rb', line 10

def initialize
  @mutex = Mutex.new
  @events = []
  @metrics = []
  @logs = []
  @errors = []
  @error_messages = []
  @traces = []
  @alerts = []
  @notifications = []
  @triggers = []
end

Instance Method Details

#alert_sent?(name, message: nil, severity: nil) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
282
283
# File 'lib/brainzlab/testing/event_store.rb', line 273

def alert_sent?(name, message: nil, severity: nil)
  @mutex.synchronize do
    @alerts.any? do |alert|
      next false unless alert[:name] == name.to_s
      next false if message && !alert[:message].include?(message.to_s)
      next false if severity && alert[:severity] != severity.to_sym

      true
    end
  end
end

#alertsObject



269
270
271
# File 'lib/brainzlab/testing/event_store.rb', line 269

def alerts
  @mutex.synchronize { @alerts.dup }
end

#clear!Object

General ===



333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/brainzlab/testing/event_store.rb', line 333

def clear!
  @mutex.synchronize do
    @events.clear
    @metrics.clear
    @logs.clear
    @errors.clear
    @error_messages.clear
    @traces.clear
    @alerts.clear
    @notifications.clear
    @triggers.clear
  end
end

#clear_alerts!Object



285
286
287
# File 'lib/brainzlab/testing/event_store.rb', line 285

def clear_alerts!
  @mutex.synchronize { @alerts.clear }
end

#clear_errors!Object



216
217
218
219
220
221
# File 'lib/brainzlab/testing/event_store.rb', line 216

def clear_errors!
  @mutex.synchronize do
    @errors.clear
    @error_messages.clear
  end
end

#clear_events!Object



60
61
62
# File 'lib/brainzlab/testing/event_store.rb', line 60

def clear_events!
  @mutex.synchronize { @events.clear }
end

#clear_logs!Object



149
150
151
# File 'lib/brainzlab/testing/event_store.rb', line 149

def clear_logs!
  @mutex.synchronize { @logs.clear }
end

#clear_metrics!Object



101
102
103
# File 'lib/brainzlab/testing/event_store.rb', line 101

def clear_metrics!
  @mutex.synchronize { @metrics.clear }
end

#clear_notifications!Object



307
308
309
# File 'lib/brainzlab/testing/event_store.rb', line 307

def clear_notifications!
  @mutex.synchronize { @notifications.clear }
end

#clear_traces!Object



250
251
252
# File 'lib/brainzlab/testing/event_store.rb', line 250

def clear_traces!
  @mutex.synchronize { @traces.clear }
end

#clear_triggers!Object



327
328
329
# File 'lib/brainzlab/testing/event_store.rb', line 327

def clear_triggers!
  @mutex.synchronize { @triggers.clear }
end

#empty?Boolean

Returns:

  • (Boolean)


347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/brainzlab/testing/event_store.rb', line 347

def empty?
  @mutex.synchronize do
    @events.empty? &&
      @metrics.empty? &&
      @logs.empty? &&
      @errors.empty? &&
      @error_messages.empty? &&
      @traces.empty? &&
      @alerts.empty? &&
      @notifications.empty? &&
      @triggers.empty?
  end
end

#error_captured?(error_class = nil, message: nil, context: nil) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/brainzlab/testing/event_store.rb', line 187

def error_captured?(error_class = nil, message: nil, context: nil)
  @mutex.synchronize do
    @errors.any? do |error|
      if error_class
        next false unless error[:error_class] == error_class.to_s ||
                          (error_class.is_a?(Class) && error[:exception].is_a?(error_class))
      end

      if message
        message_matches = case message
                          when Regexp
                            error[:message].match?(message)
                          else
                            error[:message].include?(message.to_s)
                          end
        next false unless message_matches
      end

      next false if context && !properties_match?(error[:context], context)

      true
    end
  end
end

#error_messagesObject



183
184
185
# File 'lib/brainzlab/testing/event_store.rb', line 183

def error_messages
  @mutex.synchronize { @error_messages.dup }
end

#errorsObject



179
180
181
# File 'lib/brainzlab/testing/event_store.rb', line 179

def errors
  @mutex.synchronize { @errors.dup }
end

#event_tracked?(name, properties = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/brainzlab/testing/event_store.rb', line 45

def event_tracked?(name, properties = nil)
  @mutex.synchronize do
    @events.any? do |event|
      next false unless event[:name] == name.to_s
      next true if properties.nil?

      properties_match?(event[:properties], properties)
    end
  end
end

#eventsObject



35
36
37
# File 'lib/brainzlab/testing/event_store.rb', line 35

def events
  @mutex.synchronize { @events.dup }
end

#events_named(name) ⇒ Object



39
40
41
42
43
# File 'lib/brainzlab/testing/event_store.rb', line 39

def events_named(name)
  @mutex.synchronize do
    @events.select { |e| e[:name] == name.to_s }
  end
end

#last_errorObject



212
213
214
# File 'lib/brainzlab/testing/event_store.rb', line 212

def last_error
  @mutex.synchronize { @errors.last }
end

#last_eventObject



56
57
58
# File 'lib/brainzlab/testing/event_store.rb', line 56

def last_event
  @mutex.synchronize { @events.last }
end

#logged?(level, message = nil, data = nil) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/brainzlab/testing/event_store.rb', line 128

def logged?(level, message = nil, data = nil)
  @mutex.synchronize do
    @logs.any? do |log|
      next false unless log[:level] == level.to_sym
      next true if message.nil?

      message_matches = case message
                        when Regexp
                          log[:message].match?(message)
                        else
                          log[:message].include?(message.to_s)
                        end

      next false unless message_matches
      next true if data.nil?

      properties_match?(log[:data], data)
    end
  end
end

#logsObject



118
119
120
# File 'lib/brainzlab/testing/event_store.rb', line 118

def logs
  @mutex.synchronize { @logs.dup }
end

#logs_at_level(level) ⇒ Object



122
123
124
125
126
# File 'lib/brainzlab/testing/event_store.rb', line 122

def logs_at_level(level)
  @mutex.synchronize do
    @logs.select { |l| l[:level] == level.to_sym }
  end
end

#metric_recorded?(type, name, value: nil, tags: nil) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/brainzlab/testing/event_store.rb', line 88

def metric_recorded?(type, name, value: nil, tags: nil)
  @mutex.synchronize do
    @metrics.any? do |metric|
      next false unless metric[:type] == type.to_sym
      next false unless metric[:name] == name.to_s
      next false if value && metric[:value] != value
      next false if tags && !properties_match?(metric[:tags], tags)

      true
    end
  end
end

#metricsObject



78
79
80
# File 'lib/brainzlab/testing/event_store.rb', line 78

def metrics
  @mutex.synchronize { @metrics.dup }
end

#metrics_named(name) ⇒ Object



82
83
84
85
86
# File 'lib/brainzlab/testing/event_store.rb', line 82

def metrics_named(name)
  @mutex.synchronize do
    @metrics.select { |m| m[:name] == name.to_s }
  end
end

#notificationsObject



303
304
305
# File 'lib/brainzlab/testing/event_store.rb', line 303

def notifications
  @mutex.synchronize { @notifications.dup }
end

#record_alert(name, message, severity, channels, data) ⇒ Object

Alerts (Signal) ===



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/brainzlab/testing/event_store.rb', line 256

def record_alert(name, message, severity, channels, data)
  @mutex.synchronize do
    @alerts << {
      name: name.to_s,
      message: message.to_s,
      severity: severity.to_sym,
      channels: channels,
      data: data,
      timestamp: Time.now.utc
    }
  end
end

#record_error(exception, context = {}) ⇒ Object

Errors (Reflex) ===



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/brainzlab/testing/event_store.rb', line 155

def record_error(exception, context = {})
  @mutex.synchronize do
    @errors << {
      exception: exception,
      error_class: exception.class.name,
      message: exception.message,
      backtrace: exception.backtrace,
      context: context,
      timestamp: Time.now.utc
    }
  end
end

#record_error_message(message, level, context = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/brainzlab/testing/event_store.rb', line 168

def record_error_message(message, level, context = {})
  @mutex.synchronize do
    @error_messages << {
      message: message.to_s,
      level: level.to_sym,
      context: context,
      timestamp: Time.now.utc
    }
  end
end

#record_event(name, properties = {}) ⇒ Object

Events (Flux) ===



25
26
27
28
29
30
31
32
33
# File 'lib/brainzlab/testing/event_store.rb', line 25

def record_event(name, properties = {})
  @mutex.synchronize do
    @events << {
      name: name.to_s,
      properties: properties,
      timestamp: Time.now.utc
    }
  end
end

#record_log(level, message, data = {}) ⇒ Object

Logs (Recall) ===



107
108
109
110
111
112
113
114
115
116
# File 'lib/brainzlab/testing/event_store.rb', line 107

def record_log(level, message, data = {})
  @mutex.synchronize do
    @logs << {
      level: level.to_sym,
      message: message.to_s,
      data: data,
      timestamp: Time.now.utc
    }
  end
end

#record_metric(type, name, value, opts = {}) ⇒ Object

Metrics (Flux) ===



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brainzlab/testing/event_store.rb', line 66

def record_metric(type, name, value, opts = {})
  @mutex.synchronize do
    @metrics << {
      type: type.to_sym,
      name: name.to_s,
      value: value,
      tags: opts[:tags] || {},
      timestamp: Time.now.utc
    }
  end
end

#record_notification(channel, message, title, data) ⇒ Object

Notifications (Signal) ===



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/brainzlab/testing/event_store.rb', line 291

def record_notification(channel, message, title, data)
  @mutex.synchronize do
    @notifications << {
      channel: Array(channel).map(&:to_s),
      message: message.to_s,
      title: title,
      data: data,
      timestamp: Time.now.utc
    }
  end
end

#record_trace(name, opts = {}) ⇒ Object

Traces (Pulse) ===



225
226
227
228
229
230
231
232
233
# File 'lib/brainzlab/testing/event_store.rb', line 225

def record_trace(name, opts = {})
  @mutex.synchronize do
    @traces << {
      name: name.to_s,
      options: opts,
      timestamp: Time.now.utc
    }
  end
end

#record_trigger(rule_name, context) ⇒ Object

Triggers (Signal) ===



313
314
315
316
317
318
319
320
321
# File 'lib/brainzlab/testing/event_store.rb', line 313

def record_trigger(rule_name, context)
  @mutex.synchronize do
    @triggers << {
      rule_name: rule_name.to_s,
      context: context,
      timestamp: Time.now.utc
    }
  end
end

#trace_recorded?(name, opts = nil) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
246
247
248
# File 'lib/brainzlab/testing/event_store.rb', line 239

def trace_recorded?(name, opts = nil)
  @mutex.synchronize do
    @traces.any? do |trace|
      next false unless trace[:name] == name.to_s
      next true if opts.nil?

      properties_match?(trace[:options], opts)
    end
  end
end

#tracesObject



235
236
237
# File 'lib/brainzlab/testing/event_store.rb', line 235

def traces
  @mutex.synchronize { @traces.dup }
end

#triggersObject



323
324
325
# File 'lib/brainzlab/testing/event_store.rb', line 323

def triggers
  @mutex.synchronize { @triggers.dup }
end