-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
359 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "base_matcher" | ||
|
||
module Yabeda | ||
module RSpec | ||
# Checks whether Yabeda summary was observed during test run or not | ||
# @param metric [Yabeda::Summary,String,Symbol] metric instance or name | ||
# @return [Yabeda::RSpec::ObserveYabedaSummary] | ||
def observe_yabeda_summary(metric) | ||
ObserveYabedaSummary.new(metric) | ||
end | ||
|
||
# Custom matcher class with implementation for +observe_yabeda_summary+ | ||
class ObserveYabedaSummary < BaseMatcher | ||
def with(value) | ||
@expected_value = value | ||
self | ||
end | ||
|
||
attr_reader :expected_value | ||
|
||
def initialize(*) | ||
super | ||
return if metric.is_a? Yabeda::Summary | ||
|
||
raise ArgumentError, "Pass summary instance/name to `observe_yabeda_summary`. Got #{metric.inspect} instead" | ||
end | ||
|
||
def match(metric, block) | ||
block.call | ||
|
||
observations = filter_matching_changes(Yabeda::TestAdapter.instance.summaries.fetch(metric)) | ||
|
||
observations.values.any? { |observation| expected_value.nil? || values_match?(expected_value, observation) } | ||
end | ||
|
||
def match_when_negated(metric, block) | ||
unless expected_value.nil? | ||
raise NotImplementedError, <<~MSG | ||
`expect {}.not_to observe_yabeda_summary` doesn't support specifying values with `.with` | ||
as it can lead to false positives. | ||
MSG | ||
end | ||
|
||
block.call | ||
|
||
observations = filter_matching_changes(Yabeda::TestAdapter.instance.summaries.fetch(metric)) | ||
|
||
observations.none? | ||
end | ||
|
||
def failure_message | ||
"expected #{expected_formatted} " \ | ||
"to be observed #{"with #{expected} " unless expected_value.nil?}" \ | ||
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \ | ||
"but #{actual_changes_message}" | ||
end | ||
|
||
def failure_message_when_negated | ||
"expected #{expected_formatted} " \ | ||
"not to be observed " \ | ||
"#{"with tags #{::RSpec::Support::ObjectFormatter.format(tags)} " if tags}" \ | ||
"but #{actual_changes_message}" | ||
end | ||
|
||
def actual_changes_message | ||
observations = Yabeda::TestAdapter.instance.summaries.fetch(metric) | ||
if observations.empty? | ||
"no observations of this summary have been made" | ||
elsif tags && observations.key?(tags) | ||
formatted_tags = ::RSpec::Support::ObjectFormatter.format(tags) | ||
"has been observed with #{observations.fetch(tags)} with tags #{formatted_tags}" | ||
else | ||
"following observations have been made: #{::RSpec::Support::ObjectFormatter.format(observations)}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yabeda | ||
# Base class for complex metric for measuring time values that allow to | ||
# calculate averages, percentiles, and so on. | ||
class Summary < Metric | ||
# rubocop: disable Metrics/MethodLength | ||
def observe(tags, value = nil) | ||
if value.nil? ^ block_given? | ||
raise ArgumentError, "You must provide either numeric value or block for Yabeda::Summary#observe!" | ||
end | ||
|
||
if block_given? | ||
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
yield | ||
value = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting) | ||
end | ||
|
||
all_tags = ::Yabeda::Tags.build(tags, group) | ||
values[all_tags] = value | ||
::Yabeda.adapters.each do |_, adapter| | ||
adapter.perform_summary_observe!(self, all_tags, value) | ||
end | ||
value | ||
end | ||
# rubocop: enable Metrics/MethodLength | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Yabeda | ||
VERSION = "0.11.0" | ||
VERSION = "0.12.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.